From 7b915a40458f2cd01db29f9979fda39c7958980e Mon Sep 17 00:00:00 2001 From: Mohamed Naufal Date: Thu, 19 Apr 2012 00:02:53 +0530 Subject: [PATCH] libstagefright: explicitly set positive timestamps as stagefright expects them so --- configure | 2 +- libavcodec/libstagefright.cpp | 28 +++++++++++++++++++++++----- tools/build_libstagefright | 6 +++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/configure b/configure index 6f23af3969..8b87ad4824 100755 --- a/configure +++ b/configure @@ -2951,7 +2951,7 @@ enabled libschroedinger && require_pkg_config schroedinger-1.0 schroedinger/schr enabled libspeex && require libspeex speex/speex.h speex_decoder_init -lspeex enabled libstagefright_h264 && require_cpp libstagefright_h264 "binder/ProcessState.h media/stagefright/MetaData.h media/stagefright/MediaBufferGroup.h media/stagefright/MediaDebug.h media/stagefright/MediaDefs.h - media/stagefright/OMXClient.h media/stagefright/OMXCodec.h" android::OMXClient -lstagefright -lmedia -lutils -lbinder + media/stagefright/OMXClient.h media/stagefright/OMXCodec.h" android::OMXClient -lstagefright -lmedia -lutils -lbinder -lgnustl_static enabled libtheora && require libtheora theora/theoraenc.h th_info_init -ltheoraenc -ltheoradec -logg enabled libvo_aacenc && require libvo_aacenc vo-aacenc/voAAC.h voGetAACEncAPI -lvo-aacenc enabled libvo_amrwbenc && require libvo_amrwbenc vo-amrwbenc/enc_if.h E_IF_init -lvo-amrwbenc diff --git a/libavcodec/libstagefright.cpp b/libavcodec/libstagefright.cpp index 9ffe27c0d9..f373a10fa5 100644 --- a/libavcodec/libstagefright.cpp +++ b/libavcodec/libstagefright.cpp @@ -31,6 +31,7 @@ #include #include #include +#include extern "C" { #include "avcodec.h" @@ -51,6 +52,11 @@ struct Frame { int32_t w, h; }; +struct TimeStamp { + int64_t pts; + int64_t reordered_opaque; +}; + class CustomSource; struct StagefrightContext { @@ -69,6 +75,8 @@ struct StagefrightContext { volatile sig_atomic_t thread_started, thread_exited, stop_decode; AVFrame ret_frame; + std::map *ts_map; + int64_t frame_index; uint8_t *dummy_buf; int dummy_bufsize; @@ -234,10 +242,11 @@ static av_cold int Stagefright_init(AVCodecContext *avctx) *s->source = new CustomSource(avctx, meta); s->in_queue = new List; s->out_queue = new List; + s->ts_map = new std::map; s->client = new OMXClient; s->end_frame = (Frame*)av_mallocz(sizeof(Frame)); if (s->source == NULL || !s->in_queue || !s->out_queue || !s->client || - !s->end_frame) { + !s->ts_map || !s->end_frame) { ret = AVERROR(ENOMEM); goto fail; } @@ -282,6 +291,7 @@ fail: av_freep(&s->end_frame); delete s->in_queue; delete s->out_queue; + delete s->ts_map; delete s->client; return ret; } @@ -300,6 +310,7 @@ static int Stagefright_decode_frame(AVCodecContext *avctx, void *data, int src_linesize[3]; int orig_size = avpkt->size; AVPacket pkt = *avpkt; + int64_t out_frame_index = 0; int ret; if (!s->thread_started) { @@ -326,10 +337,6 @@ static int Stagefright_decode_frame(AVCodecContext *avctx, void *data, if (avpkt->data) { frame->status = OK; frame->size = avpkt->size; - // Stagefright can't handle negative timestamps - - // if needed, work around this by offsetting them manually? - if (avpkt->pts >= 0) - frame->time = avpkt->pts; frame->key = avpkt->flags & AV_PKT_FLAG_KEY ? 1 : 0; frame->buffer = (uint8_t*)av_malloc(avpkt->size); if (!frame->buffer) { @@ -343,6 +350,10 @@ static int Stagefright_decode_frame(AVCodecContext *avctx, void *data, frame->size = orig_size; } memcpy(frame->buffer, ptr, orig_size); + + frame->time = ++s->frame_index; + (*s->ts_map)[s->frame_index].pts = avpkt->pts; + (*s->ts_map)[s->frame_index].reordered_opaque = avctx->reordered_opaque; } else { frame->status = ERROR_END_OF_STREAM; s->source_done = true; @@ -431,6 +442,12 @@ static int Stagefright_decode_frame(AVCodecContext *avctx, void *data, src_data, src_linesize, avctx->pix_fmt, avctx->width, avctx->height); + mbuffer->meta_data()->findInt64(kKeyTime, &out_frame_index); + if (out_frame_index && s->ts_map->count(out_frame_index) > 0) { + s->ret_frame.pts = (*s->ts_map)[out_frame_index].pts; + s->ret_frame.reordered_opaque = (*s->ts_map)[out_frame_index].reordered_opaque; + s->ts_map->erase(out_frame_index); + } *data_size = sizeof(AVFrame); *(AVFrame*)data = s->ret_frame; ret = orig_size; @@ -523,6 +540,7 @@ static av_cold int Stagefright_close(AVCodecContext *avctx) delete s->in_queue; delete s->out_queue; + delete s->ts_map; delete s->client; delete s->decoder; delete s->source; diff --git a/tools/build_libstagefright b/tools/build_libstagefright index 78c20841ee..8b3a0930f9 100755 --- a/tools/build_libstagefright +++ b/tools/build_libstagefright @@ -28,6 +28,7 @@ TOOLCHAIN=`echo $NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/*-x86` export PATH=$TOOLCHAIN/bin:$PATH ANDROID_SOURCE=../android-source ANDROID_LIBS=../android-libs +ABI="armeabi-v7a" rm -rf ../build/stagefright mkdir -p ../build/stagefright @@ -40,12 +41,11 @@ FLAGS="$FLAGS --disable-avdevice --disable-decoder=h264 --disable-decoder=h264_v EXTRA_CFLAGS="-I$ANDROID_SOURCE/frameworks/base/include -I$ANDROID_SOURCE/system/core/include" EXTRA_CFLAGS="$EXTRA_CFLAGS -I$ANDROID_SOURCE/frameworks/base/media/libstagefright" EXTRA_CFLAGS="$EXTRA_CFLAGS -I$ANDROID_SOURCE/frameworks/base/include/media/stagefright/openmax" -EXTRA_CFLAGS="$EXTRA_CFLAGS -I$NDK/sources/cxx-stl/system/include" +EXTRA_CFLAGS="$EXTRA_CFLAGS -I$NDK/sources/cxx-stl/gnu-libstdc++/include -I$NDK/sources/cxx-stl/gnu-libstdc++/libs/$ABI/include" EXTRA_CFLAGS="$EXTRA_CFLAGS -march=armv7-a -mfloat-abi=softfp -mfpu=neon" -EXTRA_LDFLAGS="-Wl,--fix-cortex-a8 -L$ANDROID_LIBS -Wl,-rpath-link,$ANDROID_LIBS" +EXTRA_LDFLAGS="-Wl,--fix-cortex-a8 -L$ANDROID_LIBS -Wl,-rpath-link,$ANDROID_LIBS -L$NDK/sources/cxx-stl/gnu-libstdc++/libs/$ABI" EXTRA_CXXFLAGS="-Wno-multichar -fno-exceptions -fno-rtti" -ABI="armeabi-v7a" DEST="$DEST/$ABI" FLAGS="$FLAGS --prefix=$DEST"