diff options
author | Kitae Kim <kt920.kim@samsung.com> | 2012-08-29 22:47:03 +0900 |
---|---|---|
committer | Kitae Kim <kt920.kim@samsung.com> | 2012-08-29 22:47:03 +0900 |
commit | f6a2b9995b2d22971fd3c84a01794acc1f340172 (patch) | |
tree | 7481510e8a4960bcc7bac1bc7f2df3cdd272947c | |
parent | 3a62b2d953b2e36678962aaa21dafbd45dbe85c8 (diff) | |
download | gstreamer0.10-ffmpeg-emulator-f6a2b9995b2d22971fd3c84a01794acc1f340172.tar.gz gstreamer0.10-ffmpeg-emulator-f6a2b9995b2d22971fd3c84a01794acc1f340172.tar.bz2 gstreamer0.10-ffmpeg-emulator-f6a2b9995b2d22971fd3c84a01794acc1f340172.zip |
Change a way to use device memory of codec module.
-rw-r--r-- | debian/changelog | 8 | ||||
-rw-r--r-- | ext/ffmpeg/gstffmpeg.c | 15 | ||||
-rw-r--r-- | ext/ffmpeg/gstffmpegdec.c | 2 | ||||
-rw-r--r-- | ext/ffmpeg/gstffmpegdef.c | 1460 | ||||
-rw-r--r-- | ext/ffmpeg/gstffmpegdef.h | 179 | ||||
-rw-r--r-- | ext/ffmpeg/gstffmpegenc.c | 59 | ||||
-rw-r--r-- | packaging/gst-ffmpeg-emul.spec | 2 |
7 files changed, 773 insertions, 952 deletions
diff --git a/debian/changelog b/debian/changelog index f788061..3dec681 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,10 +1,16 @@ +gstreamer0.10-ffmpeg-emul (0.2.17) unstable; urgency=low + + * change a way to use device memory of codec module. + + -- Kitae Kim <kt920.kim@samsung.com> Wed, 29 Aug 2012 22:41:15 +0900 + gstreamer0.10-ffmpeg-emul (0.2.16) unstable; urgency=low * change a way to get memory region from kernel. * Git: slp/pkgs/g/gstreamer0.10-ffmpeg-emulator * Tag: gstreamer0.10-ffmpeg-emul_0.2.16 - -- Kitae Kimt <kt920.kim@samsung.com> Wed, 08 Aug 2012 18:59:19 +0900 + -- Kitae Kim <kt920.kim@samsung.com> Wed, 08 Aug 2012 18:59:19 +0900 gstreamer0.10-ffmpeg-emul (0.2.15) unstable; urgency=low diff --git a/ext/ffmpeg/gstffmpeg.c b/ext/ffmpeg/gstffmpeg.c index e5a4dad..8fc1a76 100644 --- a/ext/ffmpeg/gstffmpeg.c +++ b/ext/ffmpeg/gstffmpeg.c @@ -46,7 +46,7 @@ static uint32_t codec_debug_level = 1; int gst_ffmpeg_avcodec_open (AVCodecContext * avctx, AVCodec * codec, - CodecExtraInfo * codecInfo) + CodecExtraInfo * codec_info) { int ret; @@ -54,13 +54,12 @@ gst_ffmpeg_avcodec_open (AVCodecContext * avctx, AVCodec * codec, CODEC_LOG (2, "before avcodec_open! codec_id:%x.\n", codec->id); CODEC_LOG (2, "extradata size:%d\n", avctx->extradata_size); + #ifdef EMUL_CODEC ret = avcodec_open (avctx, codec); #else - ret = emul_codec_init (avctx, codec, codecInfo); + ret = emul_avcodec_init (avctx, codec, codec_info); #endif - CODEC_LOG (2, "after avcodec_open type:%d id:%x, context indext:%d", - avctx->codec_type, avctx->codec_id, codecInfo->contextIndex); if (ret < 0) { CODEC_LOG (1, "failed to open codec_id:%d. ret:%d\n", codec->id, ret); @@ -72,18 +71,16 @@ gst_ffmpeg_avcodec_open (AVCodecContext * avctx, AVCodec * codec, } int -gst_ffmpeg_avcodec_close (AVCodecContext * avctx, CodecExtraInfo * codecInfo) +gst_ffmpeg_avcodec_close (AVCodecContext * avctx, CodecExtraInfo * codec_info) { int ret; g_static_mutex_lock (&gst_avcodec_mutex); - CODEC_LOG (2, "codec close, codec_type:%d, context index:%d\n", - avctx->codec_type, codecInfo->contextIndex); #ifdef EMUL_CODEC ret = avcodec_close (avctx); #else - ret = emul_avcodec_close (avctx, codecInfo); + ret = emul_avcodec_close (avctx, codec_info); #endif if (ret < 0) { @@ -156,7 +153,7 @@ gboolean _shut_up_I_am_probing = FALSE; static gboolean plugin_init (GstPlugin * plugin) { - GST_DEBUG_CATEGORY_INIT (ffmpeg_debug, "ffmpeg", 0, "FFmpeg elements"); + GST_DEBUG_CATEGORY_INIT (ffmpeg_debug, "ffmpeg-emul", 0, "FFmpeg elements"); #ifndef GST_DISABLE_GST_DEBUG av_log_set_callback (gst_ffmpeg_log_callback); diff --git a/ext/ffmpeg/gstffmpegdec.c b/ext/ffmpeg/gstffmpegdec.c index f2f9ca7..6f759ab 100644 --- a/ext/ffmpeg/gstffmpegdec.c +++ b/ext/ffmpeg/gstffmpegdec.c @@ -494,7 +494,7 @@ gst_ffmpegdec_finalize (GObject * object) } #ifndef EMUL_CODEC emul_av_free (&ffmpegdec->codecInfo); - emul_close_codecdev (&ffmpegdec->codecInfo); + emul_close_codec_device (&ffmpegdec->codecInfo); #endif G_OBJECT_CLASS (parent_class)->finalize (object); diff --git a/ext/ffmpeg/gstffmpegdef.c b/ext/ffmpeg/gstffmpegdef.c index aaa7531..1cd07be 100644 --- a/ext/ffmpeg/gstffmpegdef.c +++ b/ext/ffmpeg/gstffmpegdef.c @@ -35,55 +35,34 @@ * */ -#include "gstffmpegdef.h" +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> #include <semaphore.h> +#include <string.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <libavcodec/avcodec.h> +#include <libavformat/avformat.h> +#include "gstffmpegdef.h" #define CODEC_DEV "/dev/codec" #define CODEC_MMAP_SIZE (16 * 1024 * 1024) -#define CODEC_VERSION "1.2" -#define CODEC_PAGE_ALIGN(offset, page_size) \ - ((offset + (page_size - 1)) & ~(page_size - 1)) - -static CodecParam _param; -static ParserBuf _parserBuf; -static int bRegister = 0; -static sem_t hCodecSem; -static uint32_t goffsetArr[MMAP_MAX_SIZE] = { 0, }; +#define CODEC_VERSION "1.3" +static CodecParam codec_param; +static ParserBuf parser_buffer; +static int is_registered = 0; +static sem_t codec_sema; +static int access_mem = 0; static uint32_t codec_debug_level = 1; /* * Helper APIs */ -void -restore_codec_context(AVCodecContext * dstCtx, AVCodecContext * srcCtx) -{ - dstCtx->av_class = srcCtx->av_class; - dstCtx->codec = srcCtx->codec; - dstCtx->extradata = srcCtx->extradata; - dstCtx->draw_horiz_band = srcCtx->draw_horiz_band; - dstCtx->rtp_callback = srcCtx->rtp_callback; - dstCtx->opaque = srcCtx->opaque; - dstCtx->get_buffer = srcCtx->get_buffer; - dstCtx->release_buffer = srcCtx->release_buffer; - dstCtx->stats_out = srcCtx->stats_out; - dstCtx->stats_in = srcCtx->stats_in; - dstCtx->rc_override = srcCtx->rc_override; - dstCtx->rc_eq = srcCtx->rc_eq; - dstCtx->slice_offset = srcCtx->slice_offset; - dstCtx->get_format = srcCtx->get_format; - dstCtx->internal_buffer = srcCtx->internal_buffer; - dstCtx->intra_matrix = srcCtx->intra_matrix; - dstCtx->inter_matrix = srcCtx->inter_matrix; - dstCtx->palctrl = srcCtx->palctrl; - dstCtx->reget_buffer = srcCtx->reget_buffer; - dstCtx->execute = srcCtx->execute; - dstCtx->thread_opaque = srcCtx->thread_opaque; - dstCtx->hwaccel_context = srcCtx->hwaccel_context; - dstCtx->execute2 = srcCtx->execute2; - dstCtx->subtitle_header = srcCtx->subtitle_header; -} - int emul_serialize_rational (const AVRational * elem, uint8_t * buff) { @@ -113,7 +92,7 @@ emul_deserialize_rational (const uint8_t * buff, AVRational * elem) } int -emul_serialize_frame (uint8_t * buff, const AVFrame * elem) +emul_serialize_frame (const AVFrame * elem, uint8_t * buff) { int size = 0; @@ -168,7 +147,7 @@ emul_serialize_frame (uint8_t * buff, const AVFrame * elem) } int -emul_deserialize_frame (AVFrame * elem, const uint8_t * buff) +emul_deserialize_frame (const uint8_t * buff, AVFrame * elem) { int size = 0; @@ -207,28 +186,28 @@ emul_deserialize_frame (AVFrame * elem, const uint8_t * buff) * Open Codec Device */ int -emul_open_codecdev (CodecExtraInfo * pInfo) +emul_open_codec_device (CodecExtraInfo * codec_info) { - int fd = -1; + int fd; CODEC_LOG (2, "Enter\n"); if ((fd = open (CODEC_DEV, O_RDWR)) < 0) { perror ("failed to open codec device"); - CODEC_LOG (2, "failed to open %s, fd:%d, err:%d\n", CODEC_DEV, fd, errno); + CODEC_LOG (1, "failed to open %s, fd:%d, err:%d\n", CODEC_DEV, fd, errno); } else { CODEC_LOG (2, "succeeded to open %s, ret:%d\n", CODEC_DEV, fd); } - pInfo->codecfd = fd; + codec_info->codec_fd = fd; - if (!bRegister) { + if (!is_registered) { CODEC_LOG (2, "Initialize codec semaphore\n"); - if (sem_init (&hCodecSem, 0, 1) < 0) { + if (sem_init (&codec_sema, 0, 1) < 0) { CODEC_LOG (1, "failed to initialize semaphore.\n"); return -1; } CODEC_LOG (2, "register all codecs\n"); - emul_av_register_all (pInfo); -// bRegister++; + emul_av_register_all (codec_info); +// is_registered++; } CODEC_LOG (2, "Leave\n"); @@ -237,711 +216,579 @@ emul_open_codecdev (CodecExtraInfo * pInfo) } /* - * Release mmaping memory space with Codec Device Memory. - * And then, close Codec Device. + * Release mmaping memory space with Codec device memory. + * And then, close codec device. */ void -emul_close_codecdev (CodecExtraInfo * pInfo) +emul_close_codec_device (CodecExtraInfo * codec_info) { - void *pBuf; + void *buf; int fd; int result; - uint32_t mmapSize; CODEC_LOG (2, "Enter\n"); - pBuf = pInfo->pMmapBuf; - if (!pBuf) { + buf = codec_info->buffer; + if (!buf) { CODEC_LOG (1, "address of mapping region is null\n"); return; } - mmapSize = pInfo->mmapSize; - CODEC_LOG (1, "release buffer of mapping region:%p\n", pBuf); -// result = munmap(pBuf, CODEC_MMAP_SIZE); - result = munmap (pBuf, mmapSize); + CODEC_LOG (1, "release buffer of mapping region:%p\n", buf); + result = munmap(buf, CODEC_MMAP_SIZE); if (result != 0) { CODEC_LOG (1, "failure munmap(), ret:%d\n", result); } - fd = pInfo->codecfd; - if (fd < 0) { - CODEC_LOG (1, "Codec device has not been opened yet.\n"); - return; - } - close (fd); - pInfo->codecfd = -1; - - if (bRegister) { - CODEC_LOG (1, "destroy codec semaphore, %d\n", bRegister); - bRegister--; - sem_destroy (&hCodecSem); - } - - CODEC_LOG (2, "Leave, close %s device and fd:%d\n", CODEC_DEV, fd); -} - -void -emul_av_register_all (CodecExtraInfo * pInfo) -{ - int fd; - CODEC_LOG (2, "Enter\n"); - - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); return; } - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AV_REGISTER_ALL; - CODEC_WRITE (fd, &_param, 1); - - CODEC_LOG (2, "Leave\n"); -} - -void -emul_avcodec_alloc_context (CodecExtraInfo * pInfo) -{ - int fd; - int _ctxIndex = -1; - int mmapIndex = -1; - void *pBuf; - uint32_t offset = 0; - - CODEC_LOG (2, "Enter\n"); - - PARAM_INIT (pInfo, CodecExtraInfo); - fd = emul_open_codecdev (pInfo); - if (fd < 0) { - CODEC_LOG (1, "Codec device has not been opened yet.\n"); - return; - } - - sem_wait (&hCodecSem); - - PARAM_INIT (&_param, CodecParam); - -// while (1) { - _param.apiIndex = EMUL_GET_MMAP_INDEX; - _param.ret_args = (uint32_t) & mmapIndex; - CODEC_WRITE (fd, &_param, 1); - if (mmapIndex != -1) { - CODEC_LOG (2, "succedded to get avaiable codec context.\n"); -// break; - } else { - sleep (30); - CODEC_LOG (1, - "there is no avaiable codec context from qemu at the moment.\n"); - sem_post (&hCodecSem); - return; - } -// } - pInfo->mmapIndex = mmapIndex; - - offset = mmapIndex * CODEC_MMAP_SIZE; - CODEC_LOG (2, "mmap index:%d, offset:%d\n", mmapIndex, offset); + CODEC_LOG (2, "close %s device and fd:%d\n", CODEC_DEV, fd); + close (fd); + codec_info->codec_fd = -1; - pBuf = mmap (NULL, CODEC_MMAP_SIZE, PROT_READ | PROT_WRITE, - MAP_SHARED, fd, offset); - if (pBuf) { - CODEC_LOG (1, "mapped region addr of codec device:%p\n", pBuf); - pInfo->pMmapBuf = pBuf; - } else { - CODEC_LOG (1, "failure mmap().\n"); - pInfo->pMmapBuf = NULL; - sem_post (&hCodecSem); - return; + if (is_registered) { + CODEC_LOG (2, "destroy codec semaphore, %d\n", is_registered); + sem_destroy (&codec_sema); + is_registered--; } - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_ALLOC_CONTEXT; - _param.mmapOffset = mmapIndex; - CODEC_WRITE (fd, &_param, 1); - - memcpy (&_ctxIndex, pBuf, sizeof (int)); - pInfo->contextIndex = _ctxIndex; - CODEC_LOG (2, "context index:%d, mmap:%p\n", _ctxIndex, pInfo->pMmapBuf); - - sem_post (&hCodecSem); - CODEC_LOG (2, "Leave\n"); } void -emul_avcodec_alloc_frame (CodecExtraInfo * pInfo) +emul_av_register_all (CodecExtraInfo * codec_info) { int fd; - CODEC_LOG (2, "Enter\n"); - sem_wait (&hCodecSem); - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); return; } - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_ALLOC_FRAME; - _param.ctxIndex = pInfo->contextIndex; - _param.mmapOffset = pInfo->mmapIndex; - CODEC_WRITE (fd, &_param, 1); + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AV_REGISTER_ALL; + CODEC_WRITE (fd, &codec_param, 1); - sem_post (&hCodecSem); CODEC_LOG (2, "Leave\n"); } int -emul_check_version (CodecExtraInfo * pInfo) +emul_check_version (CodecExtraInfo * codec_info) { int fd; char codec_ver[32]; - void *pBuf; + void *buf; CODEC_LOG (2, "Enter\n"); - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); return -1; } - pBuf = pInfo->pMmapBuf; - if (!pBuf) { + buf = codec_info->buffer; + if (!buf) { CODEC_LOG (1, "mapping address is null\n"); return -1; } - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_GET_CODEC_VER; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; - } - CODEC_WRITE (fd, &_param, 1); - - memset (codec_ver, 0x00, 32); - memcpy (codec_ver, (uint8_t *) pBuf, 32); - if (strcmp (codec_ver, CODEC_VERSION) != 0) { - CODEC_LOG (1, "mismatch version between SDK and platform image.\n"); - CODEC_LOG (1, "SDK:%s, Platform:%s\n", codec_ver, CODEC_VERSION); - return -1; + while (1) { + ioctl(fd, EMUL_LOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "lock memory region, %d\n", access_mem); + if (access_mem == 1) { + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_GET_CODEC_VER; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); + + memset (codec_ver, 0x00, sizeof(codec_ver)); + memcpy (codec_ver, (uint8_t *) buf, sizeof(codec_ver)); + if (strcmp (codec_ver, CODEC_VERSION) != 0) { + CODEC_LOG (1, "found conflicts between different versions of" + "sdk tools and this platform.\nSDK:%s, Platform:%s\n", + codec_ver, CODEC_VERSION); + return -1; + } + CODEC_LOG (1, "codec version:%s\n", codec_ver); + break; + } + sleep (1); + CODEC_LOG (1, "cannot access memory region now.\n"); + continue; } - CODEC_LOG (1, "codec version:%s\n", codec_ver); + ioctl(fd, EMUL_UNLOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "unlock memory region, %d\n", access_mem); + CODEC_LOG (2, "Leave\n"); return 0; } int emul_avcodec_open (AVCodecContext * avctx, AVCodec * codec, - CodecExtraInfo * pInfo) + CodecExtraInfo * codec_info) { int fd; - int ret = -1; -// int priv_data_size; + int ret; int size; - int bEncode = codec->encode ? 1 : 0; - void *pBuf; -#ifndef CODEC_COPY_DATA - AVCodecContext tmpCtx; -#endif + int encode = codec->encode ? 1 : 0; + void *buf; CODEC_LOG (2, "Enter\n"); - sem_wait (&hCodecSem); - - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); return -1; } -#if 0 - priv_data_size = codec->priv_data_size; - CODEC_LOG (2, "priv_data size : %d\n", priv_data_size); - avctx->priv_data = (void *) av_mallocz (priv_data_size); -#endif - pBuf = pInfo->pMmapBuf; - if (!pBuf) { + buf = codec_info->buffer; + if (!buf) { CODEC_LOG (1, "mapping address is null\n"); - sem_post (&hCodecSem); - return ret; + return -1; } - if ((ret = emul_check_version (pInfo)) < 0) { -// av_free(avctx->priv_data); - sem_post (&hCodecSem); - return ret; + while (1) { + ioctl(fd, EMUL_LOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "lock memory region, %d\n", access_mem); + + if (access_mem == 1) { + CODEC_LOG (2, "codec type:%d, codec id:%x\n", codec->type, codec->id); + memcpy ((uint8_t *) buf, &avctx->bit_rate, sizeof (int)); + size = sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->bit_rate_tolerance, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->flags, sizeof (int)); + size += sizeof (int); + size += emul_serialize_rational (&avctx->time_base, (uint8_t *) buf + size); + memcpy ((uint8_t *) buf + size, &avctx->width, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->height, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->gop_size, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->pix_fmt, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->sample_rate, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->channels, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->codec_tag, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->block_align, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->rc_strategy, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->strict_std_compliance, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->rc_qsquish, sizeof (float)); + size += sizeof (float); + size += + emul_serialize_rational (&avctx->sample_aspect_ratio, + (uint8_t *) buf + size); + memcpy ((uint8_t *) buf + size, &avctx->qmin, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->qmax, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->pre_me, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->trellis, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &avctx->extradata_size, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &codec->id, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &encode, sizeof (int)); + size += sizeof (int); + if (avctx->extradata_size > 0) { + memcpy ((uint8_t *) buf + size, avctx->extradata, avctx->extradata_size); + } + + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AVCODEC_OPEN; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); + + memcpy (&avctx->pix_fmt, (uint8_t *) buf, sizeof (int)); + size = sizeof (int); + size += emul_deserialize_rational ((uint8_t *) buf + size, &avctx->time_base); + memcpy (&avctx->channels, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->sample_fmt, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->codec_type, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->codec_id, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->coded_width, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->coded_height, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->ticks_per_frame, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->chroma_sample_location, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&ret, (uint8_t *) buf + size, sizeof (int)); + avctx->codec = codec; + break; + } + sleep(1); + CODEC_LOG (1, "cannot access memory region now.\n"); + continue; } + ioctl(fd, EMUL_UNLOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "unlock memory region, %d\n", access_mem); - CODEC_LOG (2, "codec type:%d, codec id:%x\n", avctx->codec_type, - avctx->codec_id); - -#ifndef CODEC_COPY_DATA - memcpy (&tmpCtx, avctx, sizeof (AVCodecContext)); - memcpy (pBuf, avctx, sizeof (AVCodecContext)); - size = sizeof (AVCodecContext); - memcpy ((uint8_t *) pBuf + size, &codec->id, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &bEncode, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, avctx->extradata, avctx->extradata_size); -#else - memcpy ((uint8_t *) pBuf, &avctx->bit_rate, sizeof (int)); - size = sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->bit_rate_tolerance, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->flags, sizeof (int)); - size += sizeof (int); - size += emul_serialize_rational (&avctx->time_base, (uint8_t *) pBuf + size); - memcpy ((uint8_t *) pBuf + size, &avctx->width, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->height, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->gop_size, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->pix_fmt, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->sample_rate, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->channels, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->codec_tag, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->block_align, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->rc_strategy, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->strict_std_compliance, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->rc_qsquish, sizeof (float)); - size += sizeof (float); - size += - emul_serialize_rational (&avctx->sample_aspect_ratio, - (uint8_t *) pBuf + size); - memcpy ((uint8_t *) pBuf + size, &avctx->qmin, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->qmax, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->pre_me, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->trellis, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &avctx->extradata_size, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &codec->id, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &bEncode, sizeof (int)); - size += sizeof (int); - if (avctx->extradata_size > 0) { - memcpy ((uint8_t *) pBuf + size, avctx->extradata, avctx->extradata_size); - } -#endif - CODEC_LOG (2, "ctxIndex:%d, mmapIndex:%d\n", pInfo->contextIndex, - pInfo->mmapIndex); - - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_OPEN; - _param.ctxIndex = pInfo->contextIndex; - _param.mmapOffset = pInfo->mmapIndex; - CODEC_WRITE (fd, &_param, 1); - - CODEC_LOG (2, "after CODEC_WRITE system call\n"); - -#ifndef CODEC_COPY_DATA - size = sizeof (AVCodecContext); - memcpy (avctx, pBuf, size); - restore_codec_context (avctx, &tmpCtx); - avctx->codec = codec; - memcpy (&ret, (uint8_t *) pBuf + size, sizeof (int)); -#else - memcpy (&avctx->pix_fmt, (uint8_t *) pBuf, sizeof (int)); - size = sizeof (int); - size += - emul_deserialize_rational ((uint8_t *) pBuf + size, &avctx->time_base); - memcpy (&avctx->channels, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->sample_fmt, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->codec_type, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->codec_id, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->coded_width, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->coded_height, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->ticks_per_frame, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->chroma_sample_location, (uint8_t *) pBuf + size, - sizeof (int)); - size += sizeof (int); -// memcpy(avctx->priv_data, (uint8_t*)pBuf + size, codec->priv_data_size); -// size += codec->priv_data_size; - memcpy (&ret, (uint8_t *) pBuf + size, sizeof (int)); - avctx->codec = codec; -#endif - CODEC_LOG (2, "after codec type:%d, id:%d\n", avctx->codec_type, - avctx->codec_id); CODEC_LOG (2, "Leave, ret : %d\n", ret); - sem_post (&hCodecSem); - return ret; } int -emul_avcodec_close (AVCodecContext * avctx, CodecExtraInfo * pInfo) +emul_avcodec_close (AVCodecContext * avctx, CodecExtraInfo * codec_info) { int fd; int result; - void *pBuf; + void *buf; CODEC_LOG (2, "Enter\n"); + sem_wait (&codec_sema); - sem_wait (&hCodecSem); - - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return -1; } - pBuf = pInfo->pMmapBuf; - if (!pBuf) { + buf = codec_info->buffer; + if (!buf) { CODEC_LOG (1, "mapping address is null\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return -1; } - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_CLOSE; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; - } - CODEC_WRITE (fd, &_param, 1); + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AVCODEC_CLOSE; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); - memcpy (&result, pBuf, sizeof (int)); - sem_post (&hCodecSem); + memcpy (&result, buf, sizeof (int)); + sem_post (&codec_sema); CODEC_LOG (2, "Leave, ret : %d\n", result); return result; } void -emul_av_free (CodecExtraInfo * pInfo) +emul_av_free (CodecExtraInfo * codec_info) { int fd; CODEC_LOG (2, "Enter\n"); + sem_wait (&codec_sema); - sem_wait (&hCodecSem); - - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return; } - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AV_FREE; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; - } - CODEC_WRITE (fd, &_param, 1); - - sem_post (&hCodecSem); + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AV_FREE; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); + sem_post (&codec_sema); CODEC_LOG (2, "Leave\n"); } void -emul_avcodec_flush_buffers (AVCodecContext * avctx, CodecExtraInfo * pInfo) +emul_avcodec_flush_buffers (AVCodecContext * avctx, CodecExtraInfo * codec_info) { int fd; CODEC_LOG (2, "Enter\n"); - sem_wait (&hCodecSem); + sem_wait (&codec_sema); - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return; } - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_FLUSH_BUFFERS; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; - } - CODEC_WRITE (fd, &_param, 1); - - sem_post (&hCodecSem); + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AVCODEC_FLUSH_BUFFERS; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); + sem_post (&codec_sema); CODEC_LOG (2, "Leave\n"); } int emul_avcodec_decode_video (AVCodecContext * ctx, AVFrame * frame, - int *got_picture_ptr, AVPacket * packet, CodecExtraInfo * pInfo) + int *got_picture_ptr, AVPacket * packet, CodecExtraInfo * codec_info) { int fd; int result; int size = 0; - void *pBuf; -#ifndef CODEC_COPY_DATA - AVCodecContext tmpCtx; -#endif + void *buf; CODEC_LOG (2, "Enter\n"); + sem_wait (&codec_sema); - sem_wait (&hCodecSem); - - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return -1; } - pBuf = pInfo->pMmapBuf; - if (!pBuf) { + buf = codec_info->buffer; + if (!buf) { CODEC_LOG (1, "mapping address is null\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return -1; } - memcpy ((uint8_t *) pBuf, &ctx->reordered_opaque, sizeof (int64_t)); - size = sizeof (int64_t); - memcpy ((uint8_t *) pBuf + size, &ctx->skip_frame, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &packet->size, sizeof (int)); - size += sizeof (int); - - CODEC_LOG (2, "decoding video. in_buf size: %d\n", packet->size); - if (packet->size > 0) { - memcpy ((uint8_t *) pBuf + size, packet->data, packet->size); - } - - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_DECODE_VIDEO; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; + CODEC_LOG (2, "codec close, codec_type:%d, context index:%d\n", + ctx->codec_type, codec_info->ctx_idx); + while (1) { + ioctl(fd, EMUL_LOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "lock memory region, %d\n", access_mem); + + if (access_mem == 1) { + memcpy ((uint8_t *) buf, &ctx->reordered_opaque, sizeof (int64_t)); + size = sizeof (int64_t); + memcpy ((uint8_t *) buf + size, &ctx->skip_frame, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &packet->size, sizeof (int)); + size += sizeof (int); + + CODEC_LOG (2, "decoding video. in_buf size: %d\n", packet->size); + if (packet->size > 0) { + memcpy ((uint8_t *) buf + size, packet->data, packet->size); + } + + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AVCODEC_DECODE_VIDEO; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); + + memcpy (&ctx->pix_fmt, (uint8_t *) buf, sizeof (int)); + size = sizeof (int); + size += emul_deserialize_rational ((uint8_t *) buf + size, &ctx->time_base); + memcpy (&ctx->width, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&ctx->height, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&ctx->has_b_frames, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&ctx->frame_number, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + size += + emul_deserialize_rational ((uint8_t *) buf + size, + &ctx->sample_aspect_ratio); + memcpy (&ctx->internal_buffer_count, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&ctx->profile, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&ctx->level, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + size += emul_deserialize_frame ((uint8_t *) buf + size, frame); + memcpy (got_picture_ptr, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&result, (uint8_t *) buf + size, sizeof (int)); + ctx->coded_frame = frame; + ctx->pkt = packet; + break; + } + sleep (1); + CODEC_LOG (1, "cannot access memory region now.\n"); + continue; } - CODEC_WRITE (fd, &_param, 1); - -#ifndef CODEC_COPY_DATA - size = sizeof (AVCodecContext); - memcpy (&tmpCtx, ctx, size); - memcpy (ctx, pBuf, size); - restore_codec_context (ctx, &tmpCtx); -#else - memcpy (&ctx->pix_fmt, (uint8_t *) pBuf, sizeof (int)); - size = sizeof (int); - size += emul_deserialize_rational ((uint8_t *) pBuf + size, &ctx->time_base); - memcpy (&ctx->width, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&ctx->height, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&ctx->has_b_frames, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&ctx->frame_number, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - size += - emul_deserialize_rational ((uint8_t *) pBuf + size, - &ctx->sample_aspect_ratio); - memcpy (&ctx->internal_buffer_count, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&ctx->profile, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&ctx->level, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); -#endif - size += emul_deserialize_frame (frame, (uint8_t *) pBuf + size); - - memcpy (got_picture_ptr, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&result, (uint8_t *) pBuf + size, sizeof (int)); - ctx->coded_frame = frame; - ctx->pkt = packet; + ioctl(fd, EMUL_UNLOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "unlock memory region, %d\n", access_mem); + sem_post (&codec_sema); CODEC_LOG (2, "Leave, ret:%d\n", result); - sem_post (&hCodecSem); - return result; } int -emul_avcodec_encode_video (AVCodecContext * ctx, uint8_t * buf, +emul_avcodec_encode_video (AVCodecContext * ctx, uint8_t * out_buf, int buf_size, const AVFrame * pict, - uint8_t * pict_buf, uint32_t pict_buf_size, CodecExtraInfo * pInfo) + uint8_t * pict_buf, uint32_t pict_buf_size, CodecExtraInfo * codec_info) { - void *pBuf; + void *buf; int fd; -// int inputSize; int flush_buf; int size; int result = -1; CODEC_LOG (2, "Enter\n"); - sem_wait (&hCodecSem); + sem_wait (&codec_sema); + + if (pict->pts <= codec_info->last_pts) { + CODEC_LOG (2, "invalid timestamp last_pts:%lld, cur_pts:%lld.\n", + codec_info->last_pts, pict->pts); + sem_post (&codec_sema); + return -1; + } - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return -1; } -// ctx->coded_frame = avcodec_alloc_frame(); - pBuf = pInfo->pMmapBuf; - if (!pBuf) { + buf = codec_info->buffer; + if (!buf) { CODEC_LOG (1, "mapping address is null\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return -1; } CODEC_LOG (2, "output buffer size :%d\n", buf_size); CODEC_LOG (2, "before encoding video! pts:%lld\n", pict->pts); - size = 0; - if (pict && pict_buf) { - flush_buf = 0; - memcpy ((uint8_t *) pBuf + size, &flush_buf, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, &buf_size, sizeof (int)); - size += sizeof (int); - size += emul_serialize_frame ((uint8_t *) pBuf + size, pict); - memcpy ((uint8_t *) pBuf + size, pict_buf, pict_buf_size); - } else { - flush_buf = 1; - memcpy ((uint8_t *) pBuf + size, &flush_buf, sizeof (int)); - } - - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_ENCODE_VIDEO; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; - } - CODEC_WRITE (fd, &_param, 1); - - CODEC_LOG (2, "after CODEC_WRITE!!\n"); - - if (pict && pict_buf) { - memcpy (buf, (uint8_t *) pBuf, buf_size); - memcpy (&result, (uint8_t *) pBuf + buf_size, sizeof (int)); - } else { - memcpy (&result, pBuf, sizeof (int)); + while (1) { + ioctl(fd, EMUL_LOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "lock memory region, %d\n", access_mem); + if (access_mem == 1) { + size = 0; + if (pict && pict_buf) { + flush_buf = 0; + memcpy ((uint8_t *) buf + size, &flush_buf, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, &buf_size, sizeof (int)); + size += sizeof (int); + size += emul_serialize_frame (pict, (uint8_t *) buf + size); + memcpy ((uint8_t *) buf + size, pict_buf, pict_buf_size); + } else { + flush_buf = 1; + memcpy ((uint8_t *) buf + size, &flush_buf, sizeof (int)); + } + + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AVCODEC_ENCODE_VIDEO; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); + + if (pict && pict_buf) { + memcpy (out_buf, (uint8_t *) buf, buf_size); + memcpy (&result, (uint8_t *) buf + buf_size, sizeof (int)); + } else { + memcpy (&result, buf, sizeof (int)); + } + ctx->coded_frame = pict; + break; + } + sleep (1); + CODEC_LOG (1, "cannot access memory region now.\n"); + continue; } + ioctl(fd, EMUL_UNLOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "unlock memory region, %d\n", access_mem); CODEC_LOG (2, "after encoding video, ret :%d\n", result); - sem_post (&hCodecSem); + sem_post (&codec_sema); return result; } int emul_avcodec_decode_audio (AVCodecContext * avctx, int16_t * samples, - int *frame_size_ptr, AVPacket * packet, CodecExtraInfo * pInfo) + int *frame_size_ptr, AVPacket * packet, CodecExtraInfo * codec_info) { int fd; int result; int size; - void *pBuf; -#ifndef CODEC_COPY_DATA - AVCodecContext tmpCtx; -#endif + void *buf; CODEC_LOG (2, "Enter\n"); - sem_wait (&hCodecSem); + sem_wait (&codec_sema); - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return -1; } - pBuf = pInfo->pMmapBuf; - if (!pBuf) { + buf = codec_info->buffer; + if (!buf) { CODEC_LOG (1, "mapping address is null\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return -1; } - memcpy ((uint8_t *) pBuf, &packet->size, sizeof (int)); - size = sizeof (int); - CODEC_LOG (2, "input buffer size :%d\n", packet->size); - if (packet->size > 0) { - memcpy ((uint8_t *) pBuf + size, packet->data, packet->size); - } - - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_DECODE_AUDIO; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; + while (1) { + ioctl(fd, EMUL_LOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "lock memory region, %d\n", access_mem); + + if (access_mem == 1) { + memcpy ((uint8_t *) buf, &packet->size, sizeof (int)); + size = sizeof (int); + CODEC_LOG (2, "input buffer size :%d\n", packet->size); + if (packet->size > 0) { + memcpy ((uint8_t *) buf + size, packet->data, packet->size); + } + + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AVCODEC_DECODE_AUDIO; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); + + memcpy (&avctx->bit_rate, (uint8_t *) buf, sizeof (int)); + size = sizeof (int); + memcpy (&avctx->sample_rate, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->channels, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->sub_id, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->frame_size, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&avctx->frame_number, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (samples, (uint8_t *) buf + size, AVCODEC_MAX_AUDIO_FRAME_SIZE); + size += AVCODEC_MAX_AUDIO_FRAME_SIZE; + memcpy (frame_size_ptr, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&result, (uint8_t *) buf + size, sizeof (int)); + avctx->pkt = packet; + break; + } + sleep (1); + CODEC_LOG (1, "cannot access memory region now.\n"); + continue; } - CODEC_WRITE (fd, &_param, 1); - -#ifndef CODEC_COPY_DATA - memcpy (&tmpCtx, avctx, sizeof (AVCodecContext)); - memcpy (avctx, (uint8_t *) pBuf, sizeof (AVCodecContext)); - size = sizeof (AVCodecContext); - restore_codec_context (avctx, &tmpCtx); -#else - memcpy (&avctx->bit_rate, (uint8_t *) pBuf, sizeof (int)); - size = sizeof (int); - memcpy (&avctx->sub_id, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->frame_size, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->frame_number, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); -#endif - memcpy (samples, (uint8_t *) pBuf + size, AVCODEC_MAX_AUDIO_FRAME_SIZE); - size += AVCODEC_MAX_AUDIO_FRAME_SIZE; - memcpy (frame_size_ptr, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&result, (uint8_t *) pBuf + size, sizeof (int)); - avctx->pkt = packet; + ioctl(fd, EMUL_UNLOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "unlock memory region, %d\n", access_mem); CODEC_LOG (2, "frame_size_ptr:%d\n", *frame_size_ptr); CODEC_LOG (2, "after decoding audio:%d\n", result); - sem_post (&hCodecSem); + sem_post (&codec_sema); CODEC_LOG (2, "Leave, ret:%d\n", result); @@ -949,77 +796,78 @@ emul_avcodec_decode_audio (AVCodecContext * avctx, int16_t * samples, } void -emul_av_picture_copy (AVPicture * dst, uint32_t fsize, CodecExtraInfo * pInfo) +emul_av_picture_copy (AVPicture * dst, uint32_t fsize, CodecExtraInfo * codec_info) { - void *pBuf; + void *buf; int fd; CODEC_LOG (2, "Enter\n"); - sem_wait (&hCodecSem); + sem_wait (&codec_sema); - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return; } - pBuf = pInfo->pMmapBuf; - if (!pBuf) { + buf = codec_info->buffer; + if (!buf) { CODEC_LOG (1, "mapping address is null\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return; } - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AV_PICTURE_COPY; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; + while (1) { + ioctl(fd, EMUL_LOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "lock memory region, %d\n", access_mem); + + if (access_mem == 1) { + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AV_PICTURE_COPY; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); + + CODEC_LOG (2, "fsize:%d\n", fsize); + /* Copy output buffer of decoded frame from QEMU */ + memcpy (dst->data[0], buf, fsize); + break; + } + sleep (1); + CODEC_LOG (1, "cannot access memory region now.\n"); + continue; } - CODEC_WRITE (fd, &_param, 1); - - CODEC_LOG (2, "after CODEC_WRITE system call\n"); - CODEC_LOG (2, "fsize:%d\n", fsize); - - /* Copy output buffer of decoded frame from QEMU */ - memcpy (dst->data[0], pBuf, fsize); + ioctl(fd, EMUL_UNLOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "unlock memory region, %d\n", access_mem); - sem_post (&hCodecSem); + sem_post (&codec_sema); CODEC_LOG (2, "Leave\n"); } void -emul_av_parser_init (CodecExtraInfo * pInfo) +emul_av_parser_init (CodecExtraInfo * codec_info) { int fd; CODEC_LOG (2, "Enter\n"); - sem_wait (&hCodecSem); + sem_wait (&codec_sema); - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return; } - PARAM_INIT (&_param, CodecParam); - PARAM_INIT (&_parserBuf, ParserBuf); - _param.apiIndex = EMUL_AV_PARSER_INIT; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; - } - CODEC_WRITE (fd, &_param, 1); + CODEC_PARAM_INIT (&codec_param, CodecParam); + CODEC_PARAM_INIT (&parser_buffer, ParserBuf); + codec_param.api_index = EMUL_AV_PARSER_INIT; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); - sem_post (&hCodecSem); + sem_post (&codec_sema); CODEC_LOG (2, "Leave\n"); } @@ -1027,316 +875,282 @@ emul_av_parser_init (CodecExtraInfo * pInfo) int emul_av_parser_parse (AVCodecParserContext * s, AVCodecContext * ctx, uint8_t ** outbuf, int *outbuf_size, - const uint8_t * buf, int buf_size, - int64_t pts, int64_t dts, int64_t pos, CodecExtraInfo * pInfo) + const uint8_t * inbuf, int inbuf_size, + int64_t pts, int64_t dts, int64_t pos, CodecExtraInfo * codec_info) { - void *pBuf; + void *buf; int fd; int size; int out_size; int result = -1; CODEC_LOG (2, "Enter\n"); - sem_wait (&hCodecSem); + sem_wait (&codec_sema); - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return -1; } - pBuf = pInfo->pMmapBuf; - if (!pBuf) { + buf = codec_info->buffer; + if (!buf) { CODEC_LOG (1, "mapping address is null\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return -1; } - CODEC_LOG (2, "input buffer size :%d\n", buf_size); - memcpy ((uint8_t *) pBuf, &s->pts, sizeof (int64_t)); - size = sizeof (int64_t); - memcpy ((uint8_t *) pBuf + size, &s->dts, sizeof (int64_t)); - size += sizeof (int64_t); - memcpy ((uint8_t *) pBuf + size, &s->pos, sizeof (int64_t)); - size += sizeof (int64_t); - - memcpy ((uint8_t *) pBuf + size, &pts, sizeof (int64_t)); - size += sizeof (int64_t); - memcpy ((uint8_t *) pBuf + size, &dts, sizeof (int64_t)); - size += sizeof (int64_t); - memcpy ((uint8_t *) pBuf + size, &pos, sizeof (int64_t)); - size += sizeof (int64_t); - memcpy ((uint8_t *) pBuf + size, &buf_size, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) pBuf + size, buf, buf_size); - - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AV_PARSER_PARSE; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; - } - CODEC_WRITE (fd, &_param, 1); - - memcpy (&s->pts, pBuf, sizeof (int64_t)); - size = sizeof (int64_t); - memcpy (outbuf_size, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - memcpy (&result, (uint8_t *) pBuf + size, sizeof (int)); - size += sizeof (int); - - out_size = *outbuf_size; - - CODEC_LOG (2, "input :%d, output:%d, res:%d\n", buf_size, out_size, result); - if (*outbuf_size > 0) { - CODEC_LOG (2, "allocate output buffer :%d\n", out_size); - _parserBuf.buf = av_malloc (out_size + FF_INPUT_BUFFER_PADDING_SIZE); - _parserBuf.size = out_size; - memcpy (_parserBuf.buf, (uint8_t *) pBuf + size, out_size); - *outbuf = _parserBuf.buf; - CODEC_LOG (2, "parser buffer:%p, outbuf:%p\n", _parserBuf.buf, *outbuf); - } else { - if (_parserBuf.buf && _parserBuf.size > 0) { - av_free (_parserBuf.buf); - _parserBuf.buf = NULL; - CODEC_LOG (2, "free the previous buffer\n"); + while (1) { + ioctl(fd, EMUL_LOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "lock memory region, %d\n", access_mem); + + if (access_mem == 1) { + CODEC_LOG (2, "input buffer size :%d\n", inbuf_size); + memcpy ((uint8_t *) buf, &s->pts, sizeof (int64_t)); + size = sizeof (int64_t); + memcpy ((uint8_t *) buf + size, &s->dts, sizeof (int64_t)); + size += sizeof (int64_t); + memcpy ((uint8_t *) buf + size, &s->pos, sizeof (int64_t)); + size += sizeof (int64_t); + memcpy ((uint8_t *) buf + size, &pts, sizeof (int64_t)); + size += sizeof (int64_t); + memcpy ((uint8_t *) buf + size, &dts, sizeof (int64_t)); + size += sizeof (int64_t); + memcpy ((uint8_t *) buf + size, &pos, sizeof (int64_t)); + size += sizeof (int64_t); + memcpy ((uint8_t *) buf + size, &inbuf_size, sizeof (int)); + size += sizeof (int); + memcpy ((uint8_t *) buf + size, inbuf, inbuf_size); + + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AV_PARSER_PARSE; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); + + memcpy (&s->pts, buf, sizeof (int64_t)); + size = sizeof (int64_t); + memcpy (outbuf_size, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + memcpy (&result, (uint8_t *) buf + size, sizeof (int)); + size += sizeof (int); + + out_size = *outbuf_size; + + CODEC_LOG (2, "input :%d, output:%d, res:%d\n", + inbuf_size, out_size, result); + if (*outbuf_size > 0) { + CODEC_LOG (2, "allocate output buffer :%d\n", out_size); + parser_buffer.buf = av_malloc (out_size + FF_INPUT_BUFFER_PADDING_SIZE); + parser_buffer.size = out_size; + memcpy (parser_buffer.buf, (uint8_t *) buf + size, out_size); + *outbuf = parser_buffer.buf; + CODEC_LOG (2, "parser buffer:%p, outbuf:%p\n", parser_buffer.buf, *outbuf); + } else { + if (parser_buffer.buf && parser_buffer.size > 0) { + av_free (parser_buffer.buf); + parser_buffer.buf = NULL; + CODEC_LOG (2, "free the previous buffer\n"); + } + *outbuf = NULL; + parser_buffer.size = -1; + } + CODEC_LOG (2, "outbuf:%p\n", *outbuf); + break; } - *outbuf = NULL; - _parserBuf.size = -1; + sleep (1); + CODEC_LOG (1, "cannot access memory region now.\n"); + continue; } - CODEC_LOG (2, "outbuf:%p\n", *outbuf); - sem_post (&hCodecSem); + ioctl(fd, EMUL_UNLOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "unlock memory region, %d\n", access_mem); + + sem_post (&codec_sema); CODEC_LOG (2, "Leave\n"); return result; } void -emul_av_parser_close (CodecExtraInfo * pInfo) +emul_av_parser_close (CodecExtraInfo * codec_info) { int fd; CODEC_LOG (2, "Enter\n"); - sem_wait (&hCodecSem); + sem_wait (&codec_sema); - fd = pInfo->codecfd; + fd = codec_info->codec_fd; if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); - sem_post (&hCodecSem); + sem_post (&codec_sema); return; } - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AV_PARSER_CLOSE; - _param.ctxIndex = pInfo->contextIndex; -// _param.mmapOffset = pInfo->mmapIndex; - if (pInfo->mmapIndex == 0) { - _param.mmapOffset = 0; - } else { - _param.mmapOffset = goffsetArr[pInfo->mmapIndex - 1]; - } - CODEC_WRITE (fd, &_param, 1); + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AV_PARSER_CLOSE; + codec_param.ctx_index = codec_info->ctx_idx; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); - sem_post (&hCodecSem); + sem_post (&codec_sema); CODEC_LOG (2, "Leave\n"); } int -emul_codec_init (AVCodecContext * avctx, AVCodec * codec, - CodecExtraInfo * codecInfo) +emul_codec_mmap (CodecExtraInfo * codec_info) { - int fd, ctxIndex = -1, mmapIndex = -1; - uint32_t mmapSize = 0; - uint32_t *offsetArr; - void *buf = NULL; - uint32_t offset = 0; - int size, ret = 0; - int encode = codec->encode ? 1 : 0; + int fd; + int ret = 0; - CODEC_LOG (2, "enter, %s\n", __func__); + fd = codec_info->codec_fd; - PARAM_INIT (codecInfo, CodecExtraInfo); - if ((fd = emul_open_codecdev (codecInfo)) < 0) { +#if 0 + if (codec_info->codec_type == AVMEDIA_TYPE_VIDEO) { + CODEC_LOG (1, "mapped memory region for video type\n"); + codec_info->video_buf = + mmap (NULL, CODEC_MMAP_SIZE, + PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + codec_info->mmap_offset = 0; + if (!codec_info->video_buf) { + perror ("failed to mmap."); + codec_info->video_buf = NULL; + ret = -1; + } + } else if (codec_info->codec_type == AVMEDIA_TYPE_AUDIO) { + CODEC_LOG (1, "mapped memory region for audio type\n"); + codec_info->audio_buf = + mmap (NULL, CODEC_MMAP_SIZE, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, CODEC_MMAP_SIZE); + codec_info->mmap_offset = CODEC_MMAP_SIZE; + if (!codec_info->audio_buf) { + perror ("failed to mmap."); + codec_info->audio_buf = NULL; + ret = -1; + } + } else { + CODEC_LOG (1, "unknown media type\n"); + ret = -1; + } +#endif + if (codec_info->codec_type == AVMEDIA_TYPE_VIDEO) { + CODEC_LOG (2, "mapped memory region for video type\n"); + codec_info->mmap_offset = 0; + } else if (codec_info->codec_type == AVMEDIA_TYPE_AUDIO) { + CODEC_LOG (2, "mapped memory region for audio type\n"); + codec_info->mmap_offset = CODEC_MMAP_SIZE; + } else { + CODEC_LOG (1, "unknown media type\n"); + return -1; + } + codec_info->buffer = + mmap (NULL, CODEC_MMAP_SIZE, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, codec_info->mmap_offset); + + if (!codec_info->buffer) { + perror ("failed to mmap."); + codec_info->buffer = NULL; + ret = -1; + } else { + CODEC_LOG (1, "succeeded to mmap: %p\n", codec_info->buffer); + } + + return ret; +} + +int +emul_avcodec_alloc_context (CodecExtraInfo * codec_info) +{ + int fd, ctx_index; + int ret = 0; + void *buf; + + CODEC_LOG (2, "Enter\n"); + fd = codec_info->codec_fd; + if (fd < 0) { CODEC_LOG (1, "Codec device has not been opened yet.\n"); return -1; } - sem_wait (&hCodecSem); + buf = codec_info->buffer; + if (!buf) { + CODEC_LOG (1, "mapping address is null\n"); + return -1; + } - /* Get an index of mmap from kernel. */ - PARAM_INIT (&_param, CodecParam); while (1) { - _param.apiIndex = EMUL_GET_MMAP_INDEX; - _param.ret_args = (uint32_t) & mmapIndex; - CODEC_WRITE (fd, &_param, 1); - - if (mmapIndex != -1) { - CODEC_LOG (1, "succedded to get avaiable codec context. index:%d\n", - mmapIndex); + ioctl(fd, EMUL_LOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "lock memory region, %d\n", access_mem); + + if (access_mem == 1) { + /* Allocate Codec Context */ + CODEC_PARAM_INIT (&codec_param, CodecParam); + codec_param.api_index = EMUL_AVCODEC_ALLOC_CONTEXT; + codec_param.mmap_offset = codec_info->mmap_offset; + CODEC_WRITE (fd, &codec_param, 1); + + memcpy (&ctx_index, buf, sizeof (int)); + codec_info->ctx_idx = ctx_index; + CODEC_LOG (2, "context index:%d\n", ctx_index); break; - } else { - sleep (1); - CODEC_LOG (1, - "there is no avaiable codec context from qemu at the moment.\n"); } + sleep (1); + CODEC_LOG (1, "cannot access memory region now.\n"); + continue; } + ioctl(fd, EMUL_UNLOCK_MEM_REGION, &access_mem); + CODEC_LOG (2, "unlock memory region, %d\n", access_mem); - /* Set the mmap index and offset. */ - offsetArr = goffsetArr; - codecInfo->mmapIndex = mmapIndex; - if (codec->type == AVMEDIA_TYPE_VIDEO) { - CODEC_LOG (2, "Video Codec.\n"); - if (codec->decode) { - mmapSize = (avctx->width * avctx->height * 3) / 2; - } else { - mmapSize = (avctx->width * avctx->height * 6) + FF_MIN_BUFFER_SIZE + 4; - } - } else { - CODEC_LOG (2, "Audio Codec.\n"); - mmapSize = AVCODEC_MAX_AUDIO_FRAME_SIZE + 24; - } - mmapSize = CODEC_PAGE_ALIGN (mmapSize, getpagesize ()); - codecInfo->mmapSize = mmapSize; + return ret; +} - if (mmapIndex == 0) { - offsetArr[0] = mmapSize; - offset = 0; - } else { - offsetArr[mmapIndex] = offsetArr[mmapIndex - 1] + mmapSize; - offset = offsetArr[mmapIndex - 1]; +int +emul_avcodec_init (AVCodecContext * avctx, AVCodec * codec, + CodecExtraInfo * codec_info) +{ + int ret = 0; + + CODEC_LOG (2, "enter, %s\n", __func__); + + CODEC_PARAM_INIT (codec_info, CodecExtraInfo); + if (emul_open_codec_device (codec_info) < 0) { + CODEC_LOG (1, "Codec device has not been opened yet.\n"); + return -1; } - CODEC_LOG (2, "offsetArr[%d]:%d, mmapSize:%d, offset:%d\n", mmapIndex, - offsetArr[mmapIndex], mmapSize, offset); - - /* Get memory mapped adrress through mmap(). */ - buf = mmap (NULL, mmapSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); - if (buf) { - CODEC_LOG (1, "mapped region addr of codec device:%p\n", buf); - codecInfo->pMmapBuf = buf; - } else { - perror ("failed to mmap."); - codecInfo->pMmapBuf = NULL; - sem_post (&hCodecSem); + + sem_wait (&codec_sema); + + /* Get mapped memory address by mmap(). */ + codec_info->codec_type = codec->type; + if (emul_codec_mmap(codec_info) < 0) { + CODEC_LOG (1, "failure mmap(), %d\n", codec->type); + sem_post (&codec_sema); return -1; } - /* Allocate Codec Context */ - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_ALLOC_CONTEXT; - _param.mmapOffset = offset; - CODEC_WRITE (fd, &_param, 1); - - memcpy (&ctxIndex, buf, sizeof (int)); - codecInfo->contextIndex = ctxIndex; - CODEC_LOG (2, "context index:%d, mmap:%p\n", ctxIndex, codecInfo->pMmapBuf); - - /* Allocate Frame */ - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_ALLOC_FRAME; - _param.ctxIndex = codecInfo->contextIndex; - _param.mmapOffset = offset; - CODEC_WRITE (fd, &_param, 1); - - /* Check Codec Module Version between SDK and PLATFORM. */ - if (!bRegister) { - if ((ret = emul_check_version (codecInfo)) < 0) { - sem_post (&hCodecSem); - return ret; + /* Check codec module version between SDK and PLATFORM. */ + if (!is_registered) { + if (emul_check_version (codec_info) < 0) { + sem_post (&codec_sema); + return -1; } - bRegister++; + is_registered++; } - /* Call AVCODEC_OPEN */ - CODEC_LOG (2, "codec type:%d, codec id:%x\n", codec->type, codec->id); - memcpy ((uint8_t *) buf, &avctx->bit_rate, sizeof (int)); - size = sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->bit_rate_tolerance, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->flags, sizeof (int)); - size += sizeof (int); - size += emul_serialize_rational (&avctx->time_base, (uint8_t *) buf + size); - memcpy ((uint8_t *) buf + size, &avctx->width, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->height, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->gop_size, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->pix_fmt, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->sample_rate, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->channels, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->codec_tag, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->block_align, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->rc_strategy, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->strict_std_compliance, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->rc_qsquish, sizeof (float)); - size += sizeof (float); - size += - emul_serialize_rational (&avctx->sample_aspect_ratio, - (uint8_t *) buf + size); - memcpy ((uint8_t *) buf + size, &avctx->qmin, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->qmax, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->pre_me, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->trellis, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &avctx->extradata_size, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &codec->id, sizeof (int)); - size += sizeof (int); - memcpy ((uint8_t *) buf + size, &encode, sizeof (int)); - size += sizeof (int); - if (avctx->extradata_size > 0) { - memcpy ((uint8_t *) buf + size, avctx->extradata, avctx->extradata_size); + if (emul_avcodec_alloc_context (codec_info) < 0) { + CODEC_LOG (1, "failed to allocate codec context\n"); + sem_post (&codec_sema); + return -1; } - PARAM_INIT (&_param, CodecParam); - _param.apiIndex = EMUL_AVCODEC_OPEN; - _param.ctxIndex = codecInfo->contextIndex; - _param.mmapOffset = offset; - CODEC_WRITE (fd, &_param, 1); - - memcpy (&avctx->pix_fmt, (uint8_t *) buf, sizeof (int)); - size = sizeof (int); - size += emul_deserialize_rational ((uint8_t *) buf + size, &avctx->time_base); - memcpy (&avctx->channels, (uint8_t *) buf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->sample_fmt, (uint8_t *) buf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->codec_type, (uint8_t *) buf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->codec_id, (uint8_t *) buf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->coded_width, (uint8_t *) buf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->coded_height, (uint8_t *) buf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->ticks_per_frame, (uint8_t *) buf + size, sizeof (int)); - size += sizeof (int); - memcpy (&avctx->chroma_sample_location, (uint8_t *) buf + size, sizeof (int)); - size += sizeof (int); - memcpy (&ret, (uint8_t *) buf + size, sizeof (int)); - avctx->codec = codec; - + ret = emul_avcodec_open (avctx, codec, codec_info); CODEC_LOG (2, "after codec type:%d, id:%d\n", avctx->codec_type, avctx->codec_id); - CODEC_LOG (2, "Leave, ret:%d, %s\n", ret, __func__); - sem_post (&hCodecSem); + CODEC_LOG (2, "Leave\n"); + sem_post (&codec_sema); return ret; } diff --git a/ext/ffmpeg/gstffmpegdef.h b/ext/ffmpeg/gstffmpegdef.h index 2fcc2cd..48395ce 100644 --- a/ext/ffmpeg/gstffmpegdef.h +++ b/ext/ffmpeg/gstffmpegdef.h @@ -35,130 +35,121 @@ * */ -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <fcntl.h> -#include <unistd.h> -#include <string.h> -#include <sys/mman.h> -#include <sys/stat.h> - -/* FFMPEG API */ #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> /* If EMUL_CODEC defines in this module, emulator does not use host codec. */ -// #define EMUL_CODEC - -/* Determine a way to copy codec data from guest to host. - * #ifndef CODEC_COPY_DATA : Copy AVCodecContext, AVCodec, AVFrame struct - * #ifdef CODEC_COPY_DATA : Copy some fields of those structures. - */ -#define CODEC_COPY_DATA -#define MMAP_MAX_SIZE 20 +/* #define EMUL_CODEC */ #define CODEC_LOG(level, fmt, ...) \ - do { \ - if (codec_debug_level >= (level)) \ - printf("[gst-ffmpeg-emul][%d] " fmt, __LINE__, ##__VA_ARGS__); \ - } while (0) - -#define CODEC_WRITE(fd, _codec, size) \ - if (write(fd, _codec, size) < 0) { \ - printf("[gst-ffmpeg-emul][%d] failed to copy data to qemu\n", __LINE__); \ - } - -#define PARAM_INIT(varCodec, defCodec) \ - memset(varCodec, 0, sizeof(defCodec)) - -typedef struct _CodecExtraInfo { - int contextIndex; - int mmapIndex; - void *pMmapBuf; - int codecfd; - uint32_t mmapSize; + do { \ + if (codec_debug_level >= (level)) \ + printf("[gst-ffmpeg-emul][%d] " fmt, __LINE__, ##__VA_ARGS__); \ + } while (0) + +#define CODEC_WRITE(fd, codec, size) \ + if (write(fd, codec, size) < 0) { \ + printf("[gst-ffmpeg-emul][%d] failed to copy data to qemu\n", __LINE__); \ + } + +#define CODEC_PARAM_INIT(var_codec, def_codec) \ + memset(var_codec, 0, sizeof(def_codec)) + +typedef struct _CodecExtraInfo +{ + int codec_fd; + int codec_type; + void *buffer; + uint32_t ctx_idx; + uint32_t mmap_offset; + int64_t last_pts; } CodecExtraInfo; -typedef struct _CodecParam { - uint32_t apiIndex; - uint32_t ctxIndex; - uint32_t mmapOffset; - uint32_t ret_args; +typedef struct _CodecParam +{ + uint32_t api_index; + uint32_t ctx_index; + uint32_t mmap_offset; } CodecParam; -typedef struct _ParserBuf { - uint8_t *buf; - uint32_t size; +typedef struct _ParserBuf +{ + uint8_t *buf; + uint32_t size; } ParserBuf; -enum { - EMUL_AV_REGISTER_ALL = 1, - EMUL_AVCODEC_ALLOC_CONTEXT, - EMUL_AVCODEC_ALLOC_FRAME, - EMUL_AVCODEC_OPEN, - EMUL_AVCODEC_CLOSE, - EMUL_AV_FREE, - EMUL_AVCODEC_FLUSH_BUFFERS, - EMUL_AVCODEC_DECODE_VIDEO, - EMUL_AVCODEC_ENCODE_VIDEO, - EMUL_AVCODEC_DECODE_AUDIO, - EMUL_AVCODEC_ENCODE_AUDIO, - EMUL_AV_PICTURE_COPY, - EMUL_AV_PARSER_INIT, - EMUL_AV_PARSER_PARSE, - EMUL_AV_PARSER_CLOSE, - EMUL_GET_MMAP_INDEX, - EMUL_GET_CODEC_VER = 50, +enum +{ + EMUL_AV_REGISTER_ALL = 1, + EMUL_AVCODEC_ALLOC_CONTEXT, + EMUL_AVCODEC_OPEN, + EMUL_AVCODEC_CLOSE, + EMUL_AV_FREE, + EMUL_AVCODEC_FLUSH_BUFFERS, + EMUL_AVCODEC_DECODE_VIDEO, + EMUL_AVCODEC_ENCODE_VIDEO, + EMUL_AVCODEC_DECODE_AUDIO, + EMUL_AVCODEC_ENCODE_AUDIO, + EMUL_AV_PICTURE_COPY, + EMUL_AV_PARSER_INIT, + EMUL_AV_PARSER_PARSE, + EMUL_AV_PARSER_CLOSE, + EMUL_GET_CODEC_VER = 50, + EMUL_LOCK_MEM_REGION, + EMUL_UNLOCK_MEM_REGION, }; -void restore_codec_context(AVCodecContext *dstctx, - AVCodecContext *srcctx); +int emul_open_codec_device (CodecExtraInfo * info); + +void emul_close_codec_device (CodecExtraInfo * info); + +int emul_codec_mmap (CodecExtraInfo * info); + +int emul_check_version (CodecExtraInfo * info); -int get_picture_size(int pix_fmt, int width, int height); +int emul_serialize_rational (const AVRational * elem, uint8_t * buff); -int emul_open_codecdev (CodecExtraInfo *pInfo); +int emul_deserialize_rational (const uint8_t * buff, AVRational * elem); -void emul_close_codecdev (CodecExtraInfo *pInfo); +int emul_serialize_frame (const AVFrame * elem, uint8_t * buff); -int emul_check_version (CodecExtraInfo *pInfo); +int emul_deserialize_frame (const uint8_t * buff, AVFrame * elem); /* FFMPEG API */ -void emul_av_register_all (CodecExtraInfo *pInfo); +void emul_av_register_all (CodecExtraInfo * info); -int emul_avcodec_open (AVCodecContext *avctx, - AVCodec *codec, - CodecExtraInfo *pInfo); +int emul_avcodec_init (AVCodecContext * avctx, + AVCodec * codec, CodecExtraInfo * info); -int emul_avcodec_close (AVCodecContext *avctx, CodecExtraInfo *pInfo); +int emul_avcodec_open (AVCodecContext * avctx, + AVCodec * codec, CodecExtraInfo * info); -void emul_avcodec_alloc_frame (CodecExtraInfo *pInfo); +int emul_avcodec_close (AVCodecContext * avctx, CodecExtraInfo * info); -void emul_avcodec_alloc_context (CodecExtraInfo *pInfo); +int emul_avcodec_alloc_context (CodecExtraInfo * info); -void emul_av_free (CodecExtraInfo *pInfo); +void emul_av_free (CodecExtraInfo * info); -void emul_avcodec_flush_buffers (AVCodecContext *ctx, CodecExtraInfo *pInfo); +void emul_avcodec_flush_buffers (AVCodecContext * ctx, CodecExtraInfo * info); -int emul_avcodec_decode_video (AVCodecContext *ctx, AVFrame *frame, - int *got_picture_ptr, AVPacket *packet, - CodecExtraInfo *pInfo); +int emul_avcodec_decode_video (AVCodecContext * ctx, AVFrame * frame, + int *got_picture_ptr, AVPacket * packet, CodecExtraInfo * info); -int emul_avcodec_encode_video (AVCodecContext *ctx, uint8_t *buf, - int buf_size, const AVFrame *pict, - uint8_t *pict_buf, uint32_t pict_buf_size, CodecExtraInfo *pInfo); +int emul_avcodec_encode_video (AVCodecContext * ctx, uint8_t * out_buf, + int buf_size, const AVFrame * pict, + uint8_t * pict_buf, uint32_t pict_buf_size, CodecExtraInfo * info); -int emul_avcodec_decode_audio (AVCodecContext *avctx, int16_t *samples, - int *frame_size_ptr, AVPacket *packet, - CodecExtraInfo *pInfo); +int emul_avcodec_decode_audio (AVCodecContext * avctx, int16_t * samples, + int *frame_size_ptr, AVPacket * packet, CodecExtraInfo * info); -void emul_av_picture_copy (AVPicture *dst, uint32_t fsize, CodecExtraInfo *pInfo); +void emul_av_picture_copy (AVPicture * dst, + uint32_t fsize, CodecExtraInfo * info); -void emul_av_parser_init (CodecExtraInfo *pInfo); +void emul_av_parser_init (CodecExtraInfo * info); -int emul_av_parser_parse (AVCodecParserContext *s, AVCodecContext *ctx, - uint8_t **outbuf, int *outbuf_size, - const uint8_t *buf, int buf_size, - int64_t pts, int64_t dts, int64_t pos, CodecExtraInfo *pInfo); +int emul_av_parser_parse (AVCodecParserContext * s, AVCodecContext * ctx, + uint8_t ** outbuf, int *outbuf_size, + const uint8_t * inbuf, int inbuf_size, + int64_t pts, int64_t dts, int64_t pos, CodecExtraInfo * info); -void emul_av_parser_close (CodecExtraInfo *pInfo); +void emul_av_parser_close (CodecExtraInfo * info); diff --git a/ext/ffmpeg/gstffmpegenc.c b/ext/ffmpeg/gstffmpegenc.c index 9f38ee7..10b8ae5 100644 --- a/ext/ffmpeg/gstffmpegenc.c +++ b/ext/ffmpeg/gstffmpegenc.c @@ -87,7 +87,8 @@ gst_ffmpegenc_me_method_get_type (void) }; if (!ffmpegenc_me_method_type) { ffmpegenc_me_method_type = - g_enum_register_static ("GstFFMpegEncMeMethodEmul", ffmpegenc_me_methods); + g_enum_register_static ("GstFFMpegEncMeMethodEmul", + ffmpegenc_me_methods); } return ffmpegenc_me_method_type; } @@ -254,9 +255,9 @@ gst_ffmpegenc_init (GstFFMpegEnc * ffmpegenc) ffmpegenc->picture = avcodec_alloc_frame (); #if 0 #ifndef EMUL_CODEC - emul_avcodec_alloc_context(&ffmpegenc->codecInfo); - emul_avcodec_alloc_frame(&ffmpegenc->codecInfo); - CODEC_LOG(2, "context index: %d\n", ffmpegenc->codecInfo.contextIndex); + emul_avcodec_alloc_context (&ffmpegenc->codecInfo); + emul_avcodec_alloc_frame (&ffmpegenc->codecInfo); + CODEC_LOG (2, "context index: %d\n", ffmpegenc->codecInfo.contextIndex); #endif #endif ffmpegenc->opened = FALSE; @@ -307,10 +308,10 @@ gst_ffmpegenc_finalize (GObject * object) } /* clean up remaining allocated data */ - CODEC_LOG(2, "before free AVCodecContext and AVFrame\n"); #ifndef EMUL_CODEC - emul_av_free(&ffmpegenc->codecInfo); - emul_close_codecdev(&ffmpegenc->codecInfo); + CODEC_LOG (2, "free AVCodecContext and AVFrame\n"); + emul_av_free (&ffmpegenc->codecInfo); + emul_close_codec_device (&ffmpegenc->codecInfo); #endif av_free (ffmpegenc->context); av_free (ffmpegenc->picture); @@ -481,9 +482,10 @@ gst_ffmpegenc_getcaps (GstPad * pad) ctx->pix_fmt = pixfmt; GST_DEBUG ("Attempting to open codec"); - CODEC_LOG(2, "before gst_ffmpeg_avcodec_open, codec_id:%d\n", oclass->in_plugin->id); - if (gst_ffmpeg_avcodec_open (ctx, oclass->in_plugin, &ffmpegenc->codecInfo) >= 0 && - ctx->pix_fmt == pixfmt) { + CODEC_LOG (2, "before gst_ffmpeg_avcodec_open, codec_id:%d\n", + oclass->in_plugin->id); + if (gst_ffmpeg_avcodec_open (ctx, oclass->in_plugin, + &ffmpegenc->codecInfo) >= 0 && ctx->pix_fmt == pixfmt) { ctx->width = -1; if (!caps) caps = gst_caps_new_empty (); @@ -662,8 +664,9 @@ gst_ffmpegenc_setcaps (GstPad * pad, GstCaps * caps) } /* open codec */ - CODEC_LOG(2, "before gst_ffmpeg_avcodec_open\n"); - if (gst_ffmpeg_avcodec_open (ffmpegenc->context, oclass->in_plugin, &ffmpegenc->codecInfo) < 0) { + CODEC_LOG (2, "before gst_ffmpeg_avcodec_open\n"); + if (gst_ffmpeg_avcodec_open (ffmpegenc->context, oclass->in_plugin, + &ffmpegenc->codecInfo) < 0) { if (ffmpegenc->context->priv_data) gst_ffmpeg_avcodec_close (ffmpegenc->context, &ffmpegenc->codecInfo); if (ffmpegenc->context->stats_in) @@ -788,31 +791,39 @@ gst_ffmpegenc_chain_video (GstPad * pad, GstBuffer * inbuf) if (force_keyframe) ffmpegenc->picture->pict_type = FF_I_TYPE; - CODEC_LOG(2, "pict_type:%d\n", ffmpegenc->picture->pict_type); + CODEC_LOG (2, "pict_type:%d\n", ffmpegenc->picture->pict_type); frame_size = gst_ffmpeg_avpicture_fill ((AVPicture *) ffmpegenc->picture, GST_BUFFER_DATA (inbuf), ffmpegenc->context->pix_fmt, ffmpegenc->context->width, ffmpegenc->context->height); g_return_val_if_fail (frame_size == GST_BUFFER_SIZE (inbuf), GST_FLOW_ERROR); - CODEC_LOG(2, "frame_size:%d, inbuf_size:%d\n", frame_size, GST_BUFFER_SIZE(inbuf)); + CODEC_LOG (2, "frame_size:%d, inbuf_size:%d\n", frame_size, + GST_BUFFER_SIZE (inbuf)); +#ifndef EMUL_CODEC + ffmpegenc->codecInfo.last_pts = ffmpegenc->picture->pts; +#endif ffmpegenc->picture->pts = gst_ffmpeg_time_gst_to_ff (GST_BUFFER_TIMESTAMP (inbuf) / ffmpegenc->context->ticks_per_frame, ffmpegenc->context->time_base); - CODEC_LOG(2, "pts:%lld, ticks_per_frame:%d, time_base:%d/%d\n", ffmpegenc->picture->pts, - ffmpegenc->context->ticks_per_frame, ffmpegenc->context->time_base.num, ffmpegenc->context->time_base.den); + CODEC_LOG (2, "pts:%lld, ticks_per_frame:%d, time_base:%d/%d\n", + ffmpegenc->picture->pts, ffmpegenc->context->ticks_per_frame, + ffmpegenc->context->time_base.num, ffmpegenc->context->time_base.den); ffmpegenc_setup_working_buf (ffmpegenc); - CODEC_LOG(2, "before avcodec_encode_video, codec_id:%d\n", ffmpegenc->context->codec_id); + CODEC_LOG (2, "before avcodec_encode_video, codec_id:%d\n", + ffmpegenc->context->codec_id); #ifdef EMUL_CODEC ret_size = avcodec_encode_video (ffmpegenc->context, ffmpegenc->working_buf, ffmpegenc->working_buf_size, ffmpegenc->picture); #else - ret_size = emul_avcodec_encode_video (ffmpegenc->context, ffmpegenc->working_buf, - ffmpegenc->working_buf_size, ffmpegenc->picture, GST_BUFFER_DATA (inbuf), GST_BUFFER_SIZE(inbuf), &ffmpegenc->codecInfo); + ret_size = + emul_avcodec_encode_video (ffmpegenc->context, ffmpegenc->working_buf, + ffmpegenc->working_buf_size, ffmpegenc->picture, GST_BUFFER_DATA (inbuf), + GST_BUFFER_SIZE (inbuf), &ffmpegenc->codecInfo); #endif - CODEC_LOG(2, "after encode video, ret:%d\n", ret_size); + CODEC_LOG (2, "after encode video, ret:%d\n", ret_size); if (ret_size < 0) { #ifndef GST_DISABLE_GST_DEBUG @@ -1099,15 +1110,17 @@ gst_ffmpegenc_flush_buffers (GstFFMpegEnc * ffmpegenc, gboolean send) ffmpegenc_setup_working_buf (ffmpegenc); - CODEC_LOG(2, "before avcodec_encode_video, codec_id:%d\n", ffmpegenc->context->codec_id); + CODEC_LOG (2, "before avcodec_encode_video, codec_id:%d\n", + ffmpegenc->context->codec_id); #ifdef EMUL_CODEC ret_size = avcodec_encode_video (ffmpegenc->context, ffmpegenc->working_buf, ffmpegenc->working_buf_size, NULL); #else ret_size = emul_avcodec_encode_video (ffmpegenc->context, - ffmpegenc->working_buf, ffmpegenc->working_buf_size, NULL, NULL, 0, &ffmpegenc->codecInfo); + ffmpegenc->working_buf, ffmpegenc->working_buf_size, NULL, NULL, 0, + &ffmpegenc->codecInfo); #endif - CODEC_LOG(2, "after encode vidoe : ret(%d)\n", ret_size); + CODEC_LOG (2, "after encode vidoe : ret(%d)\n", ret_size); if (ret_size < 0) { /* there should be something, notify and give up */ #ifndef GST_DISABLE_GST_DEBUG diff --git a/packaging/gst-ffmpeg-emul.spec b/packaging/gst-ffmpeg-emul.spec index b639862..941c1fb 100644 --- a/packaging/gst-ffmpeg-emul.spec +++ b/packaging/gst-ffmpeg-emul.spec @@ -1,5 +1,5 @@ Name: gst-ffmpeg-emul -Version: 0.2.16 +Version: 0.2.17 Release: 1 Summary: GStreamer Streaming-media framework plug-in for Tizen emulator using FFmpeg. Group: Libraries/Multimedia |