OSDN Git Service

add HEVC(H.265) decoder. plz sync:
[android-x86/external-stagefright-plugins.git] / libstagefright / FFmpegExtractor / FFmpegExtractor.cpp
index 86cfe97..08fa7a2 100644 (file)
@@ -18,7 +18,9 @@
 #define LOG_TAG "FFmpegExtractor"
 #include <utils/Log.h>
 
+#include <stdint.h>
 #include <limits.h> /* INT_MAX */
+#include <inttypes.h>
 
 #include <media/stagefright/foundation/ABitReader.h>
 #include <media/stagefright/foundation/ABuffer.h>
 #include <media/stagefright/foundation/hexdump.h>
 #include <media/stagefright/DataSource.h>
 #include <media/stagefright/MediaBuffer.h>
-#include <media/stagefright/MediaDebug.h>
+#include <media/stagefright/foundation/ADebug.h>
 #include <media/stagefright/MediaDefs.h>
 #include <media/stagefright/MediaErrors.h>
 #include <media/stagefright/MediaSource.h>
 #include <media/stagefright/MetaData.h>
 #include <media/stagefright/Utils.h>
+#include <cutils/properties.h>
 #include <utils/String8.h>
 #include <utils/misc.h>
 
 #include "include/avc_utils.h"
-#include "utils/common_utils.h"
 #include "utils/ffmpeg_utils.h"
+#include "utils/ffmpeg_cmdutils.h"
 #include "FFmpegExtractor.h"
 
-#define DEBUG_READ_ENTRY 0
-#define DIABLE_VIDEO     0
-#define DIABLE_AUDIO     0
-#define WAIT_KEY_PACKET_AFTER_SEEK 1
-#define DISABLE_NAL_TO_ANNEXB 0
-
 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
 #define MIN_AUDIOQ_SIZE (20 * 16 * 1024)
 #define MIN_FRAMES 5
 #define EXTRACTOR_MAX_PROBE_PACKETS 200
-
 #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - FF_INPUT_BUFFER_PADDING_SIZE)
 
+//debug
+#define DEBUG_READ_ENTRY           0
+#define DEBUG_DISABLE_VIDEO        0
+#define DEBUG_DISABLE_AUDIO        0
+#define WAIT_KEY_PACKET_AFTER_SEEK 1
+#define DISABLE_NAL_TO_ANNEXB      0
+#define DEBUG_PKT                  0
+
 enum {
     NO_SEEK = 0,
     SEEK,
@@ -63,8 +67,10 @@ static AVPacket flush_pkt;
 
 namespace android {
 
+static const char *findMatchingContainer(const char *name);
+
 struct FFmpegExtractor::Track : public MediaSource {
-    Track(const sp<FFmpegExtractor> &extractor, sp<MetaData> meta, bool isAVC,
+    Track(FFmpegExtractor *extractor, sp<MetaData> meta, bool isAVC,
           AVStream *stream, PacketQueue *queue);
 
     virtual status_t start(MetaData *params);
@@ -80,7 +86,7 @@ protected:
 private:
     friend struct FFmpegExtractor;
 
-    sp<FFmpegExtractor> mExtractor;
+    FFmpegExtractor *mExtractor;
     sp<MetaData> mMeta;
 
     enum AVMediaType mMediaType;
@@ -94,6 +100,8 @@ private:
     AVStream *mStream;
     PacketQueue *mQueue;
 
+    int64_t mFirstKeyPktTimestamp;
+
     DISALLOW_EVIL_CONSTRUCTORS(Track);
 };
 
@@ -101,27 +109,18 @@ private:
 
 FFmpegExtractor::FFmpegExtractor(const sp<DataSource> &source)
     : mDataSource(source),
-      mReaderThreadStarted(false),
-      mInitCheck(NO_INIT) {
-    LOGV("FFmpegExtractor::FFmpegExtractor");
+      mMeta(new MetaData),
+      mInitCheck(NO_INIT),
+      mFFmpegInited(false),
+      mFormatCtx(NULL),
+      mReaderThreadStarted(false) {
+    ALOGV("FFmpegExtractor::FFmpegExtractor");
 
-    int err;
-    const char *url = mDataSource->getNamURI();
-    if (url == NULL) {
-        LOGI("url is error!");
-        return;
-    }
-    // is it right?
-    if (!strcmp(url, "-")) {
-        av_strlcpy(mFilename, "pipe:", strlen("pipe:") + 1);
-    } else {
-        av_strlcpy(mFilename, url, strlen(url) + 1);
-    }
-    LOGI("url: %s, mFilename: %s", url, mFilename);
+    buildFileName(source);
 
-    err = initStreams();
+    int err = initStreams();
     if (err < 0) {
-        LOGE("failed to init ffmpeg");
+        ALOGE("failed to init ffmpeg");
         return;
     }
 
@@ -132,17 +131,17 @@ FFmpegExtractor::FFmpegExtractor(const sp<DataSource> &source)
         (mFormatCtx->pb ? !mFormatCtx->pb->error : 1) &&
         (mDefersToCreateVideoTrack || mDefersToCreateAudioTrack)) {
         // FIXME, i am so lazy! Should use pthread_cond_wait to wait conditions
-        NamDelay(5);
+        usleep(5000);
     }
 
-    LOGV("mProbePkts: %d, mEOF: %d, pb->error(if has): %d, mDefersToCreateVideoTrack: %d, mDefersToCreateAudioTrack: %d",
+    ALOGV("mProbePkts: %d, mEOF: %d, pb->error(if has): %d, mDefersToCreateVideoTrack: %d, mDefersToCreateAudioTrack: %d",
         mProbePkts, mEOF, mFormatCtx->pb ? mFormatCtx->pb->error : 0, mDefersToCreateVideoTrack, mDefersToCreateAudioTrack);
 
     mInitCheck = OK;
 }
 
 FFmpegExtractor::~FFmpegExtractor() {
-    LOGV("FFmpegExtractor::~FFmpegExtractor");
+    ALOGV("FFmpegExtractor::~FFmpegExtractor");
 
     // stop reader here if no track!
     stopReaderThread();
@@ -155,7 +154,7 @@ size_t FFmpegExtractor::countTracks() {
 }
 
 sp<MediaSource> FFmpegExtractor::getTrack(size_t index) {
-    LOGV("FFmpegExtractor::getTrack[%d]", index);
+    ALOGV("FFmpegExtractor::getTrack[%d]", index);
 
     if (mInitCheck != OK) {
         return NULL;
@@ -169,7 +168,7 @@ sp<MediaSource> FFmpegExtractor::getTrack(size_t index) {
 }
 
 sp<MetaData> FFmpegExtractor::getTrackMetaData(size_t index, uint32_t flags) {
-    LOGV("FFmpegExtractor::getTrackMetaData[%d]", index);
+    ALOGV("FFmpegExtractor::getTrackMetaData[%d]", index);
 
     if (mInitCheck != OK) {
         return NULL;
@@ -183,24 +182,20 @@ sp<MetaData> FFmpegExtractor::getTrackMetaData(size_t index, uint32_t flags) {
 }
 
 sp<MetaData> FFmpegExtractor::getMetaData() {
-    LOGV("FFmpegExtractor::getMetaData");
+    ALOGV("FFmpegExtractor::getMetaData");
 
     if (mInitCheck != OK) {
         return NULL;
     }
 
-    sp<MetaData> meta = new MetaData;
-    // TODO
-    meta->setCString(kKeyMIMEType, "video/ffmpeg");
-
-    return meta;
+    return mMeta;
 }
 
 uint32_t FFmpegExtractor::flags() const {
-    LOGV("FFmpegExtractor::flags");
+    ALOGV("FFmpegExtractor::flags");
 
     if (mInitCheck != OK) {
-        return NULL;
+        return 0;
     }
 
     uint32_t flags = CAN_PAUSE;
@@ -362,36 +357,6 @@ static sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) {
     return esds;
 }
 
-// the same as MakeMPEGVideoESDS
-static sp<ABuffer> MakeRawCodecSpecificData(const sp<ABuffer> &csd) {
-    sp<ABuffer> esds = new ABuffer(csd->size() + 25);
-
-    uint8_t *ptr = esds->data();
-    *ptr++ = 0x03;
-    EncodeSize14(&ptr, 22 + csd->size());
-
-    *ptr++ = 0x00;  // ES_ID
-    *ptr++ = 0x00;
-
-    *ptr++ = 0x00;  // streamDependenceFlag, URL_Flag, OCRstreamFlag
-
-    *ptr++ = 0x04;
-    EncodeSize14(&ptr, 16 + csd->size());
-
-    *ptr++ = 0x40;  // Audio ISO/IEC 14496-3
-
-    for (size_t i = 0; i < 12; ++i) {
-        *ptr++ = 0x00;
-    }
-
-    *ptr++ = 0x05;
-    EncodeSize14(&ptr, csd->size());
-
-    memcpy(ptr, csd->data(), csd->size());
-
-    return esds;
-}
-
 // Returns the sample rate based on the sampling frequency index
 static uint32_t get_sample_rate(const uint8_t sf_index)
 {
@@ -410,9 +375,10 @@ static uint32_t get_sample_rate(const uint8_t sf_index)
 
 int FFmpegExtractor::check_extradata(AVCodecContext *avctx)
 {
-    const char *name;
-    bool *defersToCreateTrack;
-    AVBitStreamFilterContext **bsfc;
+    enum AVCodecID codec_id = AV_CODEC_ID_NONE;
+    const char *name = NULL;
+    bool *defersToCreateTrack = NULL;
+    AVBitStreamFilterContext **bsfc = NULL;
 
     // init
     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
@@ -423,22 +389,22 @@ int FFmpegExtractor::check_extradata(AVCodecContext *avctx)
         defersToCreateTrack = &mDefersToCreateAudioTrack;
     }
 
+       codec_id = avctx->codec_id;
+
     // ignore extradata
-    if (avctx->codec_id == CODEC_ID_MP3 ||
-            avctx->codec_id == CODEC_ID_MP1  ||
-            avctx->codec_id == CODEC_ID_MP2  ||
-            avctx->codec_id == CODEC_ID_AC3  ||
-            avctx->codec_id == CODEC_ID_H263  ||
-            avctx->codec_id == CODEC_ID_H263P ||
-            avctx->codec_id == CODEC_ID_H263I ||
-            avctx->codec_id == CODEC_ID_WMV1)
+    if (codec_id != AV_CODEC_ID_H264
+            && codec_id != AV_CODEC_ID_MPEG4
+            && codec_id != AV_CODEC_ID_MPEG1VIDEO
+            && codec_id != AV_CODEC_ID_MPEG2VIDEO
+            && codec_id != AV_CODEC_ID_AAC) {
         return 1;
+    }
 
     // is extradata compatible with android?
-    if (avctx->codec_id != CODEC_ID_AAC) {
+    if (codec_id != AV_CODEC_ID_AAC) {
         int is_compatible = is_extradata_compatible_with_android(avctx);
         if (!is_compatible) {
-            LOGI("%s extradata is not compatible with android, should to extract it from bitstream",
+            ALOGI("%s extradata is not compatible with android, should to extract it from bitstream",
                     av_get_media_type_string(avctx->codec_type));
             *defersToCreateTrack = true;
             *bsfc = NULL; // H264 don't need bsfc, only AAC?
@@ -447,23 +413,23 @@ int FFmpegExtractor::check_extradata(AVCodecContext *avctx)
         return 1;
     }
 
-    if (avctx->codec_id == CODEC_ID_AAC) {
+    if (codec_id == AV_CODEC_ID_AAC) {
         name = "aac_adtstoasc";
     }
 
     if (avctx->extradata_size <= 0) {
-        LOGI("No %s extradata found, should to extract it from bitstream",
+        ALOGI("No %s extradata found, should to extract it from bitstream",
                 av_get_media_type_string(avctx->codec_type));
         *defersToCreateTrack = true;
          //CHECK(name != NULL);
         if (!*bsfc && name) {
             *bsfc = av_bitstream_filter_init(name);
             if (!*bsfc) {
-                LOGE("Cannot open the %s BSF!", name);
+                ALOGE("Cannot open the %s BSF!", name);
                 *defersToCreateTrack = false;
                 return -1;
             } else {
-                LOGV("open the %s bsf", name);
+                ALOGV("open the %s bsf", name);
                 return 0;
             }
         } else {
@@ -473,59 +439,101 @@ int FFmpegExtractor::check_extradata(AVCodecContext *avctx)
     return 1;
 }
 
+void FFmpegExtractor::printTime(int64_t time)
+{
+    int hours, mins, secs, us;
 
-int FFmpegExtractor::stream_component_open(int stream_index)
+    if (time == AV_NOPTS_VALUE)
+        return;
+
+    secs = time / AV_TIME_BASE;
+    us = time % AV_TIME_BASE;
+    mins = secs / 60;
+    secs %= 60;
+    hours = mins / 60;
+    mins %= 60;
+    ALOGI("the time is %02d:%02d:%02d.%02d",
+        hours, mins, secs, (100 * us) / AV_TIME_BASE);
+}
+
+bool FFmpegExtractor::is_codec_supported(enum AVCodecID codec_id)
 {
-    AVCodecContext *avctx;
-    sp<MetaData> meta;
-    bool isAVC = false;
     bool supported = false;
-    uint32_t type;
-    const void *data;
-    size_t size;
-    int ret;
 
-    LOGI("stream_index: %d", stream_index);
-    if (stream_index < 0 || stream_index >= mFormatCtx->nb_streams)
-        return -1;
-    avctx = mFormatCtx->streams[stream_index]->codec;
+    switch(codec_id) {
+    case AV_CODEC_ID_H264:
+    case AV_CODEC_ID_MPEG4:
+    case AV_CODEC_ID_H263:
+    case AV_CODEC_ID_H263P:
+    case AV_CODEC_ID_H263I:
+    case AV_CODEC_ID_AAC:
+    case AV_CODEC_ID_AC3:
+    case AV_CODEC_ID_MP2:
+    case AV_CODEC_ID_MP3:
+    case AV_CODEC_ID_MPEG1VIDEO:
+    case AV_CODEC_ID_MPEG2VIDEO:
+    case AV_CODEC_ID_WMV1:
+    case AV_CODEC_ID_WMV2:
+    case AV_CODEC_ID_WMV3:
+    case AV_CODEC_ID_VC1:
+    case AV_CODEC_ID_WMAV1:
+    case AV_CODEC_ID_WMAV2:
+    case AV_CODEC_ID_WMAPRO:
+    case AV_CODEC_ID_WMALOSSLESS:
+    case AV_CODEC_ID_RV20:
+    case AV_CODEC_ID_RV30:
+    case AV_CODEC_ID_RV40:
+    case AV_CODEC_ID_COOK:
+    case AV_CODEC_ID_APE:
+    case AV_CODEC_ID_DTS:
+    case AV_CODEC_ID_FLAC:
+    case AV_CODEC_ID_FLV1:
+    case AV_CODEC_ID_VORBIS:
+    case AV_CODEC_ID_HEVC:
 
-    switch(avctx->codec_id) {
-    case CODEC_ID_H264:
-    case CODEC_ID_MPEG4:
-    case CODEC_ID_H263:
-    case CODEC_ID_H263P:
-    case CODEC_ID_H263I:
-    case CODEC_ID_AAC:
-    case CODEC_ID_AC3:
-    case CODEC_ID_MP1:
-    case CODEC_ID_MP2:
-    case CODEC_ID_MP3:
-    case CODEC_ID_MPEG2VIDEO:
-    case CODEC_ID_WMV1:
-    case CODEC_ID_WMV2:
-    case CODEC_ID_WMAV2:
-#if 0
-    case CODEC_ID_VC1:
-#endif
         supported = true;
         break;
     default:
-        supported = false;
+        ALOGD("unsuppoted codec(%s), but give it a chance",
+                avcodec_get_name(codec_id));
+        //Won't promise that the following codec id can be supported.
+           //Just give these codecs a chance.
+        supported = true;
         break;
     }
 
+       return supported;
+}
+
+int FFmpegExtractor::stream_component_open(int stream_index)
+{
+    AVCodecContext *avctx = NULL;
+    sp<MetaData> meta = NULL;
+    bool isAVC = false;
+    bool supported = false;
+    uint32_t type = 0;
+    const void *data = NULL;
+    size_t size = 0;
+    int ret = 0;
+
+    ALOGI("stream_index: %d", stream_index);
+    if (stream_index < 0 || stream_index >= (int)mFormatCtx->nb_streams)
+        return -1;
+    avctx = mFormatCtx->streams[stream_index]->codec;
+
+    supported = is_codec_supported(avctx->codec_id);
+
     if (!supported) {
-        LOGE("unsupport the codec, id: 0x%0x", avctx->codec_id);
+        ALOGE("unsupport the codec(%s)", avcodec_get_name(avctx->codec_id));
         return -1;
     }
-    LOGV("support the codec");
+    ALOGI("support the codec(%s)", avcodec_get_name(avctx->codec_id));
 
     unsigned streamType;
     ssize_t index = mTracks.indexOfKey(stream_index);
 
     if (index >= 0) {
-        LOGE("this track already exists");
+        ALOGE("this track already exists");
         return 0;
     }
 
@@ -533,7 +541,7 @@ int FFmpegExtractor::stream_component_open(int stream_index)
 
     char tagbuf[32];
     av_get_codec_tag_string(tagbuf, sizeof(tagbuf), avctx->codec_tag);
-    LOGV("Tag %s/0x%08x with codec id '%d'\n", tagbuf, avctx->codec_tag, avctx->codec_id);
+    ALOGV("Tag %s/0x%08x with codec(%s)\n", tagbuf, avctx->codec_tag, avcodec_get_name(avctx->codec_id));
 
     switch (avctx->codec_type) {
     case AVMEDIA_TYPE_VIDEO:
@@ -542,7 +550,7 @@ int FFmpegExtractor::stream_component_open(int stream_index)
         if (mVideoStream == NULL)
             mVideoStream = mFormatCtx->streams[stream_index];
         if (!mVideoQInited) {
-           packet_queue_init(&mVideoQ);
+               packet_queue_init(&mVideoQ);
             mVideoQInited = true;
         }
 
@@ -560,16 +568,16 @@ int FFmpegExtractor::stream_component_open(int stream_index)
          }
 
         if (avctx->extradata) {
-            LOGV("video stream extradata:");
+            ALOGV("video stream extradata:");
             hexdump(avctx->extradata, avctx->extradata_size);
         } else {
-            LOGV("video stream no extradata, but we can ignore it.");
+            ALOGV("video stream no extradata, but we can ignore it.");
         }
 
         meta = new MetaData;
 
         switch(avctx->codec_id) {
-        case CODEC_ID_H264:
+        case AV_CODEC_ID_H264:
             /**
              * H.264 Video Types
              * http://msdn.microsoft.com/en-us/library/dd757808(v=vs.85).aspx
@@ -578,7 +586,7 @@ int FFmpegExtractor::stream_component_open(int stream_index)
             if (avctx->extradata[0] == 1 /* configurationVersion */) {
                 // H.264 bitstream without start codes.
                 isAVC = true;
-                LOGV("AVC");
+                ALOGV("AVC");
 
                 if (avctx->width == 0 || avctx->height == 0) {
                     int32_t width, height;
@@ -594,7 +602,7 @@ int FFmpegExtractor::stream_component_open(int stream_index)
             } else {
                 // H.264 bitstream with start codes.
                 isAVC = false;
-                LOGV("H264");
+                ALOGV("H264");
 
                 /* set NULL to release meta as we will new a meta in MakeAVCCodecSpecificData() fxn */
                 meta->clear();
@@ -605,8 +613,8 @@ int FFmpegExtractor::stream_component_open(int stream_index)
                 meta = MakeAVCCodecSpecificData(buffer);
             }
             break;
-        case CODEC_ID_MPEG4:
-            LOGV("MPEG4");
+        case AV_CODEC_ID_MPEG4:
+            ALOGV("MPEG4");
             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
             {
                 sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
@@ -615,14 +623,15 @@ int FFmpegExtractor::stream_component_open(int stream_index)
                 meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
             }
             break;
-        case CODEC_ID_H263:
-        case CODEC_ID_H263P:
-        case CODEC_ID_H263I:
-            LOGV("H263");
+        case AV_CODEC_ID_H263:
+        case AV_CODEC_ID_H263P:
+        case AV_CODEC_ID_H263I:
+            ALOGV("H263");
             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
             break;
-        case CODEC_ID_MPEG2VIDEO:
-            LOGV("MPEG2VIDEO");
+        case AV_CODEC_ID_MPEG1VIDEO:
+        case AV_CODEC_ID_MPEG2VIDEO:
+            ALOGV("MPEG%dVIDEO", avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 2 : 1);
             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
             {
                 sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
@@ -631,35 +640,91 @@ int FFmpegExtractor::stream_component_open(int stream_index)
                 meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
             }
             break;
-        case CODEC_ID_VC1:
-            LOGV("VC1");
-            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV12);
-            meta->setData(kKeyESDS, kTypeESDS, avctx->extradata, avctx->extradata_size);
+        case AV_CODEC_ID_VC1:
+            ALOGV("VC1");
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_VC1);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            break;
+        case AV_CODEC_ID_WMV1:
+            ALOGV("WMV1");
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
+            meta->setInt32(kKeyWMVVersion, kTypeWMVVer_7);
+            break;
+        case AV_CODEC_ID_WMV2:
+            ALOGV("WMV2");
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            meta->setInt32(kKeyWMVVersion, kTypeWMVVer_8);
             break;
-        case CODEC_ID_WMV1:
-            LOGV("WMV1");
-            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV12);
+        case AV_CODEC_ID_WMV3:
+            ALOGV("WMV3");
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            meta->setInt32(kKeyWMVVersion, kTypeWMVVer_9);
             break;
-        case CODEC_ID_WMV2:
-            LOGV("WMV2");
-            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV12);
+        case AV_CODEC_ID_RV20:
+            ALOGV("RV30");
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            meta->setInt32(kKeyRVVersion, kTypeRVVer_G2); //http://en.wikipedia.org/wiki/RealVideo
+        case AV_CODEC_ID_RV30:
+            ALOGV("RV30");
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            meta->setInt32(kKeyRVVersion, kTypeRVVer_8); //http://en.wikipedia.org/wiki/RealVideo
+            break;
+        case AV_CODEC_ID_RV40:
+            ALOGV("RV40");
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            meta->setInt32(kKeyRVVersion, kTypeRVVer_9); //http://en.wikipedia.org/wiki/RealVideo
+            break;
+        case AV_CODEC_ID_FLV1:
+            ALOGV("FLV1");
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_FLV1);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            break;
+        case AV_CODEC_ID_HEVC:
+            ALOGV("HEVC");
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_HEVC);
             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
             break;
         default:
-            CHECK(!"Should not be here. Unsupported codec.");
+            ALOGD("unsuppoted video codec(id:%d, name:%s), but give it a chance",
+                    avctx->codec_id, avcodec_get_name(avctx->codec_id));
+            meta = new MetaData;
+            meta->setInt32(kKeyCodecId, avctx->codec_id);
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_FFMPEG);
+            if (avctx->extradata_size > 0) {
+                meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            }
+            //CHECK(!"Should not be here. Unsupported codec.");
             break;
         }
 
-        LOGI("width: %d, height: %d, bit_rate: %d", avctx->width, avctx->height, avctx->bit_rate);
+        ALOGI("width: %d, height: %d, bit_rate: %d",
+             avctx->width, avctx->height, avctx->bit_rate);
 
         meta->setInt32(kKeyWidth, avctx->width);
         meta->setInt32(kKeyHeight, avctx->height);
         if (avctx->bit_rate > 0)
             meta->setInt32(kKeyBitRate, avctx->bit_rate);
-        if (mFormatCtx->duration != AV_NOPTS_VALUE)
+        if (mVideoStream->duration != AV_NOPTS_VALUE) {
+            int64_t duration = mVideoStream->duration * av_q2d(mVideoStream->time_base) * 1000000;
+            printTime(duration);
+            ALOGV("video startTime: %lld", mVideoStream->start_time);
+            if (mVideoStream->start_time != AV_NOPTS_VALUE) {
+                ALOGV("video startTime:%lld", mVideoStream->start_time);
+            } else {
+                ALOGV("video startTime:N/A");
+            }
+            meta->setInt64(kKeyDuration, duration);
+        } else {
+            // default when no stream duration
             meta->setInt64(kKeyDuration, mFormatCtx->duration);
+        }
 
-        LOGV("create a video track");
+        ALOGV("create a video track");
         index = mTracks.add(
             stream_index, new Track(this, meta, isAVC, mVideoStream, &mVideoQ));
 
@@ -690,35 +755,36 @@ int FFmpegExtractor::stream_component_open(int stream_index)
         }
 
         if (avctx->extradata) {
-            LOGV("audio stream extradata:");
+            ALOGV("audio stream extradata(%d):", avctx->extradata_size);
             hexdump(avctx->extradata, avctx->extradata_size);
         } else {
-            LOGV("audio stream no extradata, but we can ignore it.");
+            ALOGV("audio stream no extradata, but we can ignore it.");
         }
 
         switch(avctx->codec_id) {
-        case CODEC_ID_MP1:
-            LOGV("MP1");
-            meta = new MetaData;
-            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
-            break;
-        case CODEC_ID_MP2:
-            LOGV("MP2");
+        case AV_CODEC_ID_MP2:
+            ALOGV("MP2");
             meta = new MetaData;
             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
             break;
-        case CODEC_ID_MP3:
-            LOGV("MP3");
+        case AV_CODEC_ID_MP3:
+            ALOGV("MP3");
             meta = new MetaData;
             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
             break;
-        case CODEC_ID_AC3:
-            LOGV("AC3");
+        case AV_CODEC_ID_VORBIS:
+            ALOGV("VORBIS");
+            meta = new MetaData;
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_VORBIS);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            break;
+        case AV_CODEC_ID_AC3:
+            ALOGV("AC3");
             meta = new MetaData;
             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3);
             break;
-        case CODEC_ID_AAC:
-            LOGV("AAC"); 
+        case AV_CODEC_ID_AAC:
+            ALOGV("AAC");
             uint32_t sr;
             const uint8_t *header;
             uint8_t profile, sf_index, channel;
@@ -735,35 +801,105 @@ int FFmpegExtractor::stream_component_open(int stream_index)
             sf_index = (header[0] & 0x07) << 1 | (header[1] & 0x80) >> 7;
             sr = get_sample_rate(sf_index);
             if (sr == 0) {
-                LOGE("unsupport the sample rate");
+                ALOGE("unsupport the sample rate");
                 return -1;
             }
             channel = (header[1] >> 3) & 0xf;
-            LOGV("profile: %d, sf_index: %d, channel: %d", profile, sf_index, channel);
+            ALOGV("profile: %d, sf_index: %d, channel: %d", profile, sf_index, channel);
 
             meta = MakeAACCodecSpecificData(profile, sf_index, channel);
             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
             break;
-        case CODEC_ID_WMAV2:
-            LOGV("WMAV2");
+        case AV_CODEC_ID_WMAV1:  // TODO, version?
+            ALOGV("WMAV1");
+            meta = new MetaData;
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            break;
+        case AV_CODEC_ID_WMAV2:
+            ALOGV("WMAV2");
+            meta = new MetaData;
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            meta->setInt32(kKeyWMAVersion, kTypeWMA);
+            break;
+        case AV_CODEC_ID_WMAPRO:
+            ALOGV("WMAPRO");
+            meta = new MetaData;
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            meta->setInt32(kKeyWMAVersion, kTypeWMAPro);
+            break;
+        case AV_CODEC_ID_WMALOSSLESS:
+            ALOGV("WMALOSSLESS");
             meta = new MetaData;
             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            meta->setInt32(kKeyWMAVersion, kTypeWMALossLess);
+            break;
+        case AV_CODEC_ID_COOK: // audio codec in RMVB
+            ALOGV("COOK");
+            meta = new MetaData;
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RA);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            break;
+        case AV_CODEC_ID_APE:
+            ALOGV("APE");
+            meta = new MetaData;
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_APE);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            break;
+        case AV_CODEC_ID_DTS:
+            ALOGV("DTS");
+            meta = new MetaData;
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_DTS);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            break;
+        case AV_CODEC_ID_FLAC:
+            ALOGV("FLAC");
+            meta = new MetaData;
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_FLAC);
+            meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
             break;
         default:
-            CHECK(!"Should not be here. Unsupported codec.");
+            ALOGD("unsuppoted audio codec(id:%d, name:%s), but give it a chance",
+                    avctx->codec_id, avcodec_get_name(avctx->codec_id));
+            meta = new MetaData;
+            meta->setInt32(kKeyCodecId, avctx->codec_id);
+            meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_FFMPEG);
+            if (avctx->extradata_size > 0) {
+                meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+            }
+            //CHECK(!"Should not be here. Unsupported codec.");
             break;
         }
 
-        LOGI("bit_rate: %d, sample_rate: %d, channels: %d", avctx->bit_rate, avctx->sample_rate, avctx->channels);
+        ALOGI("bit_rate: %d, sample_rate: %d, channels: %d, "
+                "bits_per_coded_sample: %d, block_align:%d",
+                avctx->bit_rate, avctx->sample_rate, avctx->channels,
+                avctx->bits_per_coded_sample, avctx->block_align);
 
-        meta->setInt32(kKeySampleRate, avctx->sample_rate);
         meta->setInt32(kKeyChannelCount, avctx->channels);
         meta->setInt32(kKeyBitRate, avctx->bit_rate);
-        if (mFormatCtx->duration != AV_NOPTS_VALUE)
+        meta->setInt32(kKeyBitspersample, avctx->bits_per_coded_sample);
+        meta->setInt32(kKeySampleRate, avctx->sample_rate);
+        meta->setInt32(kKeyBlockAlign, avctx->block_align);
+        meta->setInt32(kKeySampleFormat, avctx->sample_fmt);
+        if (mAudioStream->duration != AV_NOPTS_VALUE) {
+            int64_t duration = mAudioStream->duration * av_q2d(mAudioStream->time_base) * 1000000;
+            printTime(duration);
+            if (mAudioStream->start_time != AV_NOPTS_VALUE) {
+                ALOGV("audio startTime:%lld", mAudioStream->start_time);
+            } else {
+                ALOGV("audio startTime:N/A");
+            }
+            meta->setInt64(kKeyDuration, duration);
+        } else {
+            // default when no stream duration
             meta->setInt64(kKeyDuration, mFormatCtx->duration);
+        }
 
-        LOGV("create a audio track");
+        ALOGV("create a audio track");
         index = mTracks.add(
             stream_index, new Track(this, meta, false, mAudioStream, &mAudioQ));
 
@@ -785,30 +921,30 @@ void FFmpegExtractor::stream_component_close(int stream_index)
 {
     AVCodecContext *avctx;
 
-    if (stream_index < 0 || stream_index >= mFormatCtx->nb_streams)
+    if (stream_index < 0 || stream_index >= (int)mFormatCtx->nb_streams)
         return;
     avctx = mFormatCtx->streams[stream_index]->codec;
 
     switch (avctx->codec_type) {
     case AVMEDIA_TYPE_VIDEO:
-        LOGV("packet_queue_abort videoq");
+        ALOGV("packet_queue_abort videoq");
         packet_queue_abort(&mVideoQ);
         /* wait until the end */
         while (!mAbortRequest && !mVideoEOSReceived) {
-            LOGV("wait for video received");
-            NamDelay(10);
+            ALOGV("wait for video received");
+            usleep(10000);
         }
-        LOGV("packet_queue_end videoq");
+        ALOGV("packet_queue_end videoq");
         packet_queue_end(&mVideoQ);
         break;
     case AVMEDIA_TYPE_AUDIO:
-        LOGV("packet_queue_abort audioq");
+        ALOGV("packet_queue_abort audioq");
         packet_queue_abort(&mAudioQ);
         while (!mAbortRequest && !mAudioEOSReceived) {
-            LOGV("wait for audio received");
-            NamDelay(10);
+            ALOGV("wait for audio received");
+            usleep(10000);
         }
-        LOGV("packet_queue_end audioq");
+        ALOGV("packet_queue_end audioq");
         packet_queue_end(&mAudioQ);
         break;
     case AVMEDIA_TYPE_SUBTITLE:
@@ -885,32 +1021,44 @@ int FFmpegExtractor::decode_interrupt_cb(void *ctx)
     return extrator->mAbortRequest;
 }
 
-void FFmpegExtractor::print_error_ex(const char *filename, int err)
+void FFmpegExtractor::buildFileName(const sp<DataSource> &source)
 {
-    char errbuf[128];
-    const char *errbuf_ptr = errbuf;
-
-    if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
-        errbuf_ptr = strerror(AVUNERROR(err));
-    LOGI("%s: %s\n", filename, errbuf_ptr);
+#if 1
+    ALOGI("android-source:%p", source.get());
+    // pass the addr of smart pointer("source")
+    snprintf(mFilename, sizeof(mFilename), "android-source:%p", source.get());
+    ALOGI("build mFilename: %s", mFilename);
+#else
+    const char *url = mDataSource->getNamURI();
+    if (url == NULL) {
+        ALOGI("url is error!");
+        return;
+    }
+    // is it right?
+    if (!strcmp(url, "-")) {
+        av_strlcpy(mFilename, "pipe:", strlen("pipe:") + 1);
+    } else {
+        av_strlcpy(mFilename, url, strlen(url) + 1);
+    }
+    ALOGI("build url: %s, mFilename: %s", url, mFilename);
+#endif
 }
 
 void FFmpegExtractor::setFFmpegDefaultOpts()
 {
     mGenPTS       = 0;
-#if DIABLE_VIDEO
+#if DEBUG_DISABLE_VIDEO
     mVideoDisable = 1;
 #else
     mVideoDisable = 0;
 #endif
-#if DIABLE_AUDIO
+#if DEBUG_DISABLE_AUDIO
     mAudioDisable = 1;
 #else
     mAudioDisable = 0;
 #endif
-    mShowStatus   = 1;
+    mShowStatus   = 0;
     mSeekByBytes  = 0; /* seek by bytes 0=off 1=on -1=auto" */
-    mStartTime    = AV_NOPTS_VALUE;
     mDuration     = AV_NOPTS_VALUE;
     mSeekPos      = AV_NOPTS_VALUE;
     mAutoExit     = 1;
@@ -938,20 +1086,22 @@ void FFmpegExtractor::setFFmpegDefaultOpts()
 
 int FFmpegExtractor::initStreams()
 {
-    int err, i;
-    status_t status;
+    int err = 0;
+       int i = 0;
+    status_t status = UNKNOWN_ERROR;
     int eof = 0;
-    int ret = 0, audio_ret = 0, video_ret = 0;
+    int ret = 0, audio_ret = -1, video_ret = -1;
     int pkt_in_play_range = 0;
-    AVDictionaryEntry *t;
-    AVDictionary **opts;
-    int orig_nb_streams;
+    AVDictionaryEntry *t = NULL;
+    AVDictionary **opts = NULL;
+    int orig_nb_streams = 0;
     int st_index[AVMEDIA_TYPE_NB] = {0};
     int wanted_stream[AVMEDIA_TYPE_NB] = {0};
     st_index[AVMEDIA_TYPE_AUDIO]  = -1;
     st_index[AVMEDIA_TYPE_VIDEO]  = -1;
     wanted_stream[AVMEDIA_TYPE_AUDIO]  = -1;
     wanted_stream[AVMEDIA_TYPE_VIDEO]  = -1;
+    const char *mime = NULL;
 
     setFFmpegDefaultOpts();
 
@@ -960,23 +1110,30 @@ int FFmpegExtractor::initStreams()
         ret = -1;
         goto fail;
     }
+    mFFmpegInited = true;
 
     av_init_packet(&flush_pkt);
     flush_pkt.data = (uint8_t *)"FLUSH";
     flush_pkt.size = 0;
 
     mFormatCtx = avformat_alloc_context();
+       if (!mFormatCtx)
+       {
+        ALOGE("oom for alloc avformat context");
+        ret = -1;
+        goto fail;
+       }
     mFormatCtx->interrupt_callback.callback = decode_interrupt_cb;
     mFormatCtx->interrupt_callback.opaque = this;
-    LOGV("mFilename: %s", mFilename);
+    ALOGV("mFilename: %s", mFilename);
     err = avformat_open_input(&mFormatCtx, mFilename, NULL, &format_opts);
     if (err < 0) {
-        print_error_ex(mFilename, err);
+        ALOGE("%s: avformat_open_input failed, err:%s", mFilename, av_err2str(err));
         ret = -1;
         goto fail;
     }
     if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
-        LOGE("Option %s not found.\n", t->key);
+        ALOGE("Option %s not found.\n", t->key);
         //ret = AVERROR_OPTION_NOT_FOUND;
         ret = -1;
         goto fail;
@@ -990,7 +1147,7 @@ int FFmpegExtractor::initStreams()
 
     err = avformat_find_stream_info(mFormatCtx, opts);
     if (err < 0) {
-        LOGE("%s: could not find codec parameters\n", mFilename);
+        ALOGE("%s: could not find stream info, err:%s", mFilename, av_err2str(err));
         ret = -1;
         goto fail;
     }
@@ -998,29 +1155,17 @@ int FFmpegExtractor::initStreams()
         av_dict_free(&opts[i]);
     av_freep(&opts);
 
+    mime = findMatchingContainer(mFormatCtx->iformat->name);
+    CHECK(mime != NULL);
+    mMeta->setCString(kKeyMIMEType, mime);
+
     if (mFormatCtx->pb)
         mFormatCtx->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
 
     if (mSeekByBytes < 0)
         mSeekByBytes = !!(mFormatCtx->iformat->flags & AVFMT_TS_DISCONT);
 
-    /* if seeking requested, we execute it */
-    if (mStartTime != AV_NOPTS_VALUE) {
-        int64_t timestamp;
-
-        timestamp = mStartTime;
-        /* add the stream start time */
-        if (mFormatCtx->start_time != AV_NOPTS_VALUE)
-            timestamp += mFormatCtx->start_time;
-        ret = avformat_seek_file(mFormatCtx, -1, INT64_MIN, timestamp, INT64_MAX, 0);
-        if (ret < 0) {
-            LOGE("%s: could not seek to position %0.3f",
-                    mFilename, (double)timestamp / AV_TIME_BASE);
-            goto fail;
-        }
-    }
-
-    for (i = 0; i < mFormatCtx->nb_streams; i++)
+    for (i = 0; i < (int)mFormatCtx->nb_streams; i++)
         mFormatCtx->streams[i]->discard = AVDISCARD_ALL;
     if (!mVideoDisable)
         st_index[AVMEDIA_TYPE_VIDEO] =
@@ -1036,15 +1181,22 @@ int FFmpegExtractor::initStreams()
         av_dump_format(mFormatCtx, 0, mFilename, 0);
     }
 
-    if (mFormatCtx->duration != AV_NOPTS_VALUE) {
+    if (mFormatCtx->duration != AV_NOPTS_VALUE &&
+            mFormatCtx->start_time != AV_NOPTS_VALUE) {
         int hours, mins, secs, us;
-        secs = mFormatCtx->duration / AV_TIME_BASE;
-        us = mFormatCtx->duration % AV_TIME_BASE;
+
+        ALOGV("file startTime: %lld", mFormatCtx->start_time);
+
+        mDuration = mFormatCtx->duration;
+
+        secs = mDuration / AV_TIME_BASE;
+        us = mDuration % AV_TIME_BASE;
         mins = secs / 60;
         secs %= 60;
         hours = mins / 60;
         mins %= 60;
-        LOGI("the duration is %02d:%02d:%02d.%02d", hours, mins, secs, (100 * us) / AV_TIME_BASE);
+        ALOGI("the duration is %02d:%02d:%02d.%02d",
+            hours, mins, secs, (100 * us) / AV_TIME_BASE);
     }
 
     if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
@@ -1056,7 +1208,7 @@ int FFmpegExtractor::initStreams()
     }
 
     if ( audio_ret < 0 && video_ret < 0) {
-        LOGE("%s: could not open codecs\n", mFilename);
+        ALOGE("%s: could not open codecs\n", mFilename);
         ret = -1;
         goto fail;
     }
@@ -1073,11 +1225,13 @@ void FFmpegExtractor::deInitStreams()
         avformat_close_input(&mFormatCtx);
     }
 
-    deInitFFmpeg();
+    if (mFFmpegInited) {
+        deInitFFmpeg();
+    }
 }
 
 status_t FFmpegExtractor::startReaderThread() {
-    LOGV("Starting reader thread");
+    ALOGV("Starting reader thread");
     Mutex::Autolock autoLock(mLock);
 
     if (mReaderThreadStarted)
@@ -1089,17 +1243,17 @@ status_t FFmpegExtractor::startReaderThread() {
     pthread_create(&mReaderThread, &attr, ReaderWrapper, this);
     pthread_attr_destroy(&attr);
     mReaderThreadStarted = true;
-    LOGD("Reader thread started");
+    ALOGD("Reader thread started");
 
     return OK;
 }
 
 void FFmpegExtractor::stopReaderThread() {
-    LOGV("Stopping reader thread");
+    ALOGV("Stopping reader thread");
     Mutex::Autolock autoLock(mLock);
 
     if (!mReaderThreadStarted) {
-        LOGD("Reader thread have been stopped");
+        ALOGD("Reader thread have been stopped");
         return;
     }
 
@@ -1108,7 +1262,7 @@ void FFmpegExtractor::stopReaderThread() {
     void *dummy;
     pthread_join(mReaderThread, &dummy);
     mReaderThreadStarted = false;
-    LOGD("Reader thread stopped");
+    ALOGD("Reader thread stopped");
 }
 
 // static
@@ -1124,7 +1278,7 @@ void FFmpegExtractor::readerEntry() {
     int eof = 0;
     int pkt_in_play_range = 0;
 
-    LOGV("FFmpegExtractor::readerEntry");
+    ALOGV("FFmpegExtractor enter thread(readerEntry)");
 
     mVideoEOSReceived = false;
     mAudioEOSReceived = false;
@@ -1146,16 +1300,16 @@ void FFmpegExtractor::readerEntry() {
                  (mFormatCtx->pb && !strncmp(mFilename, "mmsh:", 5)))) {
             /* wait 10 ms to avoid trying to get another packet */
             /* XXX: horrible */
-            NamDelay(10);
+            usleep(10000);
             continue;
         }
 #endif
 
         if (mSeekReq) {
-            LOGV("readerEntry, mSeekReq: %d", mSeekReq);
+            ALOGV("readerEntry, mSeekReq: %d", mSeekReq);
             ret = avformat_seek_file(mFormatCtx, -1, INT64_MIN, mSeekPos, INT64_MAX, mSeekFlags);
             if (ret < 0) {
-                LOGE("%s: error while seeking", mFormatCtx->filename);
+                ALOGE("%s: error while seeking", mFormatCtx->filename);
             } else {
                 if (mAudioStreamIdx >= 0) {
                     packet_queue_flush(&mAudioQ);
@@ -1175,10 +1329,10 @@ void FFmpegExtractor::readerEntry() {
             || (   (mAudioQ   .size  > MIN_AUDIOQ_SIZE || mAudioStreamIdx < 0)
                 && (mVideoQ   .nb_packets > MIN_FRAMES || mVideoStreamIdx < 0))) {
 #if DEBUG_READ_ENTRY
-            LOGV("readerEntry, is full, fuck");
+            ALOGV("readerEntry, is full, fuck");
 #endif
             /* wait 10 ms */
-            NamDelay(10);
+            usleep(10000);
             continue;
         }
 
@@ -1197,19 +1351,13 @@ void FFmpegExtractor::readerEntry() {
                 pkt->stream_index = mAudioStreamIdx;
                 packet_queue_put(&mAudioQ, pkt);
             }
-            NamDelay(10);
+            usleep(10000);
 #if DEBUG_READ_ENTRY
-            LOGV("readerEntry, eof = 1, mVideoQ.size: %d, mVideoQ.nb_packets: %d, mAudioQ.size: %d, mAudioQ.nb_packets: %d",
+            ALOGV("readerEntry, eof = 1, mVideoQ.size: %d, mVideoQ.nb_packets: %d, mAudioQ.size: %d, mAudioQ.nb_packets: %d",
                     mVideoQ.size, mVideoQ.nb_packets, mAudioQ.size, mAudioQ.nb_packets);
 #endif
             if (mAudioQ.size + mVideoQ.size  == 0) {
-                if (mLoop != 1 && (!mLoop || --mLoop)) {
-                    if (mVideoStreamIdx >= 0) {
-                        stream_seek(mStartTime != AV_NOPTS_VALUE ? mStartTime : 0, AVMEDIA_TYPE_VIDEO);
-                    } else if (mAudioStreamIdx >= 0) {
-                        stream_seek(mStartTime != AV_NOPTS_VALUE ? mStartTime : 0, AVMEDIA_TYPE_AUDIO);
-                    }
-                } else if (mAutoExit) {
+                if (mAutoExit) {
                     ret = AVERROR_EOF;
                     goto fail;
                 }
@@ -1223,19 +1371,19 @@ void FFmpegExtractor::readerEntry() {
         if (ret < 0) {
             if (ret == AVERROR_EOF || url_feof(mFormatCtx->pb))
                 if (ret == AVERROR_EOF) {
-                    //LOGV("ret == AVERROR_EOF");
+                    //ALOGV("ret == AVERROR_EOF");
                }
                 if (url_feof(mFormatCtx->pb)) {
-                    //LOGV("url_feof(mFormatCtx->pb)");
+                    //ALOGV("url_feof(mFormatCtx->pb)");
                }
 
                 eof = 1;
                 mEOF = true;
             if (mFormatCtx->pb && mFormatCtx->pb->error) {
-                LOGE("mFormatCtx->pb->error: %d", mFormatCtx->pb->error);
+                ALOGE("mFormatCtx->pb->error: %d", mFormatCtx->pb->error);
                 break;
             }
-            NamDelay(100);
+            usleep(100000);
             continue;
         }
 
@@ -1264,9 +1412,9 @@ void FFmpegExtractor::readerEntry() {
 
                 stream_component_open(mVideoStreamIdx);
                 if (!mDefersToCreateVideoTrack)
-                    LOGI("probe packet counter: %d when create video track ok", mProbePkts);
+                    ALOGI("probe packet counter: %d when create video track ok", mProbePkts);
                 if (mProbePkts == EXTRACTOR_MAX_PROBE_PACKETS)
-                    LOGI("probe packet counter to max: %d, create video track: %d",
+                    ALOGI("probe packet counter to max: %d, create video track: %d",
                         mProbePkts, !mDefersToCreateVideoTrack);
             }
         } else if (pkt->stream_index == mAudioStreamIdx) {
@@ -1294,22 +1442,16 @@ void FFmpegExtractor::readerEntry() {
                 }
                 stream_component_open(mAudioStreamIdx);
                 if (!mDefersToCreateAudioTrack)
-                    LOGI("probe packet counter: %d when create audio track ok", mProbePkts);
+                    ALOGI("probe packet counter: %d when create audio track ok", mProbePkts);
                 if (mProbePkts == EXTRACTOR_MAX_PROBE_PACKETS)
-                    LOGI("probe packet counter to max: %d, create audio track: %d",
+                    ALOGI("probe packet counter to max: %d, create audio track: %d",
                         mProbePkts, !mDefersToCreateAudioTrack);
             }
         }
 
-        /* check if packet is in play range specified by user, then queue, otherwise discard */
-        pkt_in_play_range = mDuration == AV_NOPTS_VALUE ||
-                (pkt->pts - mFormatCtx->streams[pkt->stream_index]->start_time) *
-                av_q2d(mFormatCtx->streams[pkt->stream_index]->time_base) -
-                (double)(mStartTime != AV_NOPTS_VALUE ? mStartTime : 0) / 1000000
-                <= ((double)mDuration / 1000000);
-        if (pkt->stream_index == mAudioStreamIdx && pkt_in_play_range) {
+        if (pkt->stream_index == mAudioStreamIdx) {
             packet_queue_put(&mAudioQ, pkt);
-        } else if (pkt->stream_index == mVideoStreamIdx && pkt_in_play_range) {
+        } else if (pkt->stream_index == mVideoStreamIdx) {
             packet_queue_put(&mVideoQ, pkt);
         } else {
             av_free_packet(pkt);
@@ -1317,12 +1459,12 @@ void FFmpegExtractor::readerEntry() {
     }
     /* wait until the end */
     while (!mAbortRequest) {
-        NamDelay(100);
+        usleep(100000);
     }
 
     ret = 0;
 fail:
-    LOGI("reader thread goto end...");
+    ALOGI("reader thread goto end...");
 
     /* close each stream */
     if (mAudioStreamIdx >= 0)
@@ -1332,13 +1474,15 @@ fail:
     if (mFormatCtx) {
         avformat_close_input(&mFormatCtx);
     }
+
+    ALOGV("FFmpegExtractor exit thread(readerEntry)");
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 
 FFmpegExtractor::Track::Track(
-        const sp<FFmpegExtractor> &extractor, sp<MetaData> meta, bool isAVC,
-          AVStream *stream, PacketQueue *queue)
+        FFmpegExtractor *extractor, sp<MetaData> meta, bool isAVC,
+        AVStream *stream, PacketQueue *queue)
     : mExtractor(extractor),
       mMeta(meta),
       mIsAVC(isAVC),
@@ -1364,27 +1508,36 @@ FFmpegExtractor::Track::Track(
             // The number of bytes used to encode the length of a NAL unit.
             mNALLengthSize = 1 + (ptr[4] & 3);
 
-            LOGV("the stream is AVC, the length of a NAL unit: %d", mNALLengthSize);
+            ALOGV("the stream is AVC, the length of a NAL unit: %d", mNALLengthSize);
 
             mNal2AnnexB = true;
         }
     }
 
     mMediaType = mStream->codec->codec_type;
+    mFirstKeyPktTimestamp = AV_NOPTS_VALUE;
 }
 
 FFmpegExtractor::Track::~Track() {
+    ALOGV("FFmpegExtractor::Track::~Track %s",
+            av_get_media_type_string(mMediaType));
+       mExtractor = NULL;
+       mMeta = NULL;
 }
 
 status_t FFmpegExtractor::Track::start(MetaData *params) {
+    ALOGV("FFmpegExtractor::Track::start %s",
+            av_get_media_type_string(mMediaType));
     Mutex::Autolock autoLock(mLock);
     //mExtractor->startReaderThread();
     return OK;
 }
 
 status_t FFmpegExtractor::Track::stop() {
+    ALOGV("FFmpegExtractor::Track::stop %s",
+            av_get_media_type_string(mMediaType));
     Mutex::Autolock autoLock(mLock);
-    mExtractor->stopReaderThread();
+    //mExtractor->stopReaderThread();
     return OK;
 }
 
@@ -1406,12 +1559,17 @@ status_t FFmpegExtractor::Track::read(
     ReadOptions::SeekMode mode;
     int64_t pktTS = AV_NOPTS_VALUE;
     int64_t seekTimeUs = AV_NOPTS_VALUE;
-    int64_t timeUs;
-    int key;
+    int64_t timeUs = AV_NOPTS_VALUE;
+    int key = 0;
     status_t status = OK;
 
     if (options && options->getSeekTo(&seekTimeUs, &mode)) {
-        LOGV("~~~%s seekTimeUs: %lld, mode: %d", av_get_media_type_string(mMediaType), seekTimeUs, mode);
+        ALOGV("~~~%s seekTimeUs: %lld, mode: %d", av_get_media_type_string(mMediaType), seekTimeUs, mode);
+        /* add the stream start time */
+        if (mStream->start_time != AV_NOPTS_VALUE)
+            seekTimeUs += mStream->start_time * av_q2d(mStream->time_base) * 1000000;
+        ALOGV("~~~%s seekTimeUs[+startTime]: %lld, mode: %d", av_get_media_type_string(mMediaType), seekTimeUs, mode);
+
         if (mExtractor->stream_seek(seekTimeUs, mMediaType) == SEEK)
             seeking = true;
     }
@@ -1435,29 +1593,53 @@ retry:
     }
 
     if (pkt.data == flush_pkt.data) {
-        LOGV("read %s flush pkt", av_get_media_type_string(mMediaType));
+        ALOGV("read %s flush pkt", av_get_media_type_string(mMediaType));
         av_free_packet(&pkt);
+        mFirstKeyPktTimestamp = AV_NOPTS_VALUE;
         goto retry;
     } else if (pkt.data == NULL && pkt.size == 0) {
-        LOGV("read %s eos pkt", av_get_media_type_string(mMediaType));
+        ALOGD("read %s eos pkt", av_get_media_type_string(mMediaType));
         av_free_packet(&pkt);
         mExtractor->reachedEOS(mMediaType);
-       return ERROR_END_OF_STREAM;
+           return ERROR_END_OF_STREAM;
     }
 
     key = pkt.flags & AV_PKT_FLAG_KEY ? 1 : 0;
+    pktTS = pkt.pts; //FIXME AV_NOPTS_VALUE??
+
+    //use dts when AVI
+    if (pkt.pts == AV_NOPTS_VALUE)
+        pktTS = pkt.dts;
+
+    //FIXME, drop, omxcodec requires a positive timestamp! e.g. vorbis
+    if (pktTS != AV_NOPTS_VALUE && pktTS < 0) {
+        ALOGW("drop the packet with negative timestamp(pts:%lld)", pktTS);
+        av_free_packet(&pkt);
+        goto retry;
+    }
 
     if (waitKeyPkt) {
         if (!key) {
-            LOGV("drop the no key packet");
+            ALOGV("drop the non-key packet");
             av_free_packet(&pkt);
             goto retry;
         } else {
-            LOGV("~~~~~~ got the key packet");
+            ALOGV("~~~~~~ got the key packet");
             waitKeyPkt = false;
         }
     }
+
+    if (pktTS != AV_NOPTS_VALUE && mFirstKeyPktTimestamp == AV_NOPTS_VALUE) {
+        // update the first key timestamp
+        mFirstKeyPktTimestamp = pktTS;
+    }
      
+    if (pktTS != AV_NOPTS_VALUE && pktTS < mFirstKeyPktTimestamp) {
+            ALOGV("drop the packet with the backward timestamp, maybe they are B-frames after I-frame ^_^");
+            av_free_packet(&pkt);
+            goto retry;
+    }
+
     MediaBuffer *mediaBuffer = new MediaBuffer(pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
     mediaBuffer->meta_data()->clear();
     mediaBuffer->set_range(0, pkt.size);
@@ -1484,8 +1666,8 @@ retry:
                     status = ERROR_MALFORMED;
                     break;
                 }
-               dst += mNALLengthSize;
-               ptr += mNALLengthSize;
+                               dst += mNALLengthSize;
+                               ptr += mNALLengthSize;
                 len -= mNALLengthSize;
 
                 memcpy(dst, ptr, nal_len);
@@ -1499,7 +1681,7 @@ retry:
         }
 
         if (status != OK) {
-            LOGV("status != OK");
+            ALOGV("status != OK");
             mediaBuffer->release();
             mediaBuffer = NULL;
             av_free_packet(&pkt);
@@ -1509,30 +1691,19 @@ retry:
         memcpy(mediaBuffer->data(), pkt.data, pkt.size);
     }
 
-    pktTS = pkt.pts;
-    // use dts when AVI
-    if (pkt.pts == AV_NOPTS_VALUE)
-        pktTS = pkt.dts;
-
-#if 0
-    // TODO, Stagefright can't handle negative timestamps
-    // if needed, work around this by offsetting them manually?
-    if (pktTS < 0)
-        pktTS = 0;
-#endif
-
-    timeUs = (int64_t)(pktTS * av_q2d(mStream->time_base) * 1000000);
-
-#if 0
-    LOGV("read %s pkt, size: %d, key: %d, pts: %lld, dts: %lld, timeUs: %llu us (%.2f secs)",
-        av_get_media_type_string(mMediaType), pkt.size, key, pkt.pts, pkt.dts, timeUs, timeUs/1E6);
-#endif
+    int64_t start_time = mStream->start_time != AV_NOPTS_VALUE ? mStream->start_time : 0;
+    if (pktTS != AV_NOPTS_VALUE)
+        timeUs = (int64_t)((pktTS - start_time) * av_q2d(mStream->time_base) * 1000000);
+    else
+        timeUs = SF_NOPTS_VALUE; //FIXME AV_NOPTS_VALUE is negative, but stagefright need positive
 
-#if 0
-    // TODO, Stagefright can't handle negative timestamps
-    // if needed, work around this by offsetting them manually?
-    if (timeUs < 0)
-        timeUs = 0;
+#if DEBUG_PKT
+    if (pktTS != AV_NOPTS_VALUE)
+        ALOGV("read %s pkt, size:%d, key:%d, pts:%lld, dts:%lld, timeUs[-startTime]:%lld us (%.2f secs)",
+            av_get_media_type_string(mMediaType), pkt.size, key, pkt.pts, pkt.dts, timeUs, timeUs/1E6);
+    else
+        ALOGV("read %s pkt, size:%d, key:%d, pts:N/A, dts:N/A, timeUs[-startTime]:N/A",
+            av_get_media_type_string(mMediaType), pkt.size, key);
 #endif
 
     mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs);
@@ -1547,151 +1718,336 @@ retry:
 
 ////////////////////////////////////////////////////////////////////////////////
 
-// LegacySniffFFMPEG
 typedef struct {
-    const char *extension;
+    const char *format;
     const char *container;
-} extmap;
-
-static extmap FILE_EXTS[] = {
-        {".mp4", MEDIA_MIMETYPE_CONTAINER_MPEG4},
-        {".3gp", MEDIA_MIMETYPE_CONTAINER_MPEG4},
-        {".mp3", MEDIA_MIMETYPE_AUDIO_MPEG},
-        {".mov", MEDIA_MIMETYPE_CONTAINER_MOV},
-        {".mkv", MEDIA_MIMETYPE_CONTAINER_MATROSKA},
-        {".ts",  MEDIA_MIMETYPE_CONTAINER_TS},
-        {".avi", MEDIA_MIMETYPE_CONTAINER_AVI},
-        {".asf", MEDIA_MIMETYPE_CONTAINER_ASF},
-#if 0
-        {".wmv", MEDIA_MIMETYPE_CONTAINER_WMV},
-        {".wma", MEDIA_MIMETYPE_CONTAINER_WMA},
-        {".mpg", MEDIA_MIMETYPE_CONTAINER_MPG},
-        {".flv", MEDIA_MIMETYPE_CONTAINER_FLV},
-        {".divx", MEDIA_MIMETYPE_CONTAINER_DIVX},
-        {".mp2", MEDIA_MIMETYPE_CONTAINER_MP2},
-        {".ape", MEDIA_MIMETYPE_CONTAINER_APE},
-        {".rm ", MEDIA_MIMETYPE_CONTAINER_RM},
-        {".ra",  MEDIA_MIMETYPE_CONTAINER_RA},
-#endif
+} formatmap;
+
+static formatmap FILE_FORMATS[] = {
+        {"mpeg",                    MEDIA_MIMETYPE_CONTAINER_MPEG2PS  },
+        {"mpegts",                  MEDIA_MIMETYPE_CONTAINER_TS       },
+        {"mov,mp4,m4a,3gp,3g2,mj2", MEDIA_MIMETYPE_CONTAINER_MPEG4    },
+        {"matroska,webm",           MEDIA_MIMETYPE_CONTAINER_MATROSKA },
+        {"asf",                     MEDIA_MIMETYPE_CONTAINER_ASF      },
+        {"rm",                      MEDIA_MIMETYPE_CONTAINER_RM       },
+        {"flv",                     MEDIA_MIMETYPE_CONTAINER_FLV      },
+        {"swf",                     MEDIA_MIMETYPE_CONTAINER_FLV      },
+        {"avi",                     MEDIA_MIMETYPE_CONTAINER_AVI      },
+        {"ape",                     MEDIA_MIMETYPE_CONTAINER_APE      },
+        {"dts",                     MEDIA_MIMETYPE_CONTAINER_DTS      },
+        {"flac",                    MEDIA_MIMETYPE_CONTAINER_FLAC     },
+        {"ac3",                     MEDIA_MIMETYPE_AUDIO_AC3          },
+        {"wav",                     MEDIA_MIMETYPE_CONTAINER_WAV      },
+        {"ogg",                     MEDIA_MIMETYPE_CONTAINER_OGG      },
+        {"hevc",                     MEDIA_MIMETYPE_CONTAINER_HEVC    },
 };
 
-const char *LegacySniffFFMPEG(const char * uri)
+static void adjustMPEG4Confidence(AVFormatContext *ic, float *confidence)
 {
-    size_t i;
-    const char *container = NULL;
+       AVDictionary *tags = NULL;
+       AVDictionaryEntry *tag = NULL;
+
+       tags = ic->metadata;
+
+       //NOTE: You can use command to show these tags,
+       //e.g. "ffprobe -show_format 2012.mov"
+
+       tag = av_dict_get(tags, "major_brand", NULL, 0);
+       if (!tag) {
+               return;
+       }
+
+       ALOGV("major_brand tag is:%s", tag->value);
+
+       //when MEDIA_MIMETYPE_CONTAINER_MPEG4
+       //WTF, MPEG4Extractor.cpp can not extractor mov format
+       //NOTE: isCompatibleBrand(MPEG4Extractor.cpp)
+       //  Won't promise that the following file types can be played.
+       //  Just give these file types a chance.
+       //  FOURCC('q', 't', ' ', ' '),  // Apple's QuickTime
+       //So......
+       if (!strcmp(tag->value, "qt  ")) {
+               ALOGI("format is mov, confidence should be larger than mpeg4");
+               //the MEDIA_MIMETYPE_CONTAINER_MPEG4 of confidence is 0.4f
+               *confidence = 0.41f;
+       }
+}
 
-    LOGI("list the file extensions suppoted by ffmpeg: ");
-    LOGI("========================================");
-    for (i = 0; i < NELEM(FILE_EXTS); ++i) {
-            LOGV("file_exts[%02d]: %s", i, FILE_EXTS[i].extension);
-    }
-    LOGI("========================================");
-
-    int lenURI = strlen(uri);
-    for (i = 0; i < NELEM(FILE_EXTS); ++i) {
-        int len = strlen(FILE_EXTS[i].extension);
-        int start = lenURI - len;
-        if (start > 0) {
-            if (!av_strncasecmp(uri + start, FILE_EXTS[i].extension, len)) {
-                container = FILE_EXTS[i].container;
-                break;
-            }
-        }
-    }
+static void adjustVideoCodecConfidence(AVFormatContext *ic,
+               enum AVCodecID codec_id, float *confidence)
+{
+       //add to here
+}
 
-    return container;
+//TODO. if the other stream(e.g. mp3) is supported by stagefright
+static void adjustAudioCodecConfidence(AVFormatContext *ic,
+               enum AVCodecID codec_id, float *confidence)
+{
+       switch (codec_id) {
+       case AV_CODEC_ID_AC3:
+               ALOGI("ffmpeg can demux ac3 only");
+               *confidence = 0.88f;
+               break;
+       case AV_CODEC_ID_MP1:
+       case AV_CODEC_ID_MP2:
+               //TODO. if the other stream(e.g. mp3) is supported by stagefright
+               ALOGI("ffmpeg can demux mp1 and mp2 only");
+               *confidence = 0.88f;
+               break;
+       default:
+               break;
+       }
 }
 
-// BetterSniffFFMPEG
-typedef struct {
-    const char *format;
-    const char *container;
-} formatmap;
+static void adjustCodecConfidence(AVFormatContext *ic, float *confidence)
+{
+       unsigned int idx = 0;
+       AVCodecContext *avctx = NULL;
+       AVMediaType     codec_type = AVMEDIA_TYPE_UNKNOWN;
+       enum AVCodecID codec_id = AV_CODEC_ID_NONE;
+       bool haveVideo = false;
+       bool haveAudio = false;
+       bool haveMP3 = false;
+
+       for (idx = 0; idx < ic->nb_streams; idx++) {
+               avctx = ic->streams[idx]->codec;
+               codec_type = avctx->codec_type;
+               codec_id = avctx->codec_id;
+
+               if (codec_type == AVMEDIA_TYPE_VIDEO) {
+                       haveVideo = true;
+                       adjustVideoCodecConfidence(ic, codec_id, confidence);
+               } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
+                       haveAudio = true;
+                       adjustAudioCodecConfidence(ic, codec_id, confidence);
+                       if (codec_id == AV_CODEC_ID_MP3)
+                               haveMP3 = true;
+               }
+       }
 
-static formatmap FILE_FORMATS[] = {
-        {"mpegts",                  MEDIA_MIMETYPE_CONTAINER_TS},
-        {"mov,mp4,m4a,3gp,3g2,mj2", MEDIA_MIMETYPE_CONTAINER_MOV},
-        {"asf",                     MEDIA_MIMETYPE_CONTAINER_ASF},
-};
+       if (haveVideo && haveMP3) {
+               *confidence = 0.22f; // larger than MP3Extractor an MP3Extractor
+       }
+}
 
-const char *BetterSniffFFMPEG(const char * uri)
+static void adjustConfidenceIfNeeded(const char *mime,
+               AVFormatContext *ic, float *confidence)
 {
-    size_t i;
-    const char *container = NULL;
-    AVFormatContext *ic = NULL;
-
-    status_t status = initFFmpeg();
-    if (status != OK) {
-        LOGE("could not init ffmpeg");
-        return false;
-    }
+       //check mime
+       if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4)) {
+               adjustMPEG4Confidence(ic, confidence);
+       } else {
+               //add to here;
+       }
+
+       //check codec
+       adjustCodecConfidence(ic, confidence);
+}
 
-    ic = avformat_alloc_context();
-    avformat_open_input(&ic, uri, NULL, NULL);
+static const char *findMatchingContainer(const char *name)
+{
+       size_t i = 0;
+       const char *container = NULL;
+
+       ALOGI("list the formats suppoted by ffmpeg: ");
+       ALOGI("========================================");
+       for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
+               ALOGV("format_names[%02d]: %s", i, FILE_FORMATS[i].format);
+       }
+       ALOGI("========================================");
+
+       for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
+               int len = strlen(FILE_FORMATS[i].format);
+               if (!av_strncasecmp(name, FILE_FORMATS[i].format, len)) {
+                       container = FILE_FORMATS[i].container;
+                       break;
+               }
+       }
 
-    av_dump_format(ic, 0, uri, 0);
+       return container;
+}
 
-    LOGI("FFmpegExtrator, uri: %s, format_name: %s, format_long_name: %s", uri, ic->iformat->name, ic->iformat->long_name);
+static const char *SniffFFMPEGCommon(const char *url, float *confidence)
+{
+       size_t i = 0;
+       int err = 0;
+       const char *container = NULL;
+       AVFormatContext *ic = NULL;
+       AVDictionary **opts = NULL;
+       size_t orig_nb_streams = 0;
+
+       status_t status = initFFmpeg();
+       if (status != OK) {
+               ALOGE("could not init ffmpeg");
+               return NULL;
+       }
+
+       ic = avformat_alloc_context();
+       if (!ic)
+       {
+               ALOGE("oom for alloc avformat context");
+               goto fail;
+       }
+
+       err = avformat_open_input(&ic, url, NULL, NULL);
+       if (err < 0) {
+        ALOGE("%s: avformat_open_input failed, err:%s", url, av_err2str(err));
+               goto fail;
+       }
+
+       opts = setup_find_stream_info_opts(ic, codec_opts);
+       orig_nb_streams = ic->nb_streams;
+       err = avformat_find_stream_info(ic, opts);
+       if (err < 0) {
+        ALOGE("%s: could not find stream info, err:%s", url, av_err2str(err));
+               goto fail;
+       }
+       for (i = 0; i < orig_nb_streams; i++) {
+               av_dict_free(&opts[i]);
+       }
+       av_freep(&opts);
+
+       av_dump_format(ic, 0, url, 0);
+
+       ALOGI("FFmpegExtrator, url: %s, format_name: %s, format_long_name: %s",
+                       url, ic->iformat->name, ic->iformat->long_name);
+
+       container = findMatchingContainer(ic->iformat->name);
+
+       if (container) {
+               adjustConfidenceIfNeeded(container, ic, confidence);
+       }
 
-    LOGI("list the format suppoted by ffmpeg: ");
-    LOGI("========================================");
-    for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
-            LOGV("format_names[%02d]: %s", i, FILE_FORMATS[i].format);
-    }
-    LOGI("========================================");
+fail:
+       if (ic) {
+               avformat_close_input(&ic);
+               av_free(ic);
+       }
+       if (status == OK) {
+               deInitFFmpeg();
+       }
+
+       return container;
+}
 
-    for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
-        int len = strlen(FILE_FORMATS[i].format);
-        if (!av_strncasecmp(ic->iformat->name, FILE_FORMATS[i].format, len)) {
-            container = FILE_FORMATS[i].container;
-            break;
-        }
-    }
+static const char *LegacySniffFFMPEG(const sp<DataSource> &source, float *confidence)
+{
+       String8 uri = source->getUri();
+       if (uri.empty()) {
+               return NULL;
+       }
 
-    avformat_close_input(&ic);
-    av_free(ic);
+       ALOGI("source url:%s", uri.string());
 
-    return container;
+       return SniffFFMPEGCommon(uri.string(), confidence);
 }
 
-bool SniffFFMPEG(
-        const sp<DataSource> &source, String8 *mimeType, float *confidence,
-        sp<AMessage> *meta) {
-    LOGV("SniffFFMPEG");
-    const char *uri, *container = NULL;
-
-    uri = source->getNamURI();
+static const char *BetterSniffFFMPEG(const sp<DataSource> &source, float *confidence)
+{
+       char url[128] = {0};
 
-    if (!uri)
-        return false;
+       ALOGI("android-source:%p", source.get());
 
-    LOGI("ffmpeg uri: %s", uri);
+       // pass the addr of smart pointer("source")
+       snprintf(url, sizeof(url), "android-source:%p", source.get());
 
-    container = BetterSniffFFMPEG(uri);
-    if (!container) {
-        LOGW("sniff through LegacySniffFFMPEG, only check the file extension");
-        container = LegacySniffFFMPEG(uri);
-    }
+       return SniffFFMPEGCommon(url, confidence);
+}
 
-    if (container == NULL)
-        return false;
+bool SniffFFMPEG(
+        const sp<DataSource> &source, String8 *mimeType, float *confidence,
+        sp<AMessage> *meta) {
+       ALOGV("SniffFFMPEG");
 
-    LOGV("found container: %s", container);
+       *confidence = 0.08f;  // be the last resort, by default
 
-    *confidence = 0.88f;  // Slightly larger than other extractor's confidence
-    mimeType->setTo(container);
+       const char *container = BetterSniffFFMPEG(source, confidence);
+       if (!container) {
+               ALOGW("sniff through BetterSniffFFMPEG failed, try LegacySniffFFMPEG");
+               container = LegacySniffFFMPEG(source, confidence);
+               if (container) {
+                       ALOGI("sniff through LegacySniffFFMPEG success");
+               }
+       } else {
+               ALOGI("sniff through BetterSniffFFMPEG success");
+       }
+
+       if (container == NULL) {
+               ALOGD("SniffFFMPEG failed to sniff this source");
+               return false;
+       }
+
+       ALOGD("ffmpeg detected media content as '%s' with confidence %.2f",
+                       container, *confidence);
+
+       /* use MPEG4Extractor(not extended extractor) for HTTP source only */
+       if (!strcasecmp(container, MEDIA_MIMETYPE_CONTAINER_MPEG4)
+                       && (source->flags() & DataSource::kIsCachingDataSource)) {
+               ALOGI("support container: %s, but it is caching data source, "
+                               "Don't use ffmpegextractor", container);
+               return false;
+       }
+
+       mimeType->setTo(container);
+
+       *meta = new AMessage;
+       (*meta)->setString("extended-extractor", "extended-extractor");
+       (*meta)->setString("extended-extractor-subtype", "ffmpegextractor");
+
+       //debug only
+       char value[PROPERTY_VALUE_MAX];
+       property_get("sys.media.parser.ffmpeg", value, "0");
+       if (atoi(value)) {
+               ALOGI("[debug] parser use ffmpeg");
+               *confidence = 0.88f;
+       }
+
+       if (*confidence > 0.08f) {
+               (*meta)->setString("extended-extractor-use", "ffmpegextractor");
+       }
+
+       return true;
+}
 
-    /* use MPEG4Extractor(not extended extractor) for HTTP source only */
-    if (!av_strcasecmp(container, MEDIA_MIMETYPE_CONTAINER_MPEG4)
-            && (source->flags() & DataSource::kIsCachingDataSource)) {
-            return true;
+MediaExtractor *CreateFFmpegExtractor(const sp<DataSource> &source, const char *mime, const sp<AMessage> &meta) {
+    MediaExtractor *ret = NULL;
+    AString notuse;
+    if (meta.get() && meta->findString("extended-extractor", &notuse) && (
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4)     ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG)          ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AC3)           ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MOV)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MATROSKA)  ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_TS)        ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2PS)   ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_AVI)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_ASF)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WEBM)      ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WMV)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPG)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_FLV)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_DIVX)      ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_RM)        ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_FLAC)      ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_APE)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_DTS)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MP2)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_RA)        ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_OGG)       ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_HEVC)      ||
+            !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WMA))) {
+        ret = new FFmpegExtractor(source);
     }
 
-    *meta = new AMessage;
-    (*meta)->setString("extended-extractor", "extended-extractor");
-    (*meta)->setString("extended-extractor-subtype", "ffmpegextractor");
-
-    return true;
+    ALOGD("%ssupported mime: %s", (ret ? "" : "un"), mime);
+    return ret;
 }
 
 }  // namespace android
+
+extern "C" void getExtractorPlugin(android::MediaExtractor::Plugin *plugin)
+{
+    plugin->sniff = android::SniffFFMPEG;
+    plugin->create = android::CreateFFmpegExtractor;
+}