OSDN Git Service

remove SDL
[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 <limits.h> /* INT_MAX */
22
23 #include <media/stagefright/foundation/ABitReader.h>
24 #include <media/stagefright/foundation/ABuffer.h>
25 #include <media/stagefright/foundation/ADebug.h>
26 #include <media/stagefright/foundation/AMessage.h>
27 #include <media/stagefright/foundation/hexdump.h>
28 #include <media/stagefright/DataSource.h>
29 #include <media/stagefright/MediaBuffer.h>
30 #include <media/stagefright/MediaDebug.h>
31 #include <media/stagefright/MediaDefs.h>
32 #include <media/stagefright/MediaErrors.h>
33 #include <media/stagefright/MediaSource.h>
34 #include <media/stagefright/MetaData.h>
35 #include <media/stagefright/Utils.h>
36 #include <utils/String8.h>
37 #include <utils/misc.h>
38
39 #include "include/avc_utils.h"
40 #include "utils/common_utils.h"
41 #include "utils/ffmpeg_utils.h"
42 #include "FFmpegExtractor.h"
43
44 #define DEBUG_READ_ENTRY 0
45 #define DIABLE_VIDEO     0
46 #define DIABLE_AUDIO     0
47 #define WAIT_KEY_PACKET_AFTER_SEEK 1
48 #define DISABLE_NAL_TO_ANNEXB 0
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
55 #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - FF_INPUT_BUFFER_PADDING_SIZE)
56
57 enum {
58     NO_SEEK = 0,
59     SEEK,
60 };
61
62 static AVPacket flush_pkt;
63
64 namespace android {
65
66 struct FFmpegExtractor::Track : public MediaSource {
67     Track(const sp<FFmpegExtractor> &extractor, sp<MetaData> meta, bool isAVC,
68           AVStream *stream, PacketQueue *queue);
69
70     virtual status_t start(MetaData *params);
71     virtual status_t stop();
72     virtual sp<MetaData> getFormat();
73
74     virtual status_t read(
75             MediaBuffer **buffer, const ReadOptions *options);
76
77 protected:
78     virtual ~Track();
79
80 private:
81     friend struct FFmpegExtractor;
82
83     sp<FFmpegExtractor> mExtractor;
84     sp<MetaData> mMeta;
85
86     enum AVMediaType mMediaType;
87
88     mutable Mutex mLock;
89
90     bool mIsAVC;
91     size_t mNALLengthSize;
92     bool mNal2AnnexB;
93
94     AVStream *mStream;
95     PacketQueue *mQueue;
96
97     DISALLOW_EVIL_CONSTRUCTORS(Track);
98 };
99
100 ////////////////////////////////////////////////////////////////////////////////
101
102 FFmpegExtractor::FFmpegExtractor(const sp<DataSource> &source)
103     : mDataSource(source),
104       mReaderThreadStarted(false),
105       mInitCheck(NO_INIT) {
106     LOGV("FFmpegExtractor::FFmpegExtractor");
107
108     int err;
109     const char *url = mDataSource->getNamURI();
110     if (url == NULL) {
111         LOGI("url is error!");
112         return;
113     }
114     // is it right?
115     if (!strcmp(url, "-")) {
116         av_strlcpy(mFilename, "pipe:", strlen("pipe:") + 1);
117     } else {
118         av_strlcpy(mFilename, url, strlen(url) + 1);
119     }
120     LOGI("url: %s, mFilename: %s", url, mFilename);
121
122     err = initStreams();
123     if (err < 0) {
124         LOGE("failed to init ffmpeg");
125         return;
126     }
127
128     // start reader here, as we want to extract extradata from bitstream if no extradata
129     startReaderThread();
130
131     while(mProbePkts <= EXTRACTOR_MAX_PROBE_PACKETS && !mEOF &&
132         (mFormatCtx->pb ? !mFormatCtx->pb->error : 1) &&
133         (mDefersToCreateVideoTrack || mDefersToCreateAudioTrack)) {
134         // FIXME, i am so lazy! Should use pthread_cond_wait to wait conditions
135         NamDelay(5);
136     }
137
138     LOGV("mProbePkts: %d, mEOF: %d, pb->error(if has): %d, mDefersToCreateVideoTrack: %d, mDefersToCreateAudioTrack: %d",
139         mProbePkts, mEOF, mFormatCtx->pb ? mFormatCtx->pb->error : 0, mDefersToCreateVideoTrack, mDefersToCreateAudioTrack);
140
141     mInitCheck = OK;
142 }
143
144 FFmpegExtractor::~FFmpegExtractor() {
145     LOGV("FFmpegExtractor::~FFmpegExtractor");
146
147     // stop reader here if no track!
148     stopReaderThread();
149
150     deInitStreams();
151 }
152
153 size_t FFmpegExtractor::countTracks() {
154     return mInitCheck == OK ? mTracks.size() : 0;
155 }
156
157 sp<MediaSource> FFmpegExtractor::getTrack(size_t index) {
158     LOGV("FFmpegExtractor::getTrack[%d]", index);
159
160     if (mInitCheck != OK) {
161         return NULL;
162     }
163
164     if (index >= mTracks.size()) {
165         return NULL;
166     }
167
168     return mTracks.valueAt(index);
169 }
170
171 sp<MetaData> FFmpegExtractor::getTrackMetaData(size_t index, uint32_t flags) {
172     LOGV("FFmpegExtractor::getTrackMetaData[%d]", index);
173
174     if (mInitCheck != OK) {
175         return NULL;
176     }
177
178     if (index >= mTracks.size()) {
179         return NULL;
180     }
181
182     return mTracks.valueAt(index)->getFormat();
183 }
184
185 sp<MetaData> FFmpegExtractor::getMetaData() {
186     LOGV("FFmpegExtractor::getMetaData");
187
188     if (mInitCheck != OK) {
189         return NULL;
190     }
191
192     sp<MetaData> meta = new MetaData;
193     // TODO
194     meta->setCString(kKeyMIMEType, "video/ffmpeg");
195
196     return meta;
197 }
198
199 uint32_t FFmpegExtractor::flags() const {
200     LOGV("FFmpegExtractor::flags");
201
202     if (mInitCheck != OK) {
203         return NULL;
204     }
205
206     uint32_t flags = CAN_PAUSE;
207
208     if (mFormatCtx->duration != AV_NOPTS_VALUE) {
209         flags |= CAN_SEEK_BACKWARD | CAN_SEEK_FORWARD | CAN_SEEK;
210     }
211
212     return flags;
213 }
214
215 void FFmpegExtractor::packet_queue_init(PacketQueue *q)
216 {
217     memset(q, 0, sizeof(PacketQueue));
218     pthread_mutex_init(&q->mutex, NULL);
219     pthread_cond_init(&q->cond, NULL);
220     packet_queue_put(q, &flush_pkt);
221 }
222
223 void FFmpegExtractor::packet_queue_flush(PacketQueue *q)
224 {
225     AVPacketList *pkt, *pkt1;
226
227     pthread_mutex_lock(&q->mutex);
228     for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
229         pkt1 = pkt->next;
230         av_free_packet(&pkt->pkt);
231         av_freep(&pkt);
232     }
233     q->last_pkt = NULL;
234     q->first_pkt = NULL;
235     q->nb_packets = 0;
236     q->size = 0;
237     pthread_mutex_unlock(&q->mutex);
238 }
239
240 void FFmpegExtractor::packet_queue_end(PacketQueue *q)
241 {
242     packet_queue_flush(q);
243 }
244
245 void FFmpegExtractor::packet_queue_abort(PacketQueue *q)
246 {
247     pthread_mutex_lock(&q->mutex);
248
249     q->abort_request = 1;
250
251     pthread_cond_signal(&q->cond);
252
253     pthread_mutex_unlock(&q->mutex);
254 }
255
256 int FFmpegExtractor::packet_queue_put(PacketQueue *q, AVPacket *pkt)
257 {
258     AVPacketList *pkt1;
259
260     /* duplicate the packet */
261     if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
262         return -1;
263
264     pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
265     if (!pkt1)
266         return -1;
267     pkt1->pkt = *pkt;
268     pkt1->next = NULL;
269
270     pthread_mutex_lock(&q->mutex);
271
272     if (!q->last_pkt)
273
274         q->first_pkt = pkt1;
275     else
276         q->last_pkt->next = pkt1;
277     q->last_pkt = pkt1;
278     q->nb_packets++;
279     //q->size += pkt1->pkt.size + sizeof(*pkt1);
280     q->size += pkt1->pkt.size;
281     pthread_cond_signal(&q->cond);
282
283     pthread_mutex_unlock(&q->mutex);
284     return 0;
285 }
286
287 /* packet queue handling */
288 /* return < 0 if aborted, 0 if no packet and > 0 if packet.  */
289 int FFmpegExtractor::packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
290 {
291     AVPacketList *pkt1;
292     int ret;
293
294     pthread_mutex_lock(&q->mutex);
295
296     for (;;) {
297         if (q->abort_request) {
298             ret = -1;
299             break;
300         }
301
302         pkt1 = q->first_pkt;
303         if (pkt1) {
304             q->first_pkt = pkt1->next;
305             if (!q->first_pkt)
306                 q->last_pkt = NULL;
307             q->nb_packets--;
308             //q->size -= pkt1->pkt.size + sizeof(*pkt1);
309             q->size -= pkt1->pkt.size;
310             *pkt = pkt1->pkt;
311             av_free(pkt1);
312             ret = 1;
313             break;
314         } else if (!block) {
315             ret = 0;
316             break;
317         } else {
318             pthread_cond_wait(&q->cond, &q->mutex);
319         }
320     }
321     pthread_mutex_unlock(&q->mutex);
322     return ret;
323 }
324
325 static void EncodeSize14(uint8_t **_ptr, size_t size) {
326     CHECK_LE(size, 0x3fff);
327
328     uint8_t *ptr = *_ptr;
329
330     *ptr++ = 0x80 | (size >> 7);
331     *ptr++ = size & 0x7f;
332
333     *_ptr = ptr;
334 }
335
336 static sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) {
337     sp<ABuffer> esds = new ABuffer(csd->size() + 25);
338
339     uint8_t *ptr = esds->data();
340     *ptr++ = 0x03;
341     EncodeSize14(&ptr, 22 + csd->size());
342
343     *ptr++ = 0x00;  // ES_ID
344     *ptr++ = 0x00;
345
346     *ptr++ = 0x00;  // streamDependenceFlag, URL_Flag, OCRstreamFlag
347
348     *ptr++ = 0x04;
349     EncodeSize14(&ptr, 16 + csd->size());
350
351     *ptr++ = 0x40;  // Audio ISO/IEC 14496-3
352
353     for (size_t i = 0; i < 12; ++i) {
354         *ptr++ = 0x00;
355     }
356
357     *ptr++ = 0x05;
358     EncodeSize14(&ptr, csd->size());
359
360     memcpy(ptr, csd->data(), csd->size());
361
362     return esds;
363 }
364
365 // the same as MakeMPEGVideoESDS
366 static sp<ABuffer> MakeRawCodecSpecificData(const sp<ABuffer> &csd) {
367     sp<ABuffer> esds = new ABuffer(csd->size() + 25);
368
369     uint8_t *ptr = esds->data();
370     *ptr++ = 0x03;
371     EncodeSize14(&ptr, 22 + csd->size());
372
373     *ptr++ = 0x00;  // ES_ID
374     *ptr++ = 0x00;
375
376     *ptr++ = 0x00;  // streamDependenceFlag, URL_Flag, OCRstreamFlag
377
378     *ptr++ = 0x04;
379     EncodeSize14(&ptr, 16 + csd->size());
380
381     *ptr++ = 0x40;  // Audio ISO/IEC 14496-3
382
383     for (size_t i = 0; i < 12; ++i) {
384         *ptr++ = 0x00;
385     }
386
387     *ptr++ = 0x05;
388     EncodeSize14(&ptr, csd->size());
389
390     memcpy(ptr, csd->data(), csd->size());
391
392     return esds;
393 }
394
395 // Returns the sample rate based on the sampling frequency index
396 static uint32_t get_sample_rate(const uint8_t sf_index)
397 {
398     static const uint32_t sample_rates[] =
399     {
400         96000, 88200, 64000, 48000, 44100, 32000,
401         24000, 22050, 16000, 12000, 11025, 8000
402     };
403
404     if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
405         return sample_rates[sf_index];
406     }
407
408     return 0;
409 }
410
411 int FFmpegExtractor::check_extradata(AVCodecContext *avctx)
412 {
413     const char *name;
414     bool *defersToCreateTrack;
415     AVBitStreamFilterContext **bsfc;
416
417     // init
418     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
419         bsfc = &mVideoBsfc;
420         defersToCreateTrack = &mDefersToCreateVideoTrack;
421     } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO){
422         bsfc = &mAudioBsfc;
423         defersToCreateTrack = &mDefersToCreateAudioTrack;
424     }
425
426     // ignore extradata
427     if (avctx->codec_id == CODEC_ID_MP3 ||
428             avctx->codec_id == CODEC_ID_MP1  ||
429             avctx->codec_id == CODEC_ID_MP2  ||
430             avctx->codec_id == CODEC_ID_AC3  ||
431             avctx->codec_id == CODEC_ID_H263  ||
432             avctx->codec_id == CODEC_ID_H263P ||
433             avctx->codec_id == CODEC_ID_H263I ||
434             avctx->codec_id == CODEC_ID_WMV1)
435         return 1;
436
437     // is extradata compatible with android?
438     if (avctx->codec_id != CODEC_ID_AAC) {
439         int is_compatible = is_extradata_compatible_with_android(avctx);
440         if (!is_compatible) {
441             LOGI("%s extradata is not compatible with android, should to extract it from bitstream",
442                     av_get_media_type_string(avctx->codec_type));
443             *defersToCreateTrack = true;
444             *bsfc = NULL; // H264 don't need bsfc, only AAC?
445             return 0;
446         }
447         return 1;
448     }
449
450     if (avctx->codec_id == CODEC_ID_AAC) {
451         name = "aac_adtstoasc";
452     }
453
454     if (avctx->extradata_size <= 0) {
455         LOGI("No %s extradata found, should to extract it from bitstream",
456                 av_get_media_type_string(avctx->codec_type));
457         *defersToCreateTrack = true;
458          //CHECK(name != NULL);
459         if (!*bsfc && name) {
460             *bsfc = av_bitstream_filter_init(name);
461             if (!*bsfc) {
462                 LOGE("Cannot open the %s BSF!", name);
463                 *defersToCreateTrack = false;
464                 return -1;
465             } else {
466                 LOGV("open the %s bsf", name);
467                 return 0;
468             }
469         } else {
470             return 0;
471         }
472     }
473     return 1;
474 }
475
476
477 int FFmpegExtractor::stream_component_open(int stream_index)
478 {
479     AVCodecContext *avctx;
480     sp<MetaData> meta;
481     bool isAVC = false;
482     bool supported = false;
483     uint32_t type;
484     const void *data;
485     size_t size;
486     int ret;
487
488     LOGI("stream_index: %d", stream_index);
489     if (stream_index < 0 || stream_index >= mFormatCtx->nb_streams)
490         return -1;
491     avctx = mFormatCtx->streams[stream_index]->codec;
492
493     switch(avctx->codec_id) {
494     case CODEC_ID_H264:
495     case CODEC_ID_MPEG4:
496     case CODEC_ID_H263:
497     case CODEC_ID_H263P:
498     case CODEC_ID_H263I:
499     case CODEC_ID_AAC:
500     case CODEC_ID_AC3:
501     case CODEC_ID_MP1:
502     case CODEC_ID_MP2:
503     case CODEC_ID_MP3:
504     case CODEC_ID_MPEG2VIDEO:
505     case CODEC_ID_WMV1:
506     case CODEC_ID_WMV2:
507     case CODEC_ID_WMAV2:
508 #if 0
509     case CODEC_ID_VC1:
510 #endif
511         supported = true;
512         break;
513     default:
514         supported = false;
515         break;
516     }
517
518     if (!supported) {
519         LOGE("unsupport the codec, id: 0x%0x", avctx->codec_id);
520         return -1;
521     }
522     LOGV("support the codec");
523
524     unsigned streamType;
525     ssize_t index = mTracks.indexOfKey(stream_index);
526
527     if (index >= 0) {
528         LOGE("this track already exists");
529         return 0;
530     }
531
532     mFormatCtx->streams[stream_index]->discard = AVDISCARD_DEFAULT;
533
534     char tagbuf[32];
535     av_get_codec_tag_string(tagbuf, sizeof(tagbuf), avctx->codec_tag);
536     LOGV("Tag %s/0x%08x with codec id '%d'\n", tagbuf, avctx->codec_tag, avctx->codec_id);
537
538     switch (avctx->codec_type) {
539     case AVMEDIA_TYPE_VIDEO:
540         if (mVideoStreamIdx == -1)
541             mVideoStreamIdx = stream_index;
542         if (mVideoStream == NULL)
543             mVideoStream = mFormatCtx->streams[stream_index];
544         if (!mVideoQInited) {
545             packet_queue_init(&mVideoQ);
546             mVideoQInited = true;
547         }
548
549         ret = check_extradata(avctx);
550         if (ret != 1) {
551             if (ret == -1) {
552                 // disable the stream
553                 mVideoStreamIdx = -1;
554                 mVideoStream = NULL;
555                 packet_queue_end(&mVideoQ);
556                 mVideoQInited =  false;
557                 mFormatCtx->streams[stream_index]->discard = AVDISCARD_ALL;
558             }
559             return ret;
560          }
561
562         if (avctx->extradata) {
563             LOGV("video stream extradata:");
564             hexdump(avctx->extradata, avctx->extradata_size);
565         } else {
566             LOGV("video stream no extradata, but we can ignore it.");
567         }
568
569         meta = new MetaData;
570
571         switch(avctx->codec_id) {
572         case CODEC_ID_H264:
573             /**
574              * H.264 Video Types
575              * http://msdn.microsoft.com/en-us/library/dd757808(v=vs.85).aspx
576              */
577             //if (avctx->codec_tag && avctx->codec_tag == AV_RL32("avc1")) {
578             if (avctx->extradata[0] == 1 /* configurationVersion */) {
579                 // H.264 bitstream without start codes.
580                 isAVC = true;
581                 LOGV("AVC");
582
583                 if (avctx->width == 0 || avctx->height == 0) {
584                     int32_t width, height;
585                     sp<ABuffer> seqParamSet = new ABuffer(avctx->extradata_size - 8);
586                     memcpy(seqParamSet->data(), avctx->extradata + 8, avctx->extradata_size - 8);
587                     FindAVCDimensions(seqParamSet, &width, &height);
588                     avctx->width  = width;
589                     avctx->height = height;
590                 }
591
592                 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
593                 meta->setData(kKeyAVCC, kTypeAVCC, avctx->extradata, avctx->extradata_size);
594             } else {
595                 // H.264 bitstream with start codes.
596                 isAVC = false;
597                 LOGV("H264");
598
599                 /* set NULL to release meta as we will new a meta in MakeAVCCodecSpecificData() fxn */
600                 meta->clear();
601                 meta = NULL;
602
603                 sp<ABuffer> buffer = new ABuffer(avctx->extradata_size);
604                 memcpy(buffer->data(), avctx->extradata, avctx->extradata_size);
605                 meta = MakeAVCCodecSpecificData(buffer);
606             }
607             break;
608         case CODEC_ID_MPEG4:
609             LOGV("MPEG4");
610             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
611             {
612                 sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
613                 memcpy(csd->data(), avctx->extradata, avctx->extradata_size);
614                 sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
615                 meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
616             }
617             break;
618         case CODEC_ID_H263:
619         case CODEC_ID_H263P:
620         case CODEC_ID_H263I:
621             LOGV("H263");
622             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
623             break;
624         case CODEC_ID_MPEG2VIDEO:
625             LOGV("MPEG2VIDEO");
626             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
627             {
628                 sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
629                 memcpy(csd->data(), avctx->extradata, avctx->extradata_size);
630                 sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
631                 meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
632             }
633             break;
634         case CODEC_ID_VC1:
635             LOGV("VC1");
636             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV12);
637             meta->setData(kKeyESDS, kTypeESDS, avctx->extradata, avctx->extradata_size);
638             break;
639         case CODEC_ID_WMV1:
640             LOGV("WMV1");
641             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV12);
642             break;
643         case CODEC_ID_WMV2:
644             LOGV("WMV2");
645             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV12);
646             {
647                 sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
648                 memcpy(csd->data(), avctx->extradata, avctx->extradata_size);
649                 sp<ABuffer> esds = MakeRawCodecSpecificData(csd);
650                 meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
651             }
652             break;
653         default:
654             CHECK(!"Should not be here. Unsupported codec.");
655             break;
656         }
657
658         LOGI("width: %d, height: %d, bit_rate: %d", avctx->width, avctx->height, avctx->bit_rate);
659
660         meta->setInt32(kKeyWidth, avctx->width);
661         meta->setInt32(kKeyHeight, avctx->height);
662         if (avctx->bit_rate > 0)
663             meta->setInt32(kKeyBitRate, avctx->bit_rate);
664         if (mFormatCtx->duration != AV_NOPTS_VALUE)
665             meta->setInt64(kKeyDuration, mFormatCtx->duration);
666
667         LOGV("create a video track");
668         index = mTracks.add(
669             stream_index, new Track(this, meta, isAVC, mVideoStream, &mVideoQ));
670
671         mDefersToCreateVideoTrack = false;
672
673         break;
674     case AVMEDIA_TYPE_AUDIO:
675         if (mAudioStreamIdx == -1)
676             mAudioStreamIdx = stream_index;
677         if (mAudioStream == NULL)
678             mAudioStream = mFormatCtx->streams[stream_index];
679         if (!mAudioQInited) {
680             packet_queue_init(&mAudioQ);
681             mAudioQInited = true;
682         }
683
684         ret = check_extradata(avctx);
685         if (ret != 1) {
686             if (ret == -1) {
687                 // disable the stream
688                 mAudioStreamIdx = -1;
689                 mAudioStream = NULL;
690                 packet_queue_end(&mAudioQ);
691                 mAudioQInited =  false;
692                 mFormatCtx->streams[stream_index]->discard = AVDISCARD_ALL;
693             }
694             return ret;
695         }
696
697         if (avctx->extradata) {
698             LOGV("audio stream extradata:");
699             hexdump(avctx->extradata, avctx->extradata_size);
700         } else {
701             LOGV("audio stream no extradata, but we can ignore it.");
702         }
703
704         switch(avctx->codec_id) {
705         case CODEC_ID_MP1:
706             LOGV("MP1");
707             meta = new MetaData;
708             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
709             break;
710         case CODEC_ID_MP2:
711             LOGV("MP2");
712             meta = new MetaData;
713             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
714             break;
715         case CODEC_ID_MP3:
716             LOGV("MP3");
717             meta = new MetaData;
718             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
719             break;
720         case CODEC_ID_AC3:
721             LOGV("AC3");
722             meta = new MetaData;
723             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3);
724             break;
725         case CODEC_ID_AAC:
726             LOGV("AAC"); 
727             uint32_t sr;
728             const uint8_t *header;
729             uint8_t profile, sf_index, channel;
730
731             header = avctx->extradata;
732             CHECK(header != NULL);
733
734             // AudioSpecificInfo follows
735             // oooo offf fccc c000
736             // o - audioObjectType
737             // f - samplingFreqIndex
738             // c - channelConfig
739             profile = ((header[0] & 0xf8) >> 3) - 1;
740             sf_index = (header[0] & 0x07) << 1 | (header[1] & 0x80) >> 7;
741             sr = get_sample_rate(sf_index);
742             if (sr == 0) {
743                 LOGE("unsupport the sample rate");
744                 return -1;
745             }
746             channel = (header[1] >> 3) & 0xf;
747             LOGV("profile: %d, sf_index: %d, channel: %d", profile, sf_index, channel);
748
749             meta = MakeAACCodecSpecificData(profile, sf_index, channel);
750             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
751             break;
752         case CODEC_ID_WMAV2:
753             LOGV("WMAV2");
754             meta = new MetaData;
755             meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
756             {
757                 sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
758                 memcpy(csd->data(), avctx->extradata, avctx->extradata_size);
759                 sp<ABuffer> esds = MakeRawCodecSpecificData(csd);
760                 meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
761             }
762             break;
763         default:
764             CHECK(!"Should not be here. Unsupported codec.");
765             break;
766         }
767
768         LOGI("bit_rate: %d, sample_rate: %d, channels: %d", avctx->bit_rate, avctx->sample_rate, avctx->channels);
769
770         meta->setInt32(kKeySampleRate, avctx->sample_rate);
771         meta->setInt32(kKeyChannelCount, avctx->channels);
772         meta->setInt32(kKeyBitRate, avctx->bit_rate);
773         if (mFormatCtx->duration != AV_NOPTS_VALUE)
774             meta->setInt64(kKeyDuration, mFormatCtx->duration);
775
776         if (avctx->codec_id != CODEC_ID_MP3 &&
777                 avctx->codec_id != CODEC_ID_MP1 &&
778                 avctx->codec_id != CODEC_ID_MP2 &&
779                 avctx->codec_id != CODEC_ID_AC3) {
780             LOGV("audio meta esds:");
781             CHECK(meta->findData(kKeyESDS, &type, &data, &size));
782             hexdump(data, size);
783         }
784
785         LOGV("create a audio track");
786         index = mTracks.add(
787             stream_index, new Track(this, meta, false, mAudioStream, &mAudioQ));
788
789         mDefersToCreateAudioTrack = false;
790
791         break;
792     case AVMEDIA_TYPE_SUBTITLE:
793         /* Unsupport now */
794         CHECK(!"Should not be here. Unsupported media type.");
795         break;
796     default:
797         CHECK(!"Should not be here. Unsupported media type.");
798         break;
799     }
800     return 0;
801 }
802
803 void FFmpegExtractor::stream_component_close(int stream_index)
804 {
805     AVCodecContext *avctx;
806
807     if (stream_index < 0 || stream_index >= mFormatCtx->nb_streams)
808         return;
809     avctx = mFormatCtx->streams[stream_index]->codec;
810
811     switch (avctx->codec_type) {
812     case AVMEDIA_TYPE_VIDEO:
813         LOGV("packet_queue_abort videoq");
814         packet_queue_abort(&mVideoQ);
815         /* wait until the end */
816         while (!mAbortRequest && !mVideoEOSReceived) {
817             LOGV("wait for video received");
818             NamDelay(10);
819         }
820         LOGV("packet_queue_end videoq");
821         packet_queue_end(&mVideoQ);
822         break;
823     case AVMEDIA_TYPE_AUDIO:
824         LOGV("packet_queue_abort audioq");
825         packet_queue_abort(&mAudioQ);
826         while (!mAbortRequest && !mAudioEOSReceived) {
827             LOGV("wait for audio received");
828             NamDelay(10);
829         }
830         LOGV("packet_queue_end audioq");
831         packet_queue_end(&mAudioQ);
832         break;
833     case AVMEDIA_TYPE_SUBTITLE:
834         break;
835     default:
836         break;
837     }
838
839     mFormatCtx->streams[stream_index]->discard = AVDISCARD_ALL;
840     switch (avctx->codec_type) {
841     case AVMEDIA_TYPE_VIDEO:
842         mVideoStream    = NULL;
843         mVideoStreamIdx = -1;
844         if (mVideoBsfc) {
845             av_bitstream_filter_close(mVideoBsfc);
846             mVideoBsfc  = NULL;
847         }
848         break;
849     case AVMEDIA_TYPE_AUDIO:
850         mAudioStream    = NULL;
851         mAudioStreamIdx = -1;
852         if (mAudioBsfc) {
853             av_bitstream_filter_close(mAudioBsfc);
854             mAudioBsfc  = NULL;
855         }
856         break;
857     case AVMEDIA_TYPE_SUBTITLE:
858         break;
859     default:
860         break;
861     }
862 }
863
864 void FFmpegExtractor::reachedEOS(enum AVMediaType media_type)
865 {
866     Mutex::Autolock autoLock(mLock);
867
868     if (media_type == AVMEDIA_TYPE_VIDEO) {
869         mVideoEOSReceived = true;
870     } else if (media_type == AVMEDIA_TYPE_AUDIO) {
871         mAudioEOSReceived = true;
872     }
873 }
874
875 /* seek in the stream */
876 int FFmpegExtractor::stream_seek(int64_t pos, enum AVMediaType media_type)
877 {
878     Mutex::Autolock autoLock(mLock);
879
880     if (mVideoStreamIdx >= 0 &&
881         mAudioStreamIdx >= 0 &&
882         media_type == AVMEDIA_TYPE_AUDIO &&
883         !mVideoEOSReceived) {
884        return NO_SEEK;
885     }
886
887     // flush immediately
888     if (mAudioStreamIdx >= 0)
889         packet_queue_flush(&mAudioQ);
890     if (mVideoStreamIdx >= 0)
891         packet_queue_flush(&mVideoQ);
892
893     mSeekPos = pos;
894     mSeekFlags &= ~AVSEEK_FLAG_BYTE;
895     mSeekReq = 1;
896
897     return SEEK;
898 }
899
900 // staitc
901 int FFmpegExtractor::decode_interrupt_cb(void *ctx)
902 {
903     FFmpegExtractor *extrator = static_cast<FFmpegExtractor *>(ctx);
904     return extrator->mAbortRequest;
905 }
906
907 void FFmpegExtractor::print_error_ex(const char *filename, int err)
908 {
909     char errbuf[128];
910     const char *errbuf_ptr = errbuf;
911
912     if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
913         errbuf_ptr = strerror(AVUNERROR(err));
914     LOGI("%s: %s\n", filename, errbuf_ptr);
915 }
916
917 void FFmpegExtractor::setFFmpegDefaultOpts()
918 {
919     mGenPTS       = 0;
920 #if DIABLE_VIDEO
921     mVideoDisable = 1;
922 #else
923     mVideoDisable = 0;
924 #endif
925 #if DIABLE_AUDIO
926     mAudioDisable = 1;
927 #else
928     mAudioDisable = 0;
929 #endif
930     mShowStatus   = 1;
931     mSeekByBytes  = 0; /* seek by bytes 0=off 1=on -1=auto" */
932     mStartTime    = AV_NOPTS_VALUE;
933     mDuration     = AV_NOPTS_VALUE;
934     mSeekPos      = AV_NOPTS_VALUE;
935     mAutoExit     = 1;
936     mLoop         = 1;
937
938     mVideoStreamIdx = -1;
939     mAudioStreamIdx = -1;
940     mVideoStream  = NULL;
941     mAudioStream  = NULL;
942     mVideoQInited = false;
943     mAudioQInited = false;
944     mDefersToCreateVideoTrack = false;
945     mDefersToCreateAudioTrack = false;
946     mVideoBsfc = NULL;
947     mAudioBsfc = NULL;
948
949     mAbortRequest = 0;
950     mPaused       = 0;
951     mLastPaused   = 0;
952     mSeekReq      = 0;
953
954     mProbePkts    = 0;
955     mEOF          = false;
956 }
957
958 int FFmpegExtractor::initStreams()
959 {
960     int err, i;
961     status_t status;
962     int eof = 0;
963     int ret = 0, audio_ret = 0, video_ret = 0;
964     int pkt_in_play_range = 0;
965     AVDictionaryEntry *t;
966     AVDictionary **opts;
967     int orig_nb_streams;
968     int st_index[AVMEDIA_TYPE_NB] = {0};
969     int wanted_stream[AVMEDIA_TYPE_NB] = {0};
970     st_index[AVMEDIA_TYPE_AUDIO]  = -1;
971     st_index[AVMEDIA_TYPE_VIDEO]  = -1;
972     wanted_stream[AVMEDIA_TYPE_AUDIO]  = -1;
973     wanted_stream[AVMEDIA_TYPE_VIDEO]  = -1;
974
975     setFFmpegDefaultOpts();
976
977     status = initFFmpeg();
978     if (status != OK) {
979         ret = -1;
980         goto fail;
981     }
982
983     av_init_packet(&flush_pkt);
984     flush_pkt.data = (uint8_t *)"FLUSH";
985     flush_pkt.size = 0;
986
987     mFormatCtx = avformat_alloc_context();
988     mFormatCtx->interrupt_callback.callback = decode_interrupt_cb;
989     mFormatCtx->interrupt_callback.opaque = this;
990     LOGV("mFilename: %s", mFilename);
991     err = avformat_open_input(&mFormatCtx, mFilename, NULL, &format_opts);
992     if (err < 0) {
993         print_error_ex(mFilename, err);
994         ret = -1;
995         goto fail;
996     }
997     if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
998         LOGE("Option %s not found.\n", t->key);
999         //ret = AVERROR_OPTION_NOT_FOUND;
1000         ret = -1;
1001         goto fail;
1002     }
1003
1004     if (mGenPTS)
1005         mFormatCtx->flags |= AVFMT_FLAG_GENPTS;
1006
1007     opts = setup_find_stream_info_opts(mFormatCtx, codec_opts);
1008     orig_nb_streams = mFormatCtx->nb_streams;
1009
1010     err = avformat_find_stream_info(mFormatCtx, opts);
1011     if (err < 0) {
1012         LOGE("%s: could not find codec parameters\n", mFilename);
1013         ret = -1;
1014         goto fail;
1015     }
1016     for (i = 0; i < orig_nb_streams; i++)
1017         av_dict_free(&opts[i]);
1018     av_freep(&opts);
1019
1020     if (mFormatCtx->pb)
1021         mFormatCtx->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
1022
1023     if (mSeekByBytes < 0)
1024         mSeekByBytes = !!(mFormatCtx->iformat->flags & AVFMT_TS_DISCONT);
1025
1026     /* if seeking requested, we execute it */
1027     if (mStartTime != AV_NOPTS_VALUE) {
1028         int64_t timestamp;
1029
1030         timestamp = mStartTime;
1031         /* add the stream start time */
1032         if (mFormatCtx->start_time != AV_NOPTS_VALUE)
1033             timestamp += mFormatCtx->start_time;
1034         ret = avformat_seek_file(mFormatCtx, -1, INT64_MIN, timestamp, INT64_MAX, 0);
1035         if (ret < 0) {
1036             LOGE("%s: could not seek to position %0.3f",
1037                     mFilename, (double)timestamp / AV_TIME_BASE);
1038             goto fail;
1039         }
1040     }
1041
1042     for (i = 0; i < mFormatCtx->nb_streams; i++)
1043         mFormatCtx->streams[i]->discard = AVDISCARD_ALL;
1044     if (!mVideoDisable)
1045         st_index[AVMEDIA_TYPE_VIDEO] =
1046             av_find_best_stream(mFormatCtx, AVMEDIA_TYPE_VIDEO,
1047                                 wanted_stream[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
1048     if (!mAudioDisable)
1049         st_index[AVMEDIA_TYPE_AUDIO] =
1050             av_find_best_stream(mFormatCtx, AVMEDIA_TYPE_AUDIO,
1051                                 wanted_stream[AVMEDIA_TYPE_AUDIO],
1052                                 st_index[AVMEDIA_TYPE_VIDEO],
1053                                 NULL, 0);
1054     if (mShowStatus) {
1055         av_dump_format(mFormatCtx, 0, mFilename, 0);
1056     }
1057
1058     if (mFormatCtx->duration != AV_NOPTS_VALUE) {
1059         int hours, mins, secs, us;
1060         secs = mFormatCtx->duration / AV_TIME_BASE;
1061         us = mFormatCtx->duration % AV_TIME_BASE;
1062         mins = secs / 60;
1063         secs %= 60;
1064         hours = mins / 60;
1065         mins %= 60;
1066         LOGI("the duration is %02d:%02d:%02d.%02d", hours, mins, secs, (100 * us) / AV_TIME_BASE);
1067     }
1068
1069     if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
1070         audio_ret = stream_component_open(st_index[AVMEDIA_TYPE_AUDIO]);
1071     }
1072
1073     if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
1074         video_ret = stream_component_open(st_index[AVMEDIA_TYPE_VIDEO]);
1075     }
1076
1077     if ( audio_ret < 0 && video_ret < 0) {
1078         LOGE("%s: could not open codecs\n", mFilename);
1079         ret = -1;
1080         goto fail;
1081     }
1082
1083     ret = 0;
1084
1085 fail:
1086     return ret;
1087 }
1088
1089 void FFmpegExtractor::deInitStreams()
1090 {
1091     if (mFormatCtx) {
1092         avformat_close_input(&mFormatCtx);
1093     }
1094
1095     deInitFFmpeg();
1096 }
1097
1098 status_t FFmpegExtractor::startReaderThread() {
1099     LOGV("Starting reader thread");
1100     Mutex::Autolock autoLock(mLock);
1101
1102     if (mReaderThreadStarted)
1103         return OK;
1104
1105     pthread_attr_t attr;
1106     pthread_attr_init(&attr);
1107     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1108     pthread_create(&mReaderThread, &attr, ReaderWrapper, this);
1109     pthread_attr_destroy(&attr);
1110     mReaderThreadStarted = true;
1111     LOGD("Reader thread started");
1112
1113     return OK;
1114 }
1115
1116 void FFmpegExtractor::stopReaderThread() {
1117     LOGV("Stopping reader thread");
1118     Mutex::Autolock autoLock(mLock);
1119
1120     if (!mReaderThreadStarted) {
1121         LOGD("Reader thread have been stopped");
1122         return;
1123     }
1124
1125     mAbortRequest = 1;
1126
1127     void *dummy;
1128     pthread_join(mReaderThread, &dummy);
1129     mReaderThreadStarted = false;
1130     LOGD("Reader thread stopped");
1131 }
1132
1133 // static
1134 void *FFmpegExtractor::ReaderWrapper(void *me) {
1135     ((FFmpegExtractor *)me)->readerEntry();
1136
1137     return NULL;
1138 }
1139
1140 void FFmpegExtractor::readerEntry() {
1141     int err, i, ret;
1142     AVPacket pkt1, *pkt = &pkt1;
1143     int eof = 0;
1144     int pkt_in_play_range = 0;
1145
1146     LOGV("FFmpegExtractor::readerEntry");
1147
1148     mVideoEOSReceived = false;
1149     mAudioEOSReceived = false;
1150
1151     for (;;) {
1152         if (mAbortRequest)
1153             break;
1154
1155         if (mPaused != mLastPaused) {
1156             mLastPaused = mPaused;
1157             if (mPaused)
1158                 mReadPauseReturn = av_read_pause(mFormatCtx);
1159             else
1160                 av_read_play(mFormatCtx);
1161         }
1162 #if CONFIG_RTSP_DEMUXER || CONFIG_MMSH_PROTOCOL
1163         if (mPaused &&
1164                 (!strcmp(mFormatCtx->iformat->name, "rtsp") ||
1165                  (mFormatCtx->pb && !strncmp(mFilename, "mmsh:", 5)))) {
1166             /* wait 10 ms to avoid trying to get another packet */
1167             /* XXX: horrible */
1168             NamDelay(10);
1169             continue;
1170         }
1171 #endif
1172
1173         if (mSeekReq) {
1174             LOGV("readerEntry, mSeekReq: %d", mSeekReq);
1175             ret = avformat_seek_file(mFormatCtx, -1, INT64_MIN, mSeekPos, INT64_MAX, mSeekFlags);
1176             if (ret < 0) {
1177                 LOGE("%s: error while seeking", mFormatCtx->filename);
1178             } else {
1179                 if (mAudioStreamIdx >= 0) {
1180                     packet_queue_flush(&mAudioQ);
1181                     packet_queue_put(&mAudioQ, &flush_pkt);
1182                 }
1183                 if (mVideoStreamIdx >= 0) {
1184                     packet_queue_flush(&mVideoQ);
1185                     packet_queue_put(&mVideoQ, &flush_pkt);
1186                 }
1187             }
1188             mSeekReq = 0;
1189             eof = 0;
1190         }
1191
1192         /* if the queue are full, no need to read more */
1193         if (   mAudioQ.size + mVideoQ.size > MAX_QUEUE_SIZE
1194             || (   (mAudioQ   .size  > MIN_AUDIOQ_SIZE || mAudioStreamIdx < 0)
1195                 && (mVideoQ   .nb_packets > MIN_FRAMES || mVideoStreamIdx < 0))) {
1196 #if DEBUG_READ_ENTRY
1197             LOGV("readerEntry, is full, fuck");
1198 #endif
1199             /* wait 10 ms */
1200             NamDelay(10);
1201             continue;
1202         }
1203
1204         if (eof) {
1205             if (mVideoStreamIdx >= 0) {
1206                 av_init_packet(pkt);
1207                 pkt->data = NULL;
1208                 pkt->size = 0;
1209                 pkt->stream_index = mVideoStreamIdx;
1210                 packet_queue_put(&mVideoQ, pkt);
1211             }
1212             if (mAudioStreamIdx >= 0) {
1213                 av_init_packet(pkt);
1214                 pkt->data = NULL;
1215                 pkt->size = 0;
1216                 pkt->stream_index = mAudioStreamIdx;
1217                 packet_queue_put(&mAudioQ, pkt);
1218             }
1219             NamDelay(10);
1220 #if DEBUG_READ_ENTRY
1221             LOGV("readerEntry, eof = 1, mVideoQ.size: %d, mVideoQ.nb_packets: %d, mAudioQ.size: %d, mAudioQ.nb_packets: %d",
1222                     mVideoQ.size, mVideoQ.nb_packets, mAudioQ.size, mAudioQ.nb_packets);
1223 #endif
1224             if (mAudioQ.size + mVideoQ.size  == 0) {
1225                 if (mLoop != 1 && (!mLoop || --mLoop)) {
1226                     if (mVideoStreamIdx >= 0) {
1227                         stream_seek(mStartTime != AV_NOPTS_VALUE ? mStartTime : 0, AVMEDIA_TYPE_VIDEO);
1228                     } else if (mAudioStreamIdx >= 0) {
1229                         stream_seek(mStartTime != AV_NOPTS_VALUE ? mStartTime : 0, AVMEDIA_TYPE_AUDIO);
1230                     }
1231                 } else if (mAutoExit) {
1232                     ret = AVERROR_EOF;
1233                     goto fail;
1234                 }
1235             }
1236             eof=0;
1237             continue;
1238         }
1239
1240         ret = av_read_frame(mFormatCtx, pkt);
1241         mProbePkts++;
1242         if (ret < 0) {
1243             if (ret == AVERROR_EOF || url_feof(mFormatCtx->pb))
1244                 if (ret == AVERROR_EOF) {
1245                     //LOGV("ret == AVERROR_EOF");
1246                 }
1247                 if (url_feof(mFormatCtx->pb)) {
1248                     //LOGV("url_feof(mFormatCtx->pb)");
1249                 }
1250
1251                 eof = 1;
1252                 mEOF = true;
1253             if (mFormatCtx->pb && mFormatCtx->pb->error) {
1254                 LOGE("mFormatCtx->pb->error: %d", mFormatCtx->pb->error);
1255                 break;
1256             }
1257             NamDelay(100);
1258             continue;
1259         }
1260
1261         if (pkt->stream_index == mVideoStreamIdx) {
1262              if (mDefersToCreateVideoTrack) {
1263                 AVCodecContext *avctx = mFormatCtx->streams[mVideoStreamIdx]->codec;
1264
1265                 int i = parser_split(avctx, pkt->data, pkt->size);
1266                 if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
1267                     if (avctx->extradata)
1268                         av_freep(&avctx->extradata);
1269                     avctx->extradata_size= i;
1270                     avctx->extradata = (uint8_t *)av_malloc(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
1271                     if (!avctx->extradata) {
1272                         //return AVERROR(ENOMEM);
1273                         ret = AVERROR(ENOMEM);
1274                         goto fail;
1275                     }
1276                     // sps + pps(there may be sei in it)
1277                     memcpy(avctx->extradata, pkt->data, avctx->extradata_size);
1278                     memset(avctx->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1279                 } else {
1280                     av_free_packet(pkt);
1281                     continue;
1282                 }
1283
1284                 stream_component_open(mVideoStreamIdx);
1285                 if (!mDefersToCreateVideoTrack)
1286                     LOGI("probe packet counter: %d when create video track ok", mProbePkts);
1287                 if (mProbePkts == EXTRACTOR_MAX_PROBE_PACKETS)
1288                     LOGI("probe packet counter to max: %d, create video track: %d",
1289                         mProbePkts, !mDefersToCreateVideoTrack);
1290             }
1291         } else if (pkt->stream_index == mAudioStreamIdx) {
1292             int ret;
1293             uint8_t *outbuf;
1294             int   outbuf_size;
1295             AVCodecContext *avctx = mFormatCtx->streams[mAudioStreamIdx]->codec;
1296             if (mAudioBsfc && pkt && pkt->data) {
1297                 ret = av_bitstream_filter_filter(mAudioBsfc, avctx, NULL, &outbuf, &outbuf_size,
1298                                    pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY);
1299
1300                 if (ret < 0 ||!outbuf_size) {
1301                     av_free_packet(pkt);
1302                     continue;
1303                 }
1304                 if (outbuf && outbuf != pkt->data) {
1305                     memmove(pkt->data, outbuf, outbuf_size);
1306                     pkt->size = outbuf_size;
1307                 }
1308             }
1309             if (mDefersToCreateAudioTrack) {
1310                 if (avctx->extradata_size <= 0) {
1311                     av_free_packet(pkt);
1312                     continue;
1313                 }
1314                 stream_component_open(mAudioStreamIdx);
1315                 if (!mDefersToCreateAudioTrack)
1316                     LOGI("probe packet counter: %d when create audio track ok", mProbePkts);
1317                 if (mProbePkts == EXTRACTOR_MAX_PROBE_PACKETS)
1318                     LOGI("probe packet counter to max: %d, create audio track: %d",
1319                         mProbePkts, !mDefersToCreateAudioTrack);
1320             }
1321         }
1322
1323         /* check if packet is in play range specified by user, then queue, otherwise discard */
1324         pkt_in_play_range = mDuration == AV_NOPTS_VALUE ||
1325                 (pkt->pts - mFormatCtx->streams[pkt->stream_index]->start_time) *
1326                 av_q2d(mFormatCtx->streams[pkt->stream_index]->time_base) -
1327                 (double)(mStartTime != AV_NOPTS_VALUE ? mStartTime : 0) / 1000000
1328                 <= ((double)mDuration / 1000000);
1329         if (pkt->stream_index == mAudioStreamIdx && pkt_in_play_range) {
1330             packet_queue_put(&mAudioQ, pkt);
1331         } else if (pkt->stream_index == mVideoStreamIdx && pkt_in_play_range) {
1332             packet_queue_put(&mVideoQ, pkt);
1333         } else {
1334             av_free_packet(pkt);
1335         }
1336     }
1337     /* wait until the end */
1338     while (!mAbortRequest) {
1339         NamDelay(100);
1340     }
1341
1342     ret = 0;
1343 fail:
1344     LOGI("reader thread goto end...");
1345
1346     /* close each stream */
1347     if (mAudioStreamIdx >= 0)
1348         stream_component_close(mAudioStreamIdx);
1349     if (mVideoStreamIdx >= 0)
1350         stream_component_close(mVideoStreamIdx);
1351     if (mFormatCtx) {
1352         avformat_close_input(&mFormatCtx);
1353     }
1354 }
1355
1356 ////////////////////////////////////////////////////////////////////////////////
1357
1358 FFmpegExtractor::Track::Track(
1359         const sp<FFmpegExtractor> &extractor, sp<MetaData> meta, bool isAVC,
1360           AVStream *stream, PacketQueue *queue)
1361     : mExtractor(extractor),
1362       mMeta(meta),
1363       mIsAVC(isAVC),
1364       mStream(stream),
1365       mQueue(queue) {
1366     const char *mime;
1367
1368     /* H.264 Video Types */
1369     {
1370         mNal2AnnexB = false;
1371
1372         if (mIsAVC) {
1373             uint32_t type;
1374             const void *data;
1375             size_t size;
1376             CHECK(meta->findData(kKeyAVCC, &type, &data, &size));
1377
1378             const uint8_t *ptr = (const uint8_t *)data;
1379
1380             CHECK(size >= 7);
1381             CHECK_EQ((unsigned)ptr[0], 1u);  // configurationVersion == 1
1382
1383             // The number of bytes used to encode the length of a NAL unit.
1384             mNALLengthSize = 1 + (ptr[4] & 3);
1385
1386             LOGV("the stream is AVC, the length of a NAL unit: %d", mNALLengthSize);
1387
1388             mNal2AnnexB = true;
1389         }
1390     }
1391
1392     mMediaType = mStream->codec->codec_type;
1393 }
1394
1395 FFmpegExtractor::Track::~Track() {
1396 }
1397
1398 status_t FFmpegExtractor::Track::start(MetaData *params) {
1399     Mutex::Autolock autoLock(mLock);
1400     //mExtractor->startReaderThread();
1401     return OK;
1402 }
1403
1404 status_t FFmpegExtractor::Track::stop() {
1405     Mutex::Autolock autoLock(mLock);
1406     mExtractor->stopReaderThread();
1407     return OK;
1408 }
1409
1410 sp<MetaData> FFmpegExtractor::Track::getFormat() {
1411     Mutex::Autolock autoLock(mLock);
1412
1413     return mMeta;
1414 }
1415
1416 status_t FFmpegExtractor::Track::read(
1417         MediaBuffer **buffer, const ReadOptions *options) {
1418     *buffer = NULL;
1419
1420     Mutex::Autolock autoLock(mLock);
1421
1422     AVPacket pkt;
1423     bool seeking = false;
1424     bool waitKeyPkt = false;
1425     ReadOptions::SeekMode mode;
1426     int64_t pktTS = AV_NOPTS_VALUE;
1427     int64_t seekTimeUs = AV_NOPTS_VALUE;
1428     int64_t timeUs;
1429     int key;
1430     status_t status = OK;
1431
1432     if (options && options->getSeekTo(&seekTimeUs, &mode)) {
1433         LOGV("~~~%s seekTimeUs: %lld, mode: %d", av_get_media_type_string(mMediaType), seekTimeUs, mode);
1434         if (mExtractor->stream_seek(seekTimeUs, mMediaType) == SEEK)
1435             seeking = true;
1436     }
1437
1438 retry:
1439     if (mExtractor->packet_queue_get(mQueue, &pkt, 1) < 0) {
1440         mExtractor->reachedEOS(mMediaType);
1441         return ERROR_END_OF_STREAM;
1442     }
1443
1444     if (seeking) {
1445         if (pkt.data != flush_pkt.data) {
1446             av_free_packet(&pkt);
1447             goto retry;
1448         } else {
1449             seeking = false;
1450 #if WAIT_KEY_PACKET_AFTER_SEEK
1451             waitKeyPkt = true;
1452 #endif
1453         }
1454     }
1455
1456     if (pkt.data == flush_pkt.data) {
1457         LOGV("read %s flush pkt", av_get_media_type_string(mMediaType));
1458         av_free_packet(&pkt);
1459         goto retry;
1460     } else if (pkt.data == NULL && pkt.size == 0) {
1461         LOGV("read %s eos pkt", av_get_media_type_string(mMediaType));
1462         av_free_packet(&pkt);
1463         mExtractor->reachedEOS(mMediaType);
1464         return ERROR_END_OF_STREAM;
1465     }
1466
1467     key = pkt.flags & AV_PKT_FLAG_KEY ? 1 : 0;
1468
1469     if (waitKeyPkt) {
1470         if (!key) {
1471             LOGV("drop the no key packet");
1472             av_free_packet(&pkt);
1473             goto retry;
1474         } else {
1475             LOGV("~~~~~~ got the key packet");
1476             waitKeyPkt = false;
1477         }
1478     }
1479      
1480     MediaBuffer *mediaBuffer = new MediaBuffer(pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
1481     mediaBuffer->meta_data()->clear();
1482     mediaBuffer->set_range(0, pkt.size);
1483 #if DISABLE_NAL_TO_ANNEXB
1484     mNal2AnnexB = false;
1485 #endif
1486     if (mIsAVC && mNal2AnnexB) {
1487         /* Convert H.264 NAL format to annex b */
1488         if (mNALLengthSize >= 3 && mNALLengthSize <= 4 )
1489         {
1490             uint8_t *dst = (uint8_t *)mediaBuffer->data();
1491
1492             /* This only works for NAL sizes 3-4 */
1493             size_t len = pkt.size, i;
1494             uint8_t *ptr = pkt.data;
1495             while (len >= mNALLengthSize) {
1496                 uint32_t nal_len = 0;
1497                 for( i = 0; i < mNALLengthSize; i++ ) {
1498                     nal_len = (nal_len << 8) | ptr[i];
1499                     dst[i] = 0;
1500                 }
1501                 dst[mNALLengthSize - 1] = 1;
1502                 if (nal_len > INT_MAX || nal_len > (unsigned int)len) {
1503                     status = ERROR_MALFORMED;
1504                     break;
1505                 }
1506                 dst += mNALLengthSize;
1507                 ptr += mNALLengthSize;
1508                 len -= mNALLengthSize;
1509
1510                 memcpy(dst, ptr, nal_len);
1511
1512                 dst += nal_len;
1513                 ptr += nal_len;
1514                 len -= nal_len;
1515             }
1516         } else {
1517              status = ERROR_MALFORMED;
1518         }
1519
1520         if (status != OK) {
1521             LOGV("status != OK");
1522             mediaBuffer->release();
1523             mediaBuffer = NULL;
1524             av_free_packet(&pkt);
1525             return ERROR_MALFORMED;
1526         }
1527     } else {
1528         memcpy(mediaBuffer->data(), pkt.data, pkt.size);
1529     }
1530
1531     pktTS = pkt.pts;
1532     // use dts when AVI
1533     if (pkt.pts == AV_NOPTS_VALUE)
1534         pktTS = pkt.dts;
1535
1536 #if 0
1537     // TODO, Stagefright can't handle negative timestamps
1538     // if needed, work around this by offsetting them manually?
1539     if (pktTS < 0)
1540         pktTS = 0;
1541 #endif
1542
1543     timeUs = (int64_t)(pktTS * av_q2d(mStream->time_base) * 1000000);
1544
1545 #if 0
1546     LOGV("read %s pkt, size: %d, key: %d, pts: %lld, dts: %lld, timeUs: %llu us (%.2f secs)",
1547         av_get_media_type_string(mMediaType), pkt.size, key, pkt.pts, pkt.dts, timeUs, timeUs/1E6);
1548 #endif
1549
1550 #if 0
1551     // TODO, Stagefright can't handle negative timestamps
1552     // if needed, work around this by offsetting them manually?
1553     if (timeUs < 0)
1554         timeUs = 0;
1555 #endif
1556
1557     mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs);
1558     mediaBuffer->meta_data()->setInt32(kKeyIsSyncFrame, key);
1559
1560     *buffer = mediaBuffer;
1561
1562     av_free_packet(&pkt);
1563
1564     return OK;
1565 }
1566
1567 ////////////////////////////////////////////////////////////////////////////////
1568
1569 // LegacySniffFFMPEG
1570 typedef struct {
1571     const char *extension;
1572     const char *container;
1573 } extmap;
1574
1575 static extmap FILE_EXTS[] = {
1576         {".mp4", MEDIA_MIMETYPE_CONTAINER_MPEG4},
1577         {".3gp", MEDIA_MIMETYPE_CONTAINER_MPEG4},
1578         {".mp3", MEDIA_MIMETYPE_AUDIO_MPEG},
1579         {".mov", MEDIA_MIMETYPE_CONTAINER_MOV},
1580         {".mkv", MEDIA_MIMETYPE_CONTAINER_MATROSKA},
1581         {".ts",  MEDIA_MIMETYPE_CONTAINER_TS},
1582         {".avi", MEDIA_MIMETYPE_CONTAINER_AVI},
1583         {".asf", MEDIA_MIMETYPE_CONTAINER_ASF},
1584 #if 0
1585         {".wmv", MEDIA_MIMETYPE_CONTAINER_WMV},
1586         {".wma", MEDIA_MIMETYPE_CONTAINER_WMA},
1587         {".mpg", MEDIA_MIMETYPE_CONTAINER_MPG},
1588         {".flv", MEDIA_MIMETYPE_CONTAINER_FLV},
1589         {".divx", MEDIA_MIMETYPE_CONTAINER_DIVX},
1590         {".mp2", MEDIA_MIMETYPE_CONTAINER_MP2},
1591         {".ape", MEDIA_MIMETYPE_CONTAINER_APE},
1592         {".rm ", MEDIA_MIMETYPE_CONTAINER_RM},
1593         {".ra",  MEDIA_MIMETYPE_CONTAINER_RA},
1594 #endif
1595 };
1596
1597 const char *LegacySniffFFMPEG(const char * uri)
1598 {
1599     size_t i;
1600     const char *container = NULL;
1601
1602     LOGI("list the file extensions suppoted by ffmpeg: ");
1603     LOGI("========================================");
1604     for (i = 0; i < NELEM(FILE_EXTS); ++i) {
1605             LOGV("file_exts[%02d]: %s", i, FILE_EXTS[i].extension);
1606     }
1607     LOGI("========================================");
1608
1609     int lenURI = strlen(uri);
1610     for (i = 0; i < NELEM(FILE_EXTS); ++i) {
1611         int len = strlen(FILE_EXTS[i].extension);
1612         int start = lenURI - len;
1613         if (start > 0) {
1614             if (!av_strncasecmp(uri + start, FILE_EXTS[i].extension, len)) {
1615                 container = FILE_EXTS[i].container;
1616                 break;
1617             }
1618         }
1619     }
1620
1621     return container;
1622 }
1623
1624 // BetterSniffFFMPEG
1625 typedef struct {
1626     const char *format;
1627     const char *container;
1628 } formatmap;
1629
1630 static formatmap FILE_FORMATS[] = {
1631         {"mpegts",                  MEDIA_MIMETYPE_CONTAINER_TS},
1632         {"mov,mp4,m4a,3gp,3g2,mj2", MEDIA_MIMETYPE_CONTAINER_MOV},
1633         {"asf",                     MEDIA_MIMETYPE_CONTAINER_ASF},
1634 };
1635
1636 const char *BetterSniffFFMPEG(const char * uri)
1637 {
1638     size_t i;
1639     const char *container = NULL;
1640     AVFormatContext *ic = NULL;
1641
1642     status_t status = initFFmpeg();
1643     if (status != OK) {
1644         LOGE("could not init ffmpeg");
1645         return false;
1646     }
1647
1648     ic = avformat_alloc_context();
1649     avformat_open_input(&ic, uri, NULL, NULL);
1650
1651     av_dump_format(ic, 0, uri, 0);
1652
1653     LOGI("FFmpegExtrator, uri: %s, format_name: %s, format_long_name: %s", uri, ic->iformat->name, ic->iformat->long_name);
1654
1655     LOGI("list the format suppoted by ffmpeg: ");
1656     LOGI("========================================");
1657     for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
1658             LOGV("format_names[%02d]: %s", i, FILE_FORMATS[i].format);
1659     }
1660     LOGI("========================================");
1661
1662     for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
1663         int len = strlen(FILE_FORMATS[i].format);
1664         if (!av_strncasecmp(ic->iformat->name, FILE_FORMATS[i].format, len)) {
1665             container = FILE_FORMATS[i].container;
1666             break;
1667         }
1668     }
1669
1670     avformat_close_input(&ic);
1671     av_free(ic);
1672
1673     return container;
1674 }
1675
1676 bool SniffFFMPEG(
1677         const sp<DataSource> &source, String8 *mimeType, float *confidence,
1678         sp<AMessage> *meta) {
1679     LOGV("SniffFFMPEG");
1680     const char *uri, *container = NULL;
1681
1682     uri = source->getNamURI();
1683
1684     if (!uri)
1685         return false;
1686
1687     LOGI("ffmpeg uri: %s", uri);
1688
1689     container = BetterSniffFFMPEG(uri);
1690     if (!container) {
1691         LOGW("sniff through LegacySniffFFMPEG, only check the file extension");
1692         container = LegacySniffFFMPEG(uri);
1693     }
1694
1695     if (container == NULL)
1696         return false;
1697
1698     LOGV("found container: %s", container);
1699
1700     *confidence = 0.88f;  // Slightly larger than other extractor's confidence
1701     mimeType->setTo(container);
1702
1703     /* use MPEG4Extractor(not extended extractor) for HTTP source only */
1704     if (!av_strcasecmp(container, MEDIA_MIMETYPE_CONTAINER_MPEG4)
1705             && (source->flags() & DataSource::kIsCachingDataSource)) {
1706             return true;
1707     }
1708
1709     *meta = new AMessage;
1710     (*meta)->setString("extended-extractor", "extended-extractor");
1711     (*meta)->setString("extended-extractor-subtype", "ffmpegextractor");
1712
1713     return true;
1714 }
1715
1716 }  // namespace android