OSDN Git Service

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