OSDN Git Service

add HEVC(H.265) decoder. plz sync:
[android-x86/external-stagefright-plugins.git] / libstagefright / FFmpegExtractor / FFmpegExtractor.cpp
1 /*
2  * Copyright 2012 Michael Chen <omxcodec@gmail.com>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define LOG_NDEBUG 0
18 #define LOG_TAG "FFmpegExtractor"
19 #include <utils/Log.h>
20
21 #include <stdint.h>
22 #include <limits.h> /* INT_MAX */
23 #include <inttypes.h>
24
25 #include <media/stagefright/foundation/ABitReader.h>
26 #include <media/stagefright/foundation/ABuffer.h>
27 #include <media/stagefright/foundation/ADebug.h>
28 #include <media/stagefright/foundation/AMessage.h>
29 #include <media/stagefright/foundation/hexdump.h>
30 #include <media/stagefright/DataSource.h>
31 #include <media/stagefright/MediaBuffer.h>
32 #include <media/stagefright/foundation/ADebug.h>
33 #include <media/stagefright/MediaDefs.h>
34 #include <media/stagefright/MediaErrors.h>
35 #include <media/stagefright/MediaSource.h>
36 #include <media/stagefright/MetaData.h>
37 #include <media/stagefright/Utils.h>
38 #include <cutils/properties.h>
39 #include <utils/String8.h>
40 #include <utils/misc.h>
41
42 #include "include/avc_utils.h"
43 #include "utils/ffmpeg_utils.h"
44 #include "utils/ffmpeg_cmdutils.h"
45 #include "FFmpegExtractor.h"
46
47 #define MAX_QUEUE_SIZE (15 * 1024 * 1024)
48 #define MIN_AUDIOQ_SIZE (20 * 16 * 1024)
49 #define MIN_FRAMES 5
50 #define EXTRACTOR_MAX_PROBE_PACKETS 200
51 #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - FF_INPUT_BUFFER_PADDING_SIZE)
52
53 //debug
54 #define DEBUG_READ_ENTRY           0
55 #define DEBUG_DISABLE_VIDEO        0
56 #define DEBUG_DISABLE_AUDIO        0
57 #define WAIT_KEY_PACKET_AFTER_SEEK 1
58 #define DISABLE_NAL_TO_ANNEXB      0
59 #define DEBUG_PKT                  0
60
61 enum {
62     NO_SEEK = 0,
63     SEEK,
64 };
65
66 static AVPacket flush_pkt;
67
68 namespace android {
69
70 static const char *findMatchingContainer(const char *name);
71
72 struct FFmpegExtractor::Track : public MediaSource {
73     Track(FFmpegExtractor *extractor, sp<MetaData> meta, bool isAVC,
74           AVStream *stream, PacketQueue *queue);
75
76     virtual status_t start(MetaData *params);
77     virtual status_t stop();
78     virtual sp<MetaData> getFormat();
79
80     virtual status_t read(
81             MediaBuffer **buffer, const ReadOptions *options);
82
83 protected:
84     virtual ~Track();
85
86 private:
87     friend struct FFmpegExtractor;
88
89     FFmpegExtractor *mExtractor;
90     sp<MetaData> mMeta;
91
92     enum AVMediaType mMediaType;
93
94     mutable Mutex mLock;
95
96     bool mIsAVC;
97     size_t mNALLengthSize;
98     bool mNal2AnnexB;
99
100     AVStream *mStream;
101     PacketQueue *mQueue;
102
103     int64_t mFirstKeyPktTimestamp;
104
105     DISALLOW_EVIL_CONSTRUCTORS(Track);
106 };
107
108 ////////////////////////////////////////////////////////////////////////////////
109
110 FFmpegExtractor::FFmpegExtractor(const sp<DataSource> &source)
111     : mDataSource(source),
112       mMeta(new MetaData),
113       mInitCheck(NO_INIT),
114       mFFmpegInited(false),
115       mFormatCtx(NULL),
116       mReaderThreadStarted(false) {
117     ALOGV("FFmpegExtractor::FFmpegExtractor");
118
119     buildFileName(source);
120
121     int err = initStreams();
122     if (err < 0) {
123         ALOGE("failed to init ffmpeg");
124         return;
125     }
126
127     // start reader here, as we want to extract extradata from bitstream if no extradata
128     startReaderThread();
129
130     while(mProbePkts <= EXTRACTOR_MAX_PROBE_PACKETS && !mEOF &&
131         (mFormatCtx->pb ? !mFormatCtx->pb->error : 1) &&
132         (mDefersToCreateVideoTrack || mDefersToCreateAudioTrack)) {
133         // FIXME, i am so lazy! Should use pthread_cond_wait to wait conditions
134         usleep(5000);
135     }
136
137     ALOGV("mProbePkts: %d, mEOF: %d, pb->error(if has): %d, mDefersToCreateVideoTrack: %d, mDefersToCreateAudioTrack: %d",
138         mProbePkts, mEOF, mFormatCtx->pb ? mFormatCtx->pb->error : 0, mDefersToCreateVideoTrack, mDefersToCreateAudioTrack);
139
140     mInitCheck = OK;
141 }
142
143 FFmpegExtractor::~FFmpegExtractor() {
144     ALOGV("FFmpegExtractor::~FFmpegExtractor");
145
146     // stop reader here if no track!
147     stopReaderThread();
148
149     deInitStreams();
150 }
151
152 size_t FFmpegExtractor::countTracks() {
153     return mInitCheck == OK ? mTracks.size() : 0;
154 }
155
156 sp<MediaSource> FFmpegExtractor::getTrack(size_t index) {
157     ALOGV("FFmpegExtractor::getTrack[%d]", index);
158
159     if (mInitCheck != OK) {
160         return NULL;
161     }
162
163     if (index >= mTracks.size()) {
164         return NULL;
165     }
166
167     return mTracks.valueAt(index);
168 }
169
170 sp<MetaData> FFmpegExtractor::getTrackMetaData(size_t index, uint32_t flags) {
171     ALOGV("FFmpegExtractor::getTrackMetaData[%d]", index);
172
173     if (mInitCheck != OK) {
174         return NULL;
175     }
176
177     if (index >= mTracks.size()) {
178         return NULL;
179     }
180
181     return mTracks.valueAt(index)->getFormat();
182 }
183
184 sp<MetaData> FFmpegExtractor::getMetaData() {
185     ALOGV("FFmpegExtractor::getMetaData");
186
187     if (mInitCheck != OK) {
188         return NULL;
189     }
190
191     return mMeta;
192 }
193
194 uint32_t FFmpegExtractor::flags() const {
195     ALOGV("FFmpegExtractor::flags");
196
197     if (mInitCheck != OK) {
198         return 0;
199     }
200
201     uint32_t flags = CAN_PAUSE;
202
203     if (mFormatCtx->duration != AV_NOPTS_VALUE) {
204         flags |= CAN_SEEK_BACKWARD | CAN_SEEK_FORWARD | CAN_SEEK;
205     }
206
207     return flags;
208 }
209
210 void FFmpegExtractor::packet_queue_init(PacketQueue *q)
211 {
212     memset(q, 0, sizeof(PacketQueue));
213     pthread_mutex_init(&q->mutex, NULL);
214     pthread_cond_init(&q->cond, NULL);
215     packet_queue_put(q, &flush_pkt);
216 }
217
218 void FFmpegExtractor::packet_queue_flush(PacketQueue *q)
219 {
220     AVPacketList *pkt, *pkt1;
221
222     pthread_mutex_lock(&q->mutex);
223     for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
224         pkt1 = pkt->next;
225         av_free_packet(&pkt->pkt);
226         av_freep(&pkt);
227     }
228     q->last_pkt = NULL;
229     q->first_pkt = NULL;
230     q->nb_packets = 0;
231     q->size = 0;
232     pthread_mutex_unlock(&q->mutex);
233 }
234
235 void FFmpegExtractor::packet_queue_end(PacketQueue *q)
236 {
237     packet_queue_flush(q);
238 }
239
240 void FFmpegExtractor::packet_queue_abort(PacketQueue *q)
241 {
242     pthread_mutex_lock(&q->mutex);
243
244     q->abort_request = 1;
245
246     pthread_cond_signal(&q->cond);
247
248     pthread_mutex_unlock(&q->mutex);
249 }
250
251 int FFmpegExtractor::packet_queue_put(PacketQueue *q, AVPacket *pkt)
252 {
253     AVPacketList *pkt1;
254
255     /* duplicate the packet */
256     if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
257         return -1;
258
259     pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
260     if (!pkt1)
261         return -1;
262     pkt1->pkt = *pkt;
263     pkt1->next = NULL;
264
265     pthread_mutex_lock(&q->mutex);
266
267     if (!q->last_pkt)
268
269         q->first_pkt = pkt1;
270     else
271         q->last_pkt->next = pkt1;
272     q->last_pkt = pkt1;
273     q->nb_packets++;
274     //q->size += pkt1->pkt.size + sizeof(*pkt1);
275     q->size += pkt1->pkt.size;
276     pthread_cond_signal(&q->cond);
277
278     pthread_mutex_unlock(&q->mutex);
279     return 0;
280 }
281
282 /* packet queue handling */
283 /* return < 0 if aborted, 0 if no packet and > 0 if packet.  */
284 int FFmpegExtractor::packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
285 {
286     AVPacketList *pkt1;
287     int ret;
288
289     pthread_mutex_lock(&q->mutex);
290
291     for (;;) {
292         if (q->abort_request) {
293             ret = -1;
294             break;
295         }
296
297         pkt1 = q->first_pkt;
298         if (pkt1) {
299             q->first_pkt = pkt1->next;
300             if (!q->first_pkt)
301                 q->last_pkt = NULL;
302             q->nb_packets--;
303             //q->size -= pkt1->pkt.size + sizeof(*pkt1);
304             q->size -= pkt1->pkt.size;
305             *pkt = pkt1->pkt;
306             av_free(pkt1);
307             ret = 1;
308             break;
309         } else if (!block) {
310             ret = 0;
311             break;
312         } else {
313             pthread_cond_wait(&q->cond, &q->mutex);
314         }
315     }
316     pthread_mutex_unlock(&q->mutex);
317     return ret;
318 }
319
320 static void EncodeSize14(uint8_t **_ptr, size_t size) {
321     CHECK_LE(size, 0x3fff);
322
323     uint8_t *ptr = *_ptr;
324
325     *ptr++ = 0x80 | (size >> 7);
326     *ptr++ = size & 0x7f;
327
328     *_ptr = ptr;
329 }
330
331 static sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) {
332     sp<ABuffer> esds = new ABuffer(csd->size() + 25);
333
334     uint8_t *ptr = esds->data();
335     *ptr++ = 0x03;
336     EncodeSize14(&ptr, 22 + csd->size());
337
338     *ptr++ = 0x00;  // ES_ID
339     *ptr++ = 0x00;
340
341     *ptr++ = 0x00;  // streamDependenceFlag, URL_Flag, OCRstreamFlag
342
343     *ptr++ = 0x04;
344     EncodeSize14(&ptr, 16 + csd->size());
345
346     *ptr++ = 0x40;  // Audio ISO/IEC 14496-3
347
348     for (size_t i = 0; i < 12; ++i) {
349         *ptr++ = 0x00;
350     }
351
352     *ptr++ = 0x05;
353     EncodeSize14(&ptr, csd->size());
354
355     memcpy(ptr, csd->data(), csd->size());
356
357     return esds;
358 }
359
360 // Returns the sample rate based on the sampling frequency index
361 static uint32_t get_sample_rate(const uint8_t sf_index)
362 {
363     static const uint32_t sample_rates[] =
364     {
365         96000, 88200, 64000, 48000, 44100, 32000,
366         24000, 22050, 16000, 12000, 11025, 8000
367     };
368
369     if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
370         return sample_rates[sf_index];
371     }
372
373     return 0;
374 }
375
376 int FFmpegExtractor::check_extradata(AVCodecContext *avctx)
377 {
378     enum AVCodecID codec_id = AV_CODEC_ID_NONE;
379     const char *name = NULL;
380     bool *defersToCreateTrack = NULL;
381     AVBitStreamFilterContext **bsfc = NULL;
382
383     // init
384     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
385         bsfc = &mVideoBsfc;
386         defersToCreateTrack = &mDefersToCreateVideoTrack;
387     } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO){
388         bsfc = &mAudioBsfc;
389         defersToCreateTrack = &mDefersToCreateAudioTrack;
390     }
391
392         codec_id = avctx->codec_id;
393
394     // ignore extradata
395     if (codec_id != AV_CODEC_ID_H264
396             && codec_id != AV_CODEC_ID_MPEG4
397             && codec_id != AV_CODEC_ID_MPEG1VIDEO
398             && codec_id != AV_CODEC_ID_MPEG2VIDEO
399             && codec_id != AV_CODEC_ID_AAC) {
400         return 1;
401     }
402
403     // is extradata compatible with android?
404     if (codec_id != AV_CODEC_ID_AAC) {
405         int is_compatible = is_extradata_compatible_with_android(avctx);
406         if (!is_compatible) {
407             ALOGI("%s extradata is not compatible with android, should to extract it from bitstream",
408                     av_get_media_type_string(avctx->codec_type));
409             *defersToCreateTrack = true;
410             *bsfc = NULL; // H264 don't need bsfc, only AAC?
411             return 0;
412         }
413         return 1;
414     }
415
416     if (codec_id == AV_CODEC_ID_AAC) {
417         name = "aac_adtstoasc";
418     }
419
420     if (avctx->extradata_size <= 0) {
421         ALOGI("No %s extradata found, should to extract it from bitstream",
422                 av_get_media_type_string(avctx->codec_type));
423         *defersToCreateTrack = true;
424          //CHECK(name != NULL);
425         if (!*bsfc && name) {
426             *bsfc = av_bitstream_filter_init(name);
427             if (!*bsfc) {
428                 ALOGE("Cannot open the %s BSF!", name);
429                 *defersToCreateTrack = false;
430                 return -1;
431             } else {
432                 ALOGV("open the %s bsf", name);
433                 return 0;
434             }
435         } else {
436             return 0;
437         }
438     }
439     return 1;
440 }
441
442 void FFmpegExtractor::printTime(int64_t time)
443 {
444     int hours, mins, secs, us;
445
446     if (time == AV_NOPTS_VALUE)
447         return;
448
449     secs = time / AV_TIME_BASE;
450     us = time % AV_TIME_BASE;
451     mins = secs / 60;
452     secs %= 60;
453     hours = mins / 60;
454     mins %= 60;
455     ALOGI("the time is %02d:%02d:%02d.%02d",
456         hours, mins, secs, (100 * us) / AV_TIME_BASE);
457 }
458
459 bool FFmpegExtractor::is_codec_supported(enum AVCodecID codec_id)
460 {
461     bool supported = false;
462
463     switch(codec_id) {
464     case AV_CODEC_ID_H264:
465     case AV_CODEC_ID_MPEG4:
466     case AV_CODEC_ID_H263:
467     case AV_CODEC_ID_H263P:
468     case AV_CODEC_ID_H263I:
469     case AV_CODEC_ID_AAC:
470     case AV_CODEC_ID_AC3:
471     case AV_CODEC_ID_MP2:
472     case AV_CODEC_ID_MP3:
473     case AV_CODEC_ID_MPEG1VIDEO:
474     case AV_CODEC_ID_MPEG2VIDEO:
475     case AV_CODEC_ID_WMV1:
476     case AV_CODEC_ID_WMV2:
477     case AV_CODEC_ID_WMV3:
478     case AV_CODEC_ID_VC1:
479     case AV_CODEC_ID_WMAV1:
480     case AV_CODEC_ID_WMAV2:
481     case AV_CODEC_ID_WMAPRO:
482     case AV_CODEC_ID_WMALOSSLESS:
483     case AV_CODEC_ID_RV20:
484     case AV_CODEC_ID_RV30:
485     case AV_CODEC_ID_RV40:
486     case AV_CODEC_ID_COOK:
487     case AV_CODEC_ID_APE:
488     case AV_CODEC_ID_DTS:
489     case AV_CODEC_ID_FLAC:
490     case AV_CODEC_ID_FLV1:
491     case AV_CODEC_ID_VORBIS:
492     case AV_CODEC_ID_HEVC:
493
494         supported = true;
495         break;
496     default:
497         ALOGD("unsuppoted codec(%s), but give it a chance",
498                 avcodec_get_name(codec_id));
499         //Won't promise that the following codec id can be supported.
500             //Just give these codecs a chance.
501         supported = true;
502         break;
503     }
504
505         return supported;
506 }
507
508 int FFmpegExtractor::stream_component_open(int stream_index)
509 {
510     AVCodecContext *avctx = NULL;
511     sp<MetaData> meta = NULL;
512     bool isAVC = false;
513     bool supported = false;
514     uint32_t type = 0;
515     const void *data = NULL;
516     size_t size = 0;
517     int ret = 0;
518
519     ALOGI("stream_index: %d", stream_index);
520     if (stream_index < 0 || stream_index >= (int)mFormatCtx->nb_streams)
521         return -1;
522     avctx = mFormatCtx->streams[stream_index]->codec;
523
524     supported = is_codec_supported(avctx->codec_id);
525
526     if (!supported) {
527         ALOGE("unsupport the codec(%s)", avcodec_get_name(avctx->codec_id));
528         return -1;
529     }
530     ALOGI("support the codec(%s)", avcodec_get_name(avctx->codec_id));
531
532     unsigned streamType;
533     ssize_t index = mTracks.indexOfKey(stream_index);
534
535     if (index >= 0) {
536         ALOGE("this track already exists");
537         return 0;
538     }
539
540     mFormatCtx->streams[stream_index]->discard = AVDISCARD_DEFAULT;
541
542     char tagbuf[32];
543     av_get_codec_tag_string(tagbuf, sizeof(tagbuf), avctx->codec_tag);
544     ALOGV("Tag %s/0x%08x with codec(%s)\n", tagbuf, avctx->codec_tag, avcodec_get_name(avctx->codec_id));
545
546     switch (avctx->codec_type) {
547     case AVMEDIA_TYPE_VIDEO:
548         if (mVideoStreamIdx == -1)
549             mVideoStreamIdx = stream_index;
550         if (mVideoStream == NULL)
551             mVideoStream = mFormatCtx->streams[stream_index];
552         if (!mVideoQInited) {
553                 packet_queue_init(&mVideoQ);
554             mVideoQInited = true;
555         }
556
557         ret = check_extradata(avctx);
558         if (ret != 1) {
559             if (ret == -1) {
560                 // disable the stream
561                 mVideoStreamIdx = -1;
562                 mVideoStream = NULL;
563                 packet_queue_end(&mVideoQ);
564                 mVideoQInited =  false;
565                 mFormatCtx->streams[stream_index]->discard = AVDISCARD_ALL;
566             }
567             return ret;
568          }
569
570         if (avctx->extradata) {
571             ALOGV("video stream extradata:");
572             hexdump(avctx->extradata, avctx->extradata_size);
573         } else {
574             ALOGV("video stream no extradata, but we can ignore it.");
575         }
576
577         meta = new MetaData;
578
579         switch(avctx->codec_id) {
580         case AV_CODEC_ID_H264:
581             /**
582              * H.264 Video Types
583              * http://msdn.microsoft.com/en-us/library/dd757808(v=vs.85).aspx
584              */
585             //if (avctx->codec_tag && avctx->codec_tag == AV_RL32("avc1")) {
586             if (avctx->extradata[0] == 1 /* configurationVersion */) {
587                 // H.264 bitstream without start codes.
588                 isAVC = true;
589                 ALOGV("AVC");
590
591                 if (avctx->width == 0 || avctx->height == 0) {
592                     int32_t width, height;
593                     sp<ABuffer> seqParamSet = new ABuffer(avctx->extradata_size - 8);
594                     memcpy(seqParamSet->data(), avctx->extradata + 8, avctx->extradata_size - 8);
595                     FindAVCDimensions(seqParamSet, &width, &height);
596                     avctx->width  = width;
597                     avctx->height = height;
598                 }
599
600                 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
601                 meta->setData(kKeyAVCC, kTypeAVCC, avctx->extradata, avctx->extradata_size);
602             } else {
603                 // H.264 bitstream with start codes.
604                 isAVC = false;
605                 ALOGV("H264");
606
607                 /* set NULL to release meta as we will new a meta in MakeAVCCodecSpecificData() fxn */
608                 meta->clear();
609                 meta = NULL;
610
611                 sp<ABuffer> buffer = new ABuffer(avctx->extradata_size);
612                 memcpy(buffer->data(), avctx->extradata, avctx->extradata_size);
613                 meta = MakeAVCCodecSpecificData(buffer);
614             }
615             break;
616         case AV_CODEC_ID_MPEG4:
617             ALOGV("MPEG4");
618             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
619             {
620                 sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
621                 memcpy(csd->data(), avctx->extradata, avctx->extradata_size);
622                 sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
623                 meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
624             }
625             break;
626         case AV_CODEC_ID_H263:
627         case AV_CODEC_ID_H263P:
628         case AV_CODEC_ID_H263I:
629             ALOGV("H263");
630             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
631             break;
632         case AV_CODEC_ID_MPEG1VIDEO:
633         case AV_CODEC_ID_MPEG2VIDEO:
634             ALOGV("MPEG%dVIDEO", avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 2 : 1);
635             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
636             {
637                 sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
638                 memcpy(csd->data(), avctx->extradata, avctx->extradata_size);
639                 sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
640                 meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
641             }
642             break;
643         case AV_CODEC_ID_VC1:
644             ALOGV("VC1");
645             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_VC1);
646             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
647             break;
648         case AV_CODEC_ID_WMV1:
649             ALOGV("WMV1");
650             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
651             meta->setInt32(kKeyWMVVersion, kTypeWMVVer_7);
652             break;
653         case AV_CODEC_ID_WMV2:
654             ALOGV("WMV2");
655             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
656             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
657             meta->setInt32(kKeyWMVVersion, kTypeWMVVer_8);
658             break;
659         case AV_CODEC_ID_WMV3:
660             ALOGV("WMV3");
661             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
662             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
663             meta->setInt32(kKeyWMVVersion, kTypeWMVVer_9);
664             break;
665         case AV_CODEC_ID_RV20:
666             ALOGV("RV30");
667             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
668             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
669             meta->setInt32(kKeyRVVersion, kTypeRVVer_G2); //http://en.wikipedia.org/wiki/RealVideo
670         case AV_CODEC_ID_RV30:
671             ALOGV("RV30");
672             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
673             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
674             meta->setInt32(kKeyRVVersion, kTypeRVVer_8); //http://en.wikipedia.org/wiki/RealVideo
675             break;
676         case AV_CODEC_ID_RV40:
677             ALOGV("RV40");
678             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
679             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
680             meta->setInt32(kKeyRVVersion, kTypeRVVer_9); //http://en.wikipedia.org/wiki/RealVideo
681             break;
682         case AV_CODEC_ID_FLV1:
683             ALOGV("FLV1");
684             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_FLV1);
685             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
686             break;
687         case AV_CODEC_ID_HEVC:
688             ALOGV("HEVC");
689             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_HEVC);
690             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
691             break;
692         default:
693             ALOGD("unsuppoted video codec(id:%d, name:%s), but give it a chance",
694                     avctx->codec_id, avcodec_get_name(avctx->codec_id));
695             meta = new MetaData;
696             meta->setInt32(kKeyCodecId, avctx->codec_id);
697             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_FFMPEG);
698             if (avctx->extradata_size > 0) {
699                 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
700             }
701             //CHECK(!"Should not be here. Unsupported codec.");
702             break;
703         }
704
705         ALOGI("width: %d, height: %d, bit_rate: %d",
706              avctx->width, avctx->height, avctx->bit_rate);
707
708         meta->setInt32(kKeyWidth, avctx->width);
709         meta->setInt32(kKeyHeight, avctx->height);
710         if (avctx->bit_rate > 0)
711             meta->setInt32(kKeyBitRate, avctx->bit_rate);
712         if (mVideoStream->duration != AV_NOPTS_VALUE) {
713             int64_t duration = mVideoStream->duration * av_q2d(mVideoStream->time_base) * 1000000;
714             printTime(duration);
715             ALOGV("video startTime: %lld", mVideoStream->start_time);
716             if (mVideoStream->start_time != AV_NOPTS_VALUE) {
717                 ALOGV("video startTime:%lld", mVideoStream->start_time);
718             } else {
719                 ALOGV("video startTime:N/A");
720             }
721             meta->setInt64(kKeyDuration, duration);
722         } else {
723             // default when no stream duration
724             meta->setInt64(kKeyDuration, mFormatCtx->duration);
725         }
726
727         ALOGV("create a video track");
728         index = mTracks.add(
729             stream_index, new Track(this, meta, isAVC, mVideoStream, &mVideoQ));
730
731         mDefersToCreateVideoTrack = false;
732
733         break;
734     case AVMEDIA_TYPE_AUDIO:
735         if (mAudioStreamIdx == -1)
736             mAudioStreamIdx = stream_index;
737         if (mAudioStream == NULL)
738             mAudioStream = mFormatCtx->streams[stream_index];
739         if (!mAudioQInited) {
740             packet_queue_init(&mAudioQ);
741             mAudioQInited = true;
742         }
743
744         ret = check_extradata(avctx);
745         if (ret != 1) {
746             if (ret == -1) {
747                 // disable the stream
748                 mAudioStreamIdx = -1;
749                 mAudioStream = NULL;
750                 packet_queue_end(&mAudioQ);
751                 mAudioQInited =  false;
752                 mFormatCtx->streams[stream_index]->discard = AVDISCARD_ALL;
753             }
754             return ret;
755         }
756
757         if (avctx->extradata) {
758             ALOGV("audio stream extradata(%d):", avctx->extradata_size);
759             hexdump(avctx->extradata, avctx->extradata_size);
760         } else {
761             ALOGV("audio stream no extradata, but we can ignore it.");
762         }
763
764         switch(avctx->codec_id) {
765         case AV_CODEC_ID_MP2:
766             ALOGV("MP2");
767             meta = new MetaData;
768             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
769             break;
770         case AV_CODEC_ID_MP3:
771             ALOGV("MP3");
772             meta = new MetaData;
773             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
774             break;
775         case AV_CODEC_ID_VORBIS:
776             ALOGV("VORBIS");
777             meta = new MetaData;
778             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_VORBIS);
779             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
780             break;
781         case AV_CODEC_ID_AC3:
782             ALOGV("AC3");
783             meta = new MetaData;
784             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3);
785             break;
786         case AV_CODEC_ID_AAC:
787             ALOGV("AAC");
788             uint32_t sr;
789             const uint8_t *header;
790             uint8_t profile, sf_index, channel;
791
792             header = avctx->extradata;
793             CHECK(header != NULL);
794
795             // AudioSpecificInfo follows
796             // oooo offf fccc c000
797             // o - audioObjectType
798             // f - samplingFreqIndex
799             // c - channelConfig
800             profile = ((header[0] & 0xf8) >> 3) - 1;
801             sf_index = (header[0] & 0x07) << 1 | (header[1] & 0x80) >> 7;
802             sr = get_sample_rate(sf_index);
803             if (sr == 0) {
804                 ALOGE("unsupport the sample rate");
805                 return -1;
806             }
807             channel = (header[1] >> 3) & 0xf;
808             ALOGV("profile: %d, sf_index: %d, channel: %d", profile, sf_index, channel);
809
810             meta = MakeAACCodecSpecificData(profile, sf_index, channel);
811             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
812             break;
813         case AV_CODEC_ID_WMAV1:  // TODO, version?
814             ALOGV("WMAV1");
815             meta = new MetaData;
816             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
817             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
818             break;
819         case AV_CODEC_ID_WMAV2:
820             ALOGV("WMAV2");
821             meta = new MetaData;
822             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
823             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
824             meta->setInt32(kKeyWMAVersion, kTypeWMA);
825             break;
826         case AV_CODEC_ID_WMAPRO:
827             ALOGV("WMAPRO");
828             meta = new MetaData;
829             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
830             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
831             meta->setInt32(kKeyWMAVersion, kTypeWMAPro);
832             break;
833         case AV_CODEC_ID_WMALOSSLESS:
834             ALOGV("WMALOSSLESS");
835             meta = new MetaData;
836             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
837             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
838             meta->setInt32(kKeyWMAVersion, kTypeWMALossLess);
839             break;
840         case AV_CODEC_ID_COOK: // audio codec in RMVB
841             ALOGV("COOK");
842             meta = new MetaData;
843             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RA);
844             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
845             break;
846         case AV_CODEC_ID_APE:
847             ALOGV("APE");
848             meta = new MetaData;
849             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_APE);
850             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
851             break;
852         case AV_CODEC_ID_DTS:
853             ALOGV("DTS");
854             meta = new MetaData;
855             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_DTS);
856             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
857             break;
858         case AV_CODEC_ID_FLAC:
859             ALOGV("FLAC");
860             meta = new MetaData;
861             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_FLAC);
862             meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
863             break;
864         default:
865             ALOGD("unsuppoted audio codec(id:%d, name:%s), but give it a chance",
866                     avctx->codec_id, avcodec_get_name(avctx->codec_id));
867             meta = new MetaData;
868             meta->setInt32(kKeyCodecId, avctx->codec_id);
869             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_FFMPEG);
870             if (avctx->extradata_size > 0) {
871                 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
872             }
873             //CHECK(!"Should not be here. Unsupported codec.");
874             break;
875         }
876
877         ALOGI("bit_rate: %d, sample_rate: %d, channels: %d, "
878                 "bits_per_coded_sample: %d, block_align:%d",
879                 avctx->bit_rate, avctx->sample_rate, avctx->channels,
880                 avctx->bits_per_coded_sample, avctx->block_align);
881
882         meta->setInt32(kKeyChannelCount, avctx->channels);
883         meta->setInt32(kKeyBitRate, avctx->bit_rate);
884         meta->setInt32(kKeyBitspersample, avctx->bits_per_coded_sample);
885         meta->setInt32(kKeySampleRate, avctx->sample_rate);
886         meta->setInt32(kKeyBlockAlign, avctx->block_align);
887         meta->setInt32(kKeySampleFormat, avctx->sample_fmt);
888         if (mAudioStream->duration != AV_NOPTS_VALUE) {
889             int64_t duration = mAudioStream->duration * av_q2d(mAudioStream->time_base) * 1000000;
890             printTime(duration);
891             if (mAudioStream->start_time != AV_NOPTS_VALUE) {
892                 ALOGV("audio startTime:%lld", mAudioStream->start_time);
893             } else {
894                 ALOGV("audio startTime:N/A");
895             }
896             meta->setInt64(kKeyDuration, duration);
897         } else {
898             // default when no stream duration
899             meta->setInt64(kKeyDuration, mFormatCtx->duration);
900         }
901
902         ALOGV("create a audio track");
903         index = mTracks.add(
904             stream_index, new Track(this, meta, false, mAudioStream, &mAudioQ));
905
906         mDefersToCreateAudioTrack = false;
907
908         break;
909     case AVMEDIA_TYPE_SUBTITLE:
910         /* Unsupport now */
911         CHECK(!"Should not be here. Unsupported media type.");
912         break;
913     default:
914         CHECK(!"Should not be here. Unsupported media type.");
915         break;
916     }
917     return 0;
918 }
919
920 void FFmpegExtractor::stream_component_close(int stream_index)
921 {
922     AVCodecContext *avctx;
923
924     if (stream_index < 0 || stream_index >= (int)mFormatCtx->nb_streams)
925         return;
926     avctx = mFormatCtx->streams[stream_index]->codec;
927
928     switch (avctx->codec_type) {
929     case AVMEDIA_TYPE_VIDEO:
930         ALOGV("packet_queue_abort videoq");
931         packet_queue_abort(&mVideoQ);
932         /* wait until the end */
933         while (!mAbortRequest && !mVideoEOSReceived) {
934             ALOGV("wait for video received");
935             usleep(10000);
936         }
937         ALOGV("packet_queue_end videoq");
938         packet_queue_end(&mVideoQ);
939         break;
940     case AVMEDIA_TYPE_AUDIO:
941         ALOGV("packet_queue_abort audioq");
942         packet_queue_abort(&mAudioQ);
943         while (!mAbortRequest && !mAudioEOSReceived) {
944             ALOGV("wait for audio received");
945             usleep(10000);
946         }
947         ALOGV("packet_queue_end audioq");
948         packet_queue_end(&mAudioQ);
949         break;
950     case AVMEDIA_TYPE_SUBTITLE:
951         break;
952     default:
953         break;
954     }
955
956     mFormatCtx->streams[stream_index]->discard = AVDISCARD_ALL;
957     switch (avctx->codec_type) {
958     case AVMEDIA_TYPE_VIDEO:
959         mVideoStream    = NULL;
960         mVideoStreamIdx = -1;
961         if (mVideoBsfc) {
962             av_bitstream_filter_close(mVideoBsfc);
963             mVideoBsfc  = NULL;
964         }
965         break;
966     case AVMEDIA_TYPE_AUDIO:
967         mAudioStream    = NULL;
968         mAudioStreamIdx = -1;
969         if (mAudioBsfc) {
970             av_bitstream_filter_close(mAudioBsfc);
971             mAudioBsfc  = NULL;
972         }
973         break;
974     case AVMEDIA_TYPE_SUBTITLE:
975         break;
976     default:
977         break;
978     }
979 }
980
981 void FFmpegExtractor::reachedEOS(enum AVMediaType media_type)
982 {
983     Mutex::Autolock autoLock(mLock);
984
985     if (media_type == AVMEDIA_TYPE_VIDEO) {
986         mVideoEOSReceived = true;
987     } else if (media_type == AVMEDIA_TYPE_AUDIO) {
988         mAudioEOSReceived = true;
989     }
990 }
991
992 /* seek in the stream */
993 int FFmpegExtractor::stream_seek(int64_t pos, enum AVMediaType media_type)
994 {
995     Mutex::Autolock autoLock(mLock);
996
997     if (mVideoStreamIdx >= 0 &&
998         mAudioStreamIdx >= 0 &&
999         media_type == AVMEDIA_TYPE_AUDIO &&
1000         !mVideoEOSReceived) {
1001        return NO_SEEK;
1002     }
1003
1004     // flush immediately
1005     if (mAudioStreamIdx >= 0)
1006         packet_queue_flush(&mAudioQ);
1007     if (mVideoStreamIdx >= 0)
1008         packet_queue_flush(&mVideoQ);
1009
1010     mSeekPos = pos;
1011     mSeekFlags &= ~AVSEEK_FLAG_BYTE;
1012     mSeekReq = 1;
1013
1014     return SEEK;
1015 }
1016
1017 // staitc
1018 int FFmpegExtractor::decode_interrupt_cb(void *ctx)
1019 {
1020     FFmpegExtractor *extrator = static_cast<FFmpegExtractor *>(ctx);
1021     return extrator->mAbortRequest;
1022 }
1023
1024 void FFmpegExtractor::buildFileName(const sp<DataSource> &source)
1025 {
1026 #if 1
1027     ALOGI("android-source:%p", source.get());
1028     // pass the addr of smart pointer("source")
1029     snprintf(mFilename, sizeof(mFilename), "android-source:%p", source.get());
1030     ALOGI("build mFilename: %s", mFilename);
1031 #else
1032     const char *url = mDataSource->getNamURI();
1033     if (url == NULL) {
1034         ALOGI("url is error!");
1035         return;
1036     }
1037     // is it right?
1038     if (!strcmp(url, "-")) {
1039         av_strlcpy(mFilename, "pipe:", strlen("pipe:") + 1);
1040     } else {
1041         av_strlcpy(mFilename, url, strlen(url) + 1);
1042     }
1043     ALOGI("build url: %s, mFilename: %s", url, mFilename);
1044 #endif
1045 }
1046
1047 void FFmpegExtractor::setFFmpegDefaultOpts()
1048 {
1049     mGenPTS       = 0;
1050 #if DEBUG_DISABLE_VIDEO
1051     mVideoDisable = 1;
1052 #else
1053     mVideoDisable = 0;
1054 #endif
1055 #if DEBUG_DISABLE_AUDIO
1056     mAudioDisable = 1;
1057 #else
1058     mAudioDisable = 0;
1059 #endif
1060     mShowStatus   = 0;
1061     mSeekByBytes  = 0; /* seek by bytes 0=off 1=on -1=auto" */
1062     mDuration     = AV_NOPTS_VALUE;
1063     mSeekPos      = AV_NOPTS_VALUE;
1064     mAutoExit     = 1;
1065     mLoop         = 1;
1066
1067     mVideoStreamIdx = -1;
1068     mAudioStreamIdx = -1;
1069     mVideoStream  = NULL;
1070     mAudioStream  = NULL;
1071     mVideoQInited = false;
1072     mAudioQInited = false;
1073     mDefersToCreateVideoTrack = false;
1074     mDefersToCreateAudioTrack = false;
1075     mVideoBsfc = NULL;
1076     mAudioBsfc = NULL;
1077
1078     mAbortRequest = 0;
1079     mPaused       = 0;
1080     mLastPaused   = 0;
1081     mSeekReq      = 0;
1082
1083     mProbePkts    = 0;
1084     mEOF          = false;
1085 }
1086
1087 int FFmpegExtractor::initStreams()
1088 {
1089     int err = 0;
1090         int i = 0;
1091     status_t status = UNKNOWN_ERROR;
1092     int eof = 0;
1093     int ret = 0, audio_ret = -1, video_ret = -1;
1094     int pkt_in_play_range = 0;
1095     AVDictionaryEntry *t = NULL;
1096     AVDictionary **opts = NULL;
1097     int orig_nb_streams = 0;
1098     int st_index[AVMEDIA_TYPE_NB] = {0};
1099     int wanted_stream[AVMEDIA_TYPE_NB] = {0};
1100     st_index[AVMEDIA_TYPE_AUDIO]  = -1;
1101     st_index[AVMEDIA_TYPE_VIDEO]  = -1;
1102     wanted_stream[AVMEDIA_TYPE_AUDIO]  = -1;
1103     wanted_stream[AVMEDIA_TYPE_VIDEO]  = -1;
1104     const char *mime = NULL;
1105
1106     setFFmpegDefaultOpts();
1107
1108     status = initFFmpeg();
1109     if (status != OK) {
1110         ret = -1;
1111         goto fail;
1112     }
1113     mFFmpegInited = true;
1114
1115     av_init_packet(&flush_pkt);
1116     flush_pkt.data = (uint8_t *)"FLUSH";
1117     flush_pkt.size = 0;
1118
1119     mFormatCtx = avformat_alloc_context();
1120         if (!mFormatCtx)
1121         {
1122         ALOGE("oom for alloc avformat context");
1123         ret = -1;
1124         goto fail;
1125         }
1126     mFormatCtx->interrupt_callback.callback = decode_interrupt_cb;
1127     mFormatCtx->interrupt_callback.opaque = this;
1128     ALOGV("mFilename: %s", mFilename);
1129     err = avformat_open_input(&mFormatCtx, mFilename, NULL, &format_opts);
1130     if (err < 0) {
1131         ALOGE("%s: avformat_open_input failed, err:%s", mFilename, av_err2str(err));
1132         ret = -1;
1133         goto fail;
1134     }
1135     if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1136         ALOGE("Option %s not found.\n", t->key);
1137         //ret = AVERROR_OPTION_NOT_FOUND;
1138         ret = -1;
1139         goto fail;
1140     }
1141
1142     if (mGenPTS)
1143         mFormatCtx->flags |= AVFMT_FLAG_GENPTS;
1144
1145     opts = setup_find_stream_info_opts(mFormatCtx, codec_opts);
1146     orig_nb_streams = mFormatCtx->nb_streams;
1147
1148     err = avformat_find_stream_info(mFormatCtx, opts);
1149     if (err < 0) {
1150         ALOGE("%s: could not find stream info, err:%s", mFilename, av_err2str(err));
1151         ret = -1;
1152         goto fail;
1153     }
1154     for (i = 0; i < orig_nb_streams; i++)
1155         av_dict_free(&opts[i]);
1156     av_freep(&opts);
1157
1158     mime = findMatchingContainer(mFormatCtx->iformat->name);
1159     CHECK(mime != NULL);
1160     mMeta->setCString(kKeyMIMEType, mime);
1161
1162     if (mFormatCtx->pb)
1163         mFormatCtx->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
1164
1165     if (mSeekByBytes < 0)
1166         mSeekByBytes = !!(mFormatCtx->iformat->flags & AVFMT_TS_DISCONT);
1167
1168     for (i = 0; i < (int)mFormatCtx->nb_streams; i++)
1169         mFormatCtx->streams[i]->discard = AVDISCARD_ALL;
1170     if (!mVideoDisable)
1171         st_index[AVMEDIA_TYPE_VIDEO] =
1172             av_find_best_stream(mFormatCtx, AVMEDIA_TYPE_VIDEO,
1173                                 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
1174     if (!mAudioDisable)
1175         st_index[AVMEDIA_TYPE_AUDIO] =
1176             av_find_best_stream(mFormatCtx, AVMEDIA_TYPE_AUDIO,
1177                                 wanted_stream[AVMEDIA_TYPE_AUDIO],
1178                                 st_index[AVMEDIA_TYPE_VIDEO],
1179                                 NULL, 0);
1180     if (mShowStatus) {
1181         av_dump_format(mFormatCtx, 0, mFilename, 0);
1182     }
1183
1184     if (mFormatCtx->duration != AV_NOPTS_VALUE &&
1185             mFormatCtx->start_time != AV_NOPTS_VALUE) {
1186         int hours, mins, secs, us;
1187
1188         ALOGV("file startTime: %lld", mFormatCtx->start_time);
1189
1190         mDuration = mFormatCtx->duration;
1191
1192         secs = mDuration / AV_TIME_BASE;
1193         us = mDuration % AV_TIME_BASE;
1194         mins = secs / 60;
1195         secs %= 60;
1196         hours = mins / 60;
1197         mins %= 60;
1198         ALOGI("the duration is %02d:%02d:%02d.%02d",
1199             hours, mins, secs, (100 * us) / AV_TIME_BASE);
1200     }
1201
1202     if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
1203         audio_ret = stream_component_open(st_index[AVMEDIA_TYPE_AUDIO]);
1204     }
1205
1206     if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
1207         video_ret = stream_component_open(st_index[AVMEDIA_TYPE_VIDEO]);
1208     }
1209
1210     if ( audio_ret < 0 && video_ret < 0) {
1211         ALOGE("%s: could not open codecs\n", mFilename);
1212         ret = -1;
1213         goto fail;
1214     }
1215
1216     ret = 0;
1217
1218 fail:
1219     return ret;
1220 }
1221
1222 void FFmpegExtractor::deInitStreams()
1223 {
1224     if (mFormatCtx) {
1225         avformat_close_input(&mFormatCtx);
1226     }
1227
1228     if (mFFmpegInited) {
1229         deInitFFmpeg();
1230     }
1231 }
1232
1233 status_t FFmpegExtractor::startReaderThread() {
1234     ALOGV("Starting reader thread");
1235     Mutex::Autolock autoLock(mLock);
1236
1237     if (mReaderThreadStarted)
1238         return OK;
1239
1240     pthread_attr_t attr;
1241     pthread_attr_init(&attr);
1242     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1243     pthread_create(&mReaderThread, &attr, ReaderWrapper, this);
1244     pthread_attr_destroy(&attr);
1245     mReaderThreadStarted = true;
1246     ALOGD("Reader thread started");
1247
1248     return OK;
1249 }
1250
1251 void FFmpegExtractor::stopReaderThread() {
1252     ALOGV("Stopping reader thread");
1253     Mutex::Autolock autoLock(mLock);
1254
1255     if (!mReaderThreadStarted) {
1256         ALOGD("Reader thread have been stopped");
1257         return;
1258     }
1259
1260     mAbortRequest = 1;
1261
1262     void *dummy;
1263     pthread_join(mReaderThread, &dummy);
1264     mReaderThreadStarted = false;
1265     ALOGD("Reader thread stopped");
1266 }
1267
1268 // static
1269 void *FFmpegExtractor::ReaderWrapper(void *me) {
1270     ((FFmpegExtractor *)me)->readerEntry();
1271
1272     return NULL;
1273 }
1274
1275 void FFmpegExtractor::readerEntry() {
1276     int err, i, ret;
1277     AVPacket pkt1, *pkt = &pkt1;
1278     int eof = 0;
1279     int pkt_in_play_range = 0;
1280
1281     ALOGV("FFmpegExtractor enter thread(readerEntry)");
1282
1283     mVideoEOSReceived = false;
1284     mAudioEOSReceived = false;
1285
1286     for (;;) {
1287         if (mAbortRequest)
1288             break;
1289
1290         if (mPaused != mLastPaused) {
1291             mLastPaused = mPaused;
1292             if (mPaused)
1293                 mReadPauseReturn = av_read_pause(mFormatCtx);
1294             else
1295                 av_read_play(mFormatCtx);
1296         }
1297 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
1298         if (mPaused &&
1299                 (!strcmp(mFormatCtx->iformat->name, "rtsp") ||
1300                  (mFormatCtx->pb && !strncmp(mFilename, "mmsh:", 5)))) {
1301             /* wait 10 ms to avoid trying to get another packet */
1302             /* XXX: horrible */
1303             usleep(10000);
1304             continue;
1305         }
1306 #endif
1307
1308         if (mSeekReq) {
1309             ALOGV("readerEntry, mSeekReq: %d", mSeekReq);
1310             ret = avformat_seek_file(mFormatCtx, -1, INT64_MIN, mSeekPos, INT64_MAX, mSeekFlags);
1311             if (ret < 0) {
1312                 ALOGE("%s: error while seeking", mFormatCtx->filename);
1313             } else {
1314                 if (mAudioStreamIdx >= 0) {
1315                     packet_queue_flush(&mAudioQ);
1316                     packet_queue_put(&mAudioQ, &flush_pkt);
1317                 }
1318                 if (mVideoStreamIdx >= 0) {
1319                     packet_queue_flush(&mVideoQ);
1320                     packet_queue_put(&mVideoQ, &flush_pkt);
1321                 }
1322             }
1323             mSeekReq = 0;
1324             eof = 0;
1325         }
1326
1327         /* if the queue are full, no need to read more */
1328         if (   mAudioQ.size + mVideoQ.size > MAX_QUEUE_SIZE
1329             || (   (mAudioQ   .size  > MIN_AUDIOQ_SIZE || mAudioStreamIdx < 0)
1330                 && (mVideoQ   .nb_packets > MIN_FRAMES || mVideoStreamIdx < 0))) {
1331 #if DEBUG_READ_ENTRY
1332             ALOGV("readerEntry, is full, fuck");
1333 #endif
1334             /* wait 10 ms */
1335             usleep(10000);
1336             continue;
1337         }
1338
1339         if (eof) {
1340             if (mVideoStreamIdx >= 0) {
1341                 av_init_packet(pkt);
1342                 pkt->data = NULL;
1343                 pkt->size = 0;
1344                 pkt->stream_index = mVideoStreamIdx;
1345                 packet_queue_put(&mVideoQ, pkt);
1346             }
1347             if (mAudioStreamIdx >= 0) {
1348                 av_init_packet(pkt);
1349                 pkt->data = NULL;
1350                 pkt->size = 0;
1351                 pkt->stream_index = mAudioStreamIdx;
1352                 packet_queue_put(&mAudioQ, pkt);
1353             }
1354             usleep(10000);
1355 #if DEBUG_READ_ENTRY
1356             ALOGV("readerEntry, eof = 1, mVideoQ.size: %d, mVideoQ.nb_packets: %d, mAudioQ.size: %d, mAudioQ.nb_packets: %d",
1357                     mVideoQ.size, mVideoQ.nb_packets, mAudioQ.size, mAudioQ.nb_packets);
1358 #endif
1359             if (mAudioQ.size + mVideoQ.size  == 0) {
1360                 if (mAutoExit) {
1361                     ret = AVERROR_EOF;
1362                     goto fail;
1363                 }
1364             }
1365             eof=0;
1366             continue;
1367         }
1368
1369         ret = av_read_frame(mFormatCtx, pkt);
1370         mProbePkts++;
1371         if (ret < 0) {
1372             if (ret == AVERROR_EOF || url_feof(mFormatCtx->pb))
1373                 if (ret == AVERROR_EOF) {
1374                     //ALOGV("ret == AVERROR_EOF");
1375                 }
1376                 if (url_feof(mFormatCtx->pb)) {
1377                     //ALOGV("url_feof(mFormatCtx->pb)");
1378                 }
1379
1380                 eof = 1;
1381                 mEOF = true;
1382             if (mFormatCtx->pb && mFormatCtx->pb->error) {
1383                 ALOGE("mFormatCtx->pb->error: %d", mFormatCtx->pb->error);
1384                 break;
1385             }
1386             usleep(100000);
1387             continue;
1388         }
1389
1390         if (pkt->stream_index == mVideoStreamIdx) {
1391              if (mDefersToCreateVideoTrack) {
1392                 AVCodecContext *avctx = mFormatCtx->streams[mVideoStreamIdx]->codec;
1393
1394                 int i = parser_split(avctx, pkt->data, pkt->size);
1395                 if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
1396                     if (avctx->extradata)
1397                         av_freep(&avctx->extradata);
1398                     avctx->extradata_size= i;
1399                     avctx->extradata = (uint8_t *)av_malloc(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
1400                     if (!avctx->extradata) {
1401                         //return AVERROR(ENOMEM);
1402                         ret = AVERROR(ENOMEM);
1403                         goto fail;
1404                     }
1405                     // sps + pps(there may be sei in it)
1406                     memcpy(avctx->extradata, pkt->data, avctx->extradata_size);
1407                     memset(avctx->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1408                 } else {
1409                     av_free_packet(pkt);
1410                     continue;
1411                 }
1412
1413                 stream_component_open(mVideoStreamIdx);
1414                 if (!mDefersToCreateVideoTrack)
1415                     ALOGI("probe packet counter: %d when create video track ok", mProbePkts);
1416                 if (mProbePkts == EXTRACTOR_MAX_PROBE_PACKETS)
1417                     ALOGI("probe packet counter to max: %d, create video track: %d",
1418                         mProbePkts, !mDefersToCreateVideoTrack);
1419             }
1420         } else if (pkt->stream_index == mAudioStreamIdx) {
1421             int ret;
1422             uint8_t *outbuf;
1423             int   outbuf_size;
1424             AVCodecContext *avctx = mFormatCtx->streams[mAudioStreamIdx]->codec;
1425             if (mAudioBsfc && pkt && pkt->data) {
1426                 ret = av_bitstream_filter_filter(mAudioBsfc, avctx, NULL, &outbuf, &outbuf_size,
1427                                    pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY);
1428
1429                 if (ret < 0 ||!outbuf_size) {
1430                     av_free_packet(pkt);
1431                     continue;
1432                 }
1433                 if (outbuf && outbuf != pkt->data) {
1434                     memmove(pkt->data, outbuf, outbuf_size);
1435                     pkt->size = outbuf_size;
1436                 }
1437             }
1438             if (mDefersToCreateAudioTrack) {
1439                 if (avctx->extradata_size <= 0) {
1440                     av_free_packet(pkt);
1441                     continue;
1442                 }
1443                 stream_component_open(mAudioStreamIdx);
1444                 if (!mDefersToCreateAudioTrack)
1445                     ALOGI("probe packet counter: %d when create audio track ok", mProbePkts);
1446                 if (mProbePkts == EXTRACTOR_MAX_PROBE_PACKETS)
1447                     ALOGI("probe packet counter to max: %d, create audio track: %d",
1448                         mProbePkts, !mDefersToCreateAudioTrack);
1449             }
1450         }
1451
1452         if (pkt->stream_index == mAudioStreamIdx) {
1453             packet_queue_put(&mAudioQ, pkt);
1454         } else if (pkt->stream_index == mVideoStreamIdx) {
1455             packet_queue_put(&mVideoQ, pkt);
1456         } else {
1457             av_free_packet(pkt);
1458         }
1459     }
1460     /* wait until the end */
1461     while (!mAbortRequest) {
1462         usleep(100000);
1463     }
1464
1465     ret = 0;
1466 fail:
1467     ALOGI("reader thread goto end...");
1468
1469     /* close each stream */
1470     if (mAudioStreamIdx >= 0)
1471         stream_component_close(mAudioStreamIdx);
1472     if (mVideoStreamIdx >= 0)
1473         stream_component_close(mVideoStreamIdx);
1474     if (mFormatCtx) {
1475         avformat_close_input(&mFormatCtx);
1476     }
1477
1478     ALOGV("FFmpegExtractor exit thread(readerEntry)");
1479 }
1480
1481 ////////////////////////////////////////////////////////////////////////////////
1482
1483 FFmpegExtractor::Track::Track(
1484         FFmpegExtractor *extractor, sp<MetaData> meta, bool isAVC,
1485         AVStream *stream, PacketQueue *queue)
1486     : mExtractor(extractor),
1487       mMeta(meta),
1488       mIsAVC(isAVC),
1489       mStream(stream),
1490       mQueue(queue) {
1491     const char *mime;
1492
1493     /* H.264 Video Types */
1494     {
1495         mNal2AnnexB = false;
1496
1497         if (mIsAVC) {
1498             uint32_t type;
1499             const void *data;
1500             size_t size;
1501             CHECK(meta->findData(kKeyAVCC, &type, &data, &size));
1502
1503             const uint8_t *ptr = (const uint8_t *)data;
1504
1505             CHECK(size >= 7);
1506             CHECK_EQ((unsigned)ptr[0], 1u);  // configurationVersion == 1
1507
1508             // The number of bytes used to encode the length of a NAL unit.
1509             mNALLengthSize = 1 + (ptr[4] & 3);
1510
1511             ALOGV("the stream is AVC, the length of a NAL unit: %d", mNALLengthSize);
1512
1513             mNal2AnnexB = true;
1514         }
1515     }
1516
1517     mMediaType = mStream->codec->codec_type;
1518     mFirstKeyPktTimestamp = AV_NOPTS_VALUE;
1519 }
1520
1521 FFmpegExtractor::Track::~Track() {
1522     ALOGV("FFmpegExtractor::Track::~Track %s",
1523             av_get_media_type_string(mMediaType));
1524         mExtractor = NULL;
1525         mMeta = NULL;
1526 }
1527
1528 status_t FFmpegExtractor::Track::start(MetaData *params) {
1529     ALOGV("FFmpegExtractor::Track::start %s",
1530             av_get_media_type_string(mMediaType));
1531     Mutex::Autolock autoLock(mLock);
1532     //mExtractor->startReaderThread();
1533     return OK;
1534 }
1535
1536 status_t FFmpegExtractor::Track::stop() {
1537     ALOGV("FFmpegExtractor::Track::stop %s",
1538             av_get_media_type_string(mMediaType));
1539     Mutex::Autolock autoLock(mLock);
1540     //mExtractor->stopReaderThread();
1541     return OK;
1542 }
1543
1544 sp<MetaData> FFmpegExtractor::Track::getFormat() {
1545     Mutex::Autolock autoLock(mLock);
1546
1547     return mMeta;
1548 }
1549
1550 status_t FFmpegExtractor::Track::read(
1551         MediaBuffer **buffer, const ReadOptions *options) {
1552     *buffer = NULL;
1553
1554     Mutex::Autolock autoLock(mLock);
1555
1556     AVPacket pkt;
1557     bool seeking = false;
1558     bool waitKeyPkt = false;
1559     ReadOptions::SeekMode mode;
1560     int64_t pktTS = AV_NOPTS_VALUE;
1561     int64_t seekTimeUs = AV_NOPTS_VALUE;
1562     int64_t timeUs = AV_NOPTS_VALUE;
1563     int key = 0;
1564     status_t status = OK;
1565
1566     if (options && options->getSeekTo(&seekTimeUs, &mode)) {
1567         ALOGV("~~~%s seekTimeUs: %lld, mode: %d", av_get_media_type_string(mMediaType), seekTimeUs, mode);
1568         /* add the stream start time */
1569         if (mStream->start_time != AV_NOPTS_VALUE)
1570             seekTimeUs += mStream->start_time * av_q2d(mStream->time_base) * 1000000;
1571         ALOGV("~~~%s seekTimeUs[+startTime]: %lld, mode: %d", av_get_media_type_string(mMediaType), seekTimeUs, mode);
1572
1573         if (mExtractor->stream_seek(seekTimeUs, mMediaType) == SEEK)
1574             seeking = true;
1575     }
1576
1577 retry:
1578     if (mExtractor->packet_queue_get(mQueue, &pkt, 1) < 0) {
1579         mExtractor->reachedEOS(mMediaType);
1580         return ERROR_END_OF_STREAM;
1581     }
1582
1583     if (seeking) {
1584         if (pkt.data != flush_pkt.data) {
1585             av_free_packet(&pkt);
1586             goto retry;
1587         } else {
1588             seeking = false;
1589 #if WAIT_KEY_PACKET_AFTER_SEEK
1590             waitKeyPkt = true;
1591 #endif
1592         }
1593     }
1594
1595     if (pkt.data == flush_pkt.data) {
1596         ALOGV("read %s flush pkt", av_get_media_type_string(mMediaType));
1597         av_free_packet(&pkt);
1598         mFirstKeyPktTimestamp = AV_NOPTS_VALUE;
1599         goto retry;
1600     } else if (pkt.data == NULL && pkt.size == 0) {
1601         ALOGD("read %s eos pkt", av_get_media_type_string(mMediaType));
1602         av_free_packet(&pkt);
1603         mExtractor->reachedEOS(mMediaType);
1604             return ERROR_END_OF_STREAM;
1605     }
1606
1607     key = pkt.flags & AV_PKT_FLAG_KEY ? 1 : 0;
1608     pktTS = pkt.pts; //FIXME AV_NOPTS_VALUE??
1609
1610     //use dts when AVI
1611     if (pkt.pts == AV_NOPTS_VALUE)
1612         pktTS = pkt.dts;
1613
1614     //FIXME, drop, omxcodec requires a positive timestamp! e.g. vorbis
1615     if (pktTS != AV_NOPTS_VALUE && pktTS < 0) {
1616         ALOGW("drop the packet with negative timestamp(pts:%lld)", pktTS);
1617         av_free_packet(&pkt);
1618         goto retry;
1619     }
1620
1621     if (waitKeyPkt) {
1622         if (!key) {
1623             ALOGV("drop the non-key packet");
1624             av_free_packet(&pkt);
1625             goto retry;
1626         } else {
1627             ALOGV("~~~~~~ got the key packet");
1628             waitKeyPkt = false;
1629         }
1630     }
1631
1632     if (pktTS != AV_NOPTS_VALUE && mFirstKeyPktTimestamp == AV_NOPTS_VALUE) {
1633         // update the first key timestamp
1634         mFirstKeyPktTimestamp = pktTS;
1635     }
1636      
1637     if (pktTS != AV_NOPTS_VALUE && pktTS < mFirstKeyPktTimestamp) {
1638             ALOGV("drop the packet with the backward timestamp, maybe they are B-frames after I-frame ^_^");
1639             av_free_packet(&pkt);
1640             goto retry;
1641     }
1642
1643     MediaBuffer *mediaBuffer = new MediaBuffer(pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
1644     mediaBuffer->meta_data()->clear();
1645     mediaBuffer->set_range(0, pkt.size);
1646 #if DISABLE_NAL_TO_ANNEXB
1647     mNal2AnnexB = false;
1648 #endif
1649     if (mIsAVC && mNal2AnnexB) {
1650         /* Convert H.264 NAL format to annex b */
1651         if (mNALLengthSize >= 3 && mNALLengthSize <= 4 )
1652         {
1653             uint8_t *dst = (uint8_t *)mediaBuffer->data();
1654
1655             /* This only works for NAL sizes 3-4 */
1656             size_t len = pkt.size, i;
1657             uint8_t *ptr = pkt.data;
1658             while (len >= mNALLengthSize) {
1659                 uint32_t nal_len = 0;
1660                 for( i = 0; i < mNALLengthSize; i++ ) {
1661                     nal_len = (nal_len << 8) | ptr[i];
1662                     dst[i] = 0;
1663                 }
1664                 dst[mNALLengthSize - 1] = 1;
1665                 if (nal_len > INT_MAX || nal_len > (unsigned int)len) {
1666                     status = ERROR_MALFORMED;
1667                     break;
1668                 }
1669                                 dst += mNALLengthSize;
1670                                 ptr += mNALLengthSize;
1671                 len -= mNALLengthSize;
1672
1673                 memcpy(dst, ptr, nal_len);
1674
1675                 dst += nal_len;
1676                 ptr += nal_len;
1677                 len -= nal_len;
1678             }
1679         } else {
1680              status = ERROR_MALFORMED;
1681         }
1682
1683         if (status != OK) {
1684             ALOGV("status != OK");
1685             mediaBuffer->release();
1686             mediaBuffer = NULL;
1687             av_free_packet(&pkt);
1688             return ERROR_MALFORMED;
1689         }
1690     } else {
1691         memcpy(mediaBuffer->data(), pkt.data, pkt.size);
1692     }
1693
1694     int64_t start_time = mStream->start_time != AV_NOPTS_VALUE ? mStream->start_time : 0;
1695     if (pktTS != AV_NOPTS_VALUE)
1696         timeUs = (int64_t)((pktTS - start_time) * av_q2d(mStream->time_base) * 1000000);
1697     else
1698         timeUs = SF_NOPTS_VALUE; //FIXME AV_NOPTS_VALUE is negative, but stagefright need positive
1699
1700 #if DEBUG_PKT
1701     if (pktTS != AV_NOPTS_VALUE)
1702         ALOGV("read %s pkt, size:%d, key:%d, pts:%lld, dts:%lld, timeUs[-startTime]:%lld us (%.2f secs)",
1703             av_get_media_type_string(mMediaType), pkt.size, key, pkt.pts, pkt.dts, timeUs, timeUs/1E6);
1704     else
1705         ALOGV("read %s pkt, size:%d, key:%d, pts:N/A, dts:N/A, timeUs[-startTime]:N/A",
1706             av_get_media_type_string(mMediaType), pkt.size, key);
1707 #endif
1708
1709     mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs);
1710     mediaBuffer->meta_data()->setInt32(kKeyIsSyncFrame, key);
1711
1712     *buffer = mediaBuffer;
1713
1714     av_free_packet(&pkt);
1715
1716     return OK;
1717 }
1718
1719 ////////////////////////////////////////////////////////////////////////////////
1720
1721 typedef struct {
1722     const char *format;
1723     const char *container;
1724 } formatmap;
1725
1726 static formatmap FILE_FORMATS[] = {
1727         {"mpeg",                    MEDIA_MIMETYPE_CONTAINER_MPEG2PS  },
1728         {"mpegts",                  MEDIA_MIMETYPE_CONTAINER_TS       },
1729         {"mov,mp4,m4a,3gp,3g2,mj2", MEDIA_MIMETYPE_CONTAINER_MPEG4    },
1730         {"matroska,webm",           MEDIA_MIMETYPE_CONTAINER_MATROSKA },
1731         {"asf",                     MEDIA_MIMETYPE_CONTAINER_ASF      },
1732         {"rm",                      MEDIA_MIMETYPE_CONTAINER_RM       },
1733         {"flv",                     MEDIA_MIMETYPE_CONTAINER_FLV      },
1734         {"swf",                     MEDIA_MIMETYPE_CONTAINER_FLV      },
1735         {"avi",                     MEDIA_MIMETYPE_CONTAINER_AVI      },
1736         {"ape",                     MEDIA_MIMETYPE_CONTAINER_APE      },
1737         {"dts",                     MEDIA_MIMETYPE_CONTAINER_DTS      },
1738         {"flac",                    MEDIA_MIMETYPE_CONTAINER_FLAC     },
1739         {"ac3",                     MEDIA_MIMETYPE_AUDIO_AC3          },
1740         {"wav",                     MEDIA_MIMETYPE_CONTAINER_WAV      },
1741         {"ogg",                     MEDIA_MIMETYPE_CONTAINER_OGG      },
1742         {"hevc",                     MEDIA_MIMETYPE_CONTAINER_HEVC    },
1743 };
1744
1745 static void adjustMPEG4Confidence(AVFormatContext *ic, float *confidence)
1746 {
1747         AVDictionary *tags = NULL;
1748         AVDictionaryEntry *tag = NULL;
1749
1750         tags = ic->metadata;
1751
1752         //NOTE: You can use command to show these tags,
1753         //e.g. "ffprobe -show_format 2012.mov"
1754
1755         tag = av_dict_get(tags, "major_brand", NULL, 0);
1756         if (!tag) {
1757                 return;
1758         }
1759
1760         ALOGV("major_brand tag is:%s", tag->value);
1761
1762         //when MEDIA_MIMETYPE_CONTAINER_MPEG4
1763         //WTF, MPEG4Extractor.cpp can not extractor mov format
1764         //NOTE: isCompatibleBrand(MPEG4Extractor.cpp)
1765         //  Won't promise that the following file types can be played.
1766         //  Just give these file types a chance.
1767         //  FOURCC('q', 't', ' ', ' '),  // Apple's QuickTime
1768         //So......
1769         if (!strcmp(tag->value, "qt  ")) {
1770                 ALOGI("format is mov, confidence should be larger than mpeg4");
1771                 //the MEDIA_MIMETYPE_CONTAINER_MPEG4 of confidence is 0.4f
1772                 *confidence = 0.41f;
1773         }
1774 }
1775
1776 static void adjustVideoCodecConfidence(AVFormatContext *ic,
1777                 enum AVCodecID codec_id, float *confidence)
1778 {
1779         //add to here
1780 }
1781
1782 //TODO. if the other stream(e.g. mp3) is supported by stagefright
1783 static void adjustAudioCodecConfidence(AVFormatContext *ic,
1784                 enum AVCodecID codec_id, float *confidence)
1785 {
1786         switch (codec_id) {
1787         case AV_CODEC_ID_AC3:
1788                 ALOGI("ffmpeg can demux ac3 only");
1789                 *confidence = 0.88f;
1790                 break;
1791         case AV_CODEC_ID_MP1:
1792         case AV_CODEC_ID_MP2:
1793                 //TODO. if the other stream(e.g. mp3) is supported by stagefright
1794                 ALOGI("ffmpeg can demux mp1 and mp2 only");
1795                 *confidence = 0.88f;
1796                 break;
1797         default:
1798                 break;
1799         }
1800 }
1801
1802 static void adjustCodecConfidence(AVFormatContext *ic, float *confidence)
1803 {
1804         unsigned int idx = 0;
1805         AVCodecContext *avctx = NULL;
1806         AVMediaType     codec_type = AVMEDIA_TYPE_UNKNOWN;
1807         enum AVCodecID codec_id = AV_CODEC_ID_NONE;
1808         bool haveVideo = false;
1809         bool haveAudio = false;
1810         bool haveMP3 = false;
1811
1812         for (idx = 0; idx < ic->nb_streams; idx++) {
1813                 avctx = ic->streams[idx]->codec;
1814                 codec_type = avctx->codec_type;
1815                 codec_id = avctx->codec_id;
1816
1817                 if (codec_type == AVMEDIA_TYPE_VIDEO) {
1818                         haveVideo = true;
1819                         adjustVideoCodecConfidence(ic, codec_id, confidence);
1820                 } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
1821                         haveAudio = true;
1822                         adjustAudioCodecConfidence(ic, codec_id, confidence);
1823                         if (codec_id == AV_CODEC_ID_MP3)
1824                                 haveMP3 = true;
1825                 }
1826         }
1827
1828         if (haveVideo && haveMP3) {
1829                 *confidence = 0.22f; // larger than MP3Extractor an MP3Extractor
1830         }
1831 }
1832
1833 static void adjustConfidenceIfNeeded(const char *mime,
1834                 AVFormatContext *ic, float *confidence)
1835 {
1836         //check mime
1837         if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4)) {
1838                 adjustMPEG4Confidence(ic, confidence);
1839         } else {
1840                 //add to here;
1841         }
1842
1843         //check codec
1844         adjustCodecConfidence(ic, confidence);
1845 }
1846
1847 static const char *findMatchingContainer(const char *name)
1848 {
1849         size_t i = 0;
1850         const char *container = NULL;
1851
1852         ALOGI("list the formats suppoted by ffmpeg: ");
1853         ALOGI("========================================");
1854         for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
1855                 ALOGV("format_names[%02d]: %s", i, FILE_FORMATS[i].format);
1856         }
1857         ALOGI("========================================");
1858
1859         for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
1860                 int len = strlen(FILE_FORMATS[i].format);
1861                 if (!av_strncasecmp(name, FILE_FORMATS[i].format, len)) {
1862                         container = FILE_FORMATS[i].container;
1863                         break;
1864                 }
1865         }
1866
1867         return container;
1868 }
1869
1870 static const char *SniffFFMPEGCommon(const char *url, float *confidence)
1871 {
1872         size_t i = 0;
1873         int err = 0;
1874         const char *container = NULL;
1875         AVFormatContext *ic = NULL;
1876         AVDictionary **opts = NULL;
1877         size_t orig_nb_streams = 0;
1878
1879         status_t status = initFFmpeg();
1880         if (status != OK) {
1881                 ALOGE("could not init ffmpeg");
1882                 return NULL;
1883         }
1884
1885         ic = avformat_alloc_context();
1886         if (!ic)
1887         {
1888                 ALOGE("oom for alloc avformat context");
1889                 goto fail;
1890         }
1891
1892         err = avformat_open_input(&ic, url, NULL, NULL);
1893         if (err < 0) {
1894         ALOGE("%s: avformat_open_input failed, err:%s", url, av_err2str(err));
1895                 goto fail;
1896         }
1897
1898         opts = setup_find_stream_info_opts(ic, codec_opts);
1899         orig_nb_streams = ic->nb_streams;
1900         err = avformat_find_stream_info(ic, opts);
1901         if (err < 0) {
1902         ALOGE("%s: could not find stream info, err:%s", url, av_err2str(err));
1903                 goto fail;
1904         }
1905         for (i = 0; i < orig_nb_streams; i++) {
1906                 av_dict_free(&opts[i]);
1907         }
1908         av_freep(&opts);
1909
1910         av_dump_format(ic, 0, url, 0);
1911
1912         ALOGI("FFmpegExtrator, url: %s, format_name: %s, format_long_name: %s",
1913                         url, ic->iformat->name, ic->iformat->long_name);
1914
1915         container = findMatchingContainer(ic->iformat->name);
1916
1917         if (container) {
1918                 adjustConfidenceIfNeeded(container, ic, confidence);
1919         }
1920
1921 fail:
1922         if (ic) {
1923                 avformat_close_input(&ic);
1924                 av_free(ic);
1925         }
1926         if (status == OK) {
1927                 deInitFFmpeg();
1928         }
1929
1930         return container;
1931 }
1932
1933 static const char *LegacySniffFFMPEG(const sp<DataSource> &source, float *confidence)
1934 {
1935         String8 uri = source->getUri();
1936         if (uri.empty()) {
1937                 return NULL;
1938         }
1939
1940         ALOGI("source url:%s", uri.string());
1941
1942         return SniffFFMPEGCommon(uri.string(), confidence);
1943 }
1944
1945 static const char *BetterSniffFFMPEG(const sp<DataSource> &source, float *confidence)
1946 {
1947         char url[128] = {0};
1948
1949         ALOGI("android-source:%p", source.get());
1950
1951         // pass the addr of smart pointer("source")
1952         snprintf(url, sizeof(url), "android-source:%p", source.get());
1953
1954         return SniffFFMPEGCommon(url, confidence);
1955 }
1956
1957 bool SniffFFMPEG(
1958         const sp<DataSource> &source, String8 *mimeType, float *confidence,
1959         sp<AMessage> *meta) {
1960         ALOGV("SniffFFMPEG");
1961
1962         *confidence = 0.08f;  // be the last resort, by default
1963
1964         const char *container = BetterSniffFFMPEG(source, confidence);
1965         if (!container) {
1966                 ALOGW("sniff through BetterSniffFFMPEG failed, try LegacySniffFFMPEG");
1967                 container = LegacySniffFFMPEG(source, confidence);
1968                 if (container) {
1969                         ALOGI("sniff through LegacySniffFFMPEG success");
1970                 }
1971         } else {
1972                 ALOGI("sniff through BetterSniffFFMPEG success");
1973         }
1974
1975         if (container == NULL) {
1976                 ALOGD("SniffFFMPEG failed to sniff this source");
1977                 return false;
1978         }
1979
1980         ALOGD("ffmpeg detected media content as '%s' with confidence %.2f",
1981                         container, *confidence);
1982
1983         /* use MPEG4Extractor(not extended extractor) for HTTP source only */
1984         if (!strcasecmp(container, MEDIA_MIMETYPE_CONTAINER_MPEG4)
1985                         && (source->flags() & DataSource::kIsCachingDataSource)) {
1986                 ALOGI("support container: %s, but it is caching data source, "
1987                                 "Don't use ffmpegextractor", container);
1988                 return false;
1989         }
1990
1991         mimeType->setTo(container);
1992
1993         *meta = new AMessage;
1994         (*meta)->setString("extended-extractor", "extended-extractor");
1995         (*meta)->setString("extended-extractor-subtype", "ffmpegextractor");
1996
1997         //debug only
1998         char value[PROPERTY_VALUE_MAX];
1999         property_get("sys.media.parser.ffmpeg", value, "0");
2000         if (atoi(value)) {
2001                 ALOGI("[debug] parser use ffmpeg");
2002                 *confidence = 0.88f;
2003         }
2004
2005         if (*confidence > 0.08f) {
2006                 (*meta)->setString("extended-extractor-use", "ffmpegextractor");
2007         }
2008
2009         return true;
2010 }
2011
2012 MediaExtractor *CreateFFmpegExtractor(const sp<DataSource> &source, const char *mime, const sp<AMessage> &meta) {
2013     MediaExtractor *ret = NULL;
2014     AString notuse;
2015     if (meta.get() && meta->findString("extended-extractor", &notuse) && (
2016             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4)     ||
2017             !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG)          ||
2018             !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AC3)           ||
2019             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MOV)       ||
2020             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MATROSKA)  ||
2021             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_TS)        ||
2022             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2PS)   ||
2023             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_AVI)       ||
2024             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_ASF)       ||
2025             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WEBM)      ||
2026             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WMV)       ||
2027             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPG)       ||
2028             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_FLV)       ||
2029             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_DIVX)      ||
2030             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_RM)        ||
2031             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV)       ||
2032             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_FLAC)      ||
2033             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_APE)       ||
2034             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_DTS)       ||
2035             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MP2)       ||
2036             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_RA)        ||
2037             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_OGG)       ||
2038             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_HEVC)      ||
2039             !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WMA))) {
2040         ret = new FFmpegExtractor(source);
2041     }
2042
2043     ALOGD("%ssupported mime: %s", (ret ? "" : "un"), mime);
2044     return ret;
2045 }
2046
2047 }  // namespace android
2048
2049 extern "C" void getExtractorPlugin(android::MediaExtractor::Plugin *plugin)
2050 {
2051     plugin->sniff = android::SniffFFMPEG;
2052     plugin->create = android::CreateFFmpegExtractor;
2053 }