#define LOG_TAG "FFmpegExtractor"
#include <utils/Log.h>
+#include <stdint.h>
#include <limits.h> /* INT_MAX */
+#include <inttypes.h>
#include <media/stagefright/foundation/ABitReader.h>
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/hexdump.h>
#include <media/stagefright/DataSource.h>
#include <media/stagefright/MediaBuffer.h>
-#include <media/stagefright/MediaDebug.h>
+#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MediaSource.h>
#include <utils/misc.h>
#include "include/avc_utils.h"
-#include "ffmpeg_utils/ffmpeg_utils.h"
+#include "utils/common_utils.h"
+#include "utils/ffmpeg_utils.h"
+#include "utils/ffmpeg_cmdutils.h"
#include "FFmpegExtractor.h"
-#define DEBUG_READ_ENTRY 0
-#define DIABLE_VIDEO 0
-#define DIABLE_AUDIO 0
+#define DEBUG_READ_ENTRY 0
+#define DEBUG_DISABLE_VIDEO 0
+#define DEBUG_DISABLE_AUDIO 0
#define WAIT_KEY_PACKET_AFTER_SEEK 1
-#define DISABLE_NAL_TO_ANNEXB 0
+#define DISABLE_NAL_TO_ANNEXB 0
#define MAX_QUEUE_SIZE (15 * 1024 * 1024)
#define MIN_AUDIOQ_SIZE (20 * 16 * 1024)
AVStream *mStream;
PacketQueue *mQueue;
+ int64_t mFirstKeyPktTimestamp;
+
DISALLOW_EVIL_CONSTRUCTORS(Track);
};
FFmpegExtractor::FFmpegExtractor(const sp<DataSource> &source)
: mDataSource(source),
- mReaderThreadStarted(false),
- mInitCheck(NO_INIT) {
- LOGV("FFmpegExtractor::FFmpegExtractor");
+ mInitCheck(NO_INIT),
+ mReaderThreadStarted(false) {
+ ALOGV("FFmpegExtractor::FFmpegExtractor");
- int err;
- const char *url = mDataSource->getNamURI();
- if (url == NULL) {
- LOGI("url is error!");
- return;
- }
- // is it right?
- if (!strcmp(url, "-")) {
- av_strlcpy(mFilename, "pipe:", strlen("pipe:") + 1);
- } else {
- av_strlcpy(mFilename, url, strlen(url) + 1);
- }
- LOGI("url: %s, mFilename: %s", url, mFilename);
+ buildFileName(source);
- err = initFFmpeg();
+ int err = initStreams();
if (err < 0) {
- LOGE("failed to init ffmpeg");
+ ALOGE("failed to init ffmpeg");
return;
}
(mFormatCtx->pb ? !mFormatCtx->pb->error : 1) &&
(mDefersToCreateVideoTrack || mDefersToCreateAudioTrack)) {
// FIXME, i am so lazy! Should use pthread_cond_wait to wait conditions
- SDL_Delay(5);
+ usleep(5000);
}
- LOGV("mProbePkts: %d, mEOF: %d, pb->error(if has): %d, mDefersToCreateVideoTrack: %d, mDefersToCreateAudioTrack: %d",
+ ALOGV("mProbePkts: %d, mEOF: %d, pb->error(if has): %d, mDefersToCreateVideoTrack: %d, mDefersToCreateAudioTrack: %d",
mProbePkts, mEOF, mFormatCtx->pb ? mFormatCtx->pb->error : 0, mDefersToCreateVideoTrack, mDefersToCreateAudioTrack);
mInitCheck = OK;
}
FFmpegExtractor::~FFmpegExtractor() {
- LOGV("FFmpegExtractor::~FFmpegExtractor");
+ ALOGV("FFmpegExtractor::~FFmpegExtractor");
// stop reader here if no track!
stopReaderThread();
- deInitFFmpeg();
+ deInitStreams();
}
size_t FFmpegExtractor::countTracks() {
}
sp<MediaSource> FFmpegExtractor::getTrack(size_t index) {
- LOGV("FFmpegExtractor::getTrack[%d]", index);
+ ALOGV("FFmpegExtractor::getTrack[%d]", index);
if (mInitCheck != OK) {
return NULL;
}
sp<MetaData> FFmpegExtractor::getTrackMetaData(size_t index, uint32_t flags) {
- LOGV("FFmpegExtractor::getTrackMetaData[%d]", index);
+ ALOGV("FFmpegExtractor::getTrackMetaData[%d]", index);
if (mInitCheck != OK) {
return NULL;
}
sp<MetaData> FFmpegExtractor::getMetaData() {
- LOGV("FFmpegExtractor::getMetaData");
+ ALOGV("FFmpegExtractor::getMetaData");
if (mInitCheck != OK) {
return NULL;
}
uint32_t FFmpegExtractor::flags() const {
- LOGV("FFmpegExtractor::flags");
+ ALOGV("FFmpegExtractor::flags");
if (mInitCheck != OK) {
- return NULL;
+ return 0;
}
uint32_t flags = CAN_PAUSE;
void FFmpegExtractor::packet_queue_init(PacketQueue *q)
{
memset(q, 0, sizeof(PacketQueue));
- q->mutex = SDL_CreateMutex();
- q->cond = SDL_CreateCond();
+ pthread_mutex_init(&q->mutex, NULL);
+ pthread_cond_init(&q->cond, NULL);
packet_queue_put(q, &flush_pkt);
}
{
AVPacketList *pkt, *pkt1;
- SDL_LockMutex(q->mutex);
+ pthread_mutex_lock(&q->mutex);
for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
pkt1 = pkt->next;
av_free_packet(&pkt->pkt);
q->first_pkt = NULL;
q->nb_packets = 0;
q->size = 0;
- SDL_UnlockMutex(q->mutex);
+ pthread_mutex_unlock(&q->mutex);
}
void FFmpegExtractor::packet_queue_end(PacketQueue *q)
{
packet_queue_flush(q);
- SDL_DestroyMutex(q->mutex);
- SDL_DestroyCond(q->cond);
}
void FFmpegExtractor::packet_queue_abort(PacketQueue *q)
{
- SDL_LockMutex(q->mutex);
+ pthread_mutex_lock(&q->mutex);
q->abort_request = 1;
- SDL_CondSignal(q->cond);
+ pthread_cond_signal(&q->cond);
- SDL_UnlockMutex(q->mutex);
+ pthread_mutex_unlock(&q->mutex);
}
int FFmpegExtractor::packet_queue_put(PacketQueue *q, AVPacket *pkt)
pkt1->pkt = *pkt;
pkt1->next = NULL;
- SDL_LockMutex(q->mutex);
+ pthread_mutex_lock(&q->mutex);
if (!q->last_pkt)
q->nb_packets++;
//q->size += pkt1->pkt.size + sizeof(*pkt1);
q->size += pkt1->pkt.size;
- SDL_CondSignal(q->cond);
+ pthread_cond_signal(&q->cond);
- SDL_UnlockMutex(q->mutex);
+ pthread_mutex_unlock(&q->mutex);
return 0;
}
AVPacketList *pkt1;
int ret;
- SDL_LockMutex(q->mutex);
+ pthread_mutex_lock(&q->mutex);
for (;;) {
if (q->abort_request) {
ret = 0;
break;
} else {
- SDL_CondWait(q->cond, q->mutex);
+ pthread_cond_wait(&q->cond, &q->mutex);
}
}
- SDL_UnlockMutex(q->mutex);
+ pthread_mutex_unlock(&q->mutex);
return ret;
}
-static int lockmgr(void **mtx, enum AVLockOp op)
-{
- switch(op) {
- case AV_LOCK_CREATE:
- *mtx = (void *)SDL_CreateMutex();
- if(!*mtx)
- return 1;
- return 0;
- case AV_LOCK_OBTAIN:
- return !!SDL_LockMutex((SDL_mutex *)*mtx);
- case AV_LOCK_RELEASE:
- return !!SDL_UnlockMutex((SDL_mutex *)*mtx);
- case AV_LOCK_DESTROY:
- SDL_DestroyMutex((SDL_mutex *)*mtx);
- return 0;
- }
- return 1;
-}
-
static void EncodeSize14(uint8_t **_ptr, size_t size) {
CHECK_LE(size, 0x3fff);
int FFmpegExtractor::check_extradata(AVCodecContext *avctx)
{
- const char *name;
- bool *defersToCreateTrack;
- AVBitStreamFilterContext **bsfc;
+ const char *name = NULL;
+ bool *defersToCreateTrack = NULL;
+ AVBitStreamFilterContext **bsfc = NULL;
// init
if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
avctx->codec_id == CODEC_ID_MP1 ||
avctx->codec_id == CODEC_ID_MP2 ||
avctx->codec_id == CODEC_ID_AC3 ||
+ avctx->codec_id == CODEC_ID_DTS ||
avctx->codec_id == CODEC_ID_H263 ||
avctx->codec_id == CODEC_ID_H263P ||
- avctx->codec_id == CODEC_ID_H263I)
+ avctx->codec_id == CODEC_ID_H263I ||
+ avctx->codec_id == CODEC_ID_WMV1)
return 1;
// is extradata compatible with android?
if (avctx->codec_id != CODEC_ID_AAC) {
int is_compatible = is_extradata_compatible_with_android(avctx);
if (!is_compatible) {
- LOGI("%s extradata is not compatible with android, should to extract it from bitstream",
+ ALOGI("%s extradata is not compatible with android, should to extract it from bitstream",
av_get_media_type_string(avctx->codec_type));
*defersToCreateTrack = true;
*bsfc = NULL; // H264 don't need bsfc, only AAC?
}
if (avctx->extradata_size <= 0) {
- LOGI("No %s extradata found, should to extract it from bitstream",
+ ALOGI("No %s extradata found, should to extract it from bitstream",
av_get_media_type_string(avctx->codec_type));
*defersToCreateTrack = true;
//CHECK(name != NULL);
if (!*bsfc && name) {
*bsfc = av_bitstream_filter_init(name);
if (!*bsfc) {
- LOGE("Cannot open the %s BSF!", name);
+ ALOGE("Cannot open the %s BSF!", name);
*defersToCreateTrack = false;
return -1;
} else {
- LOGV("open the %s bsf", name);
+ ALOGV("open the %s bsf", name);
return 0;
}
} else {
return 1;
}
+void FFmpegExtractor::printTime(int64_t time)
+{
+ int hours, mins, secs, us;
+
+ if (time == AV_NOPTS_VALUE)
+ return;
+
+ secs = time / AV_TIME_BASE;
+ us = time % AV_TIME_BASE;
+ mins = secs / 60;
+ secs %= 60;
+ hours = mins / 60;
+ mins %= 60;
+ ALOGI("the time is %02d:%02d:%02d.%02d",
+ hours, mins, secs, (100 * us) / AV_TIME_BASE);
+}
int FFmpegExtractor::stream_component_open(int stream_index)
{
- AVCodecContext *avctx;
- sp<MetaData> meta;
+ AVCodecContext *avctx = NULL;
+ sp<MetaData> meta = NULL;
bool isAVC = false;
bool supported = false;
- uint32_t type;
- const void *data;
- size_t size;
- int ret;
+ uint32_t type = 0;
+ const void *data = NULL;
+ size_t size = 0;
+ int ret = 0;
- LOGI("stream_index: %d", stream_index);
- if (stream_index < 0 || stream_index >= mFormatCtx->nb_streams)
+ ALOGI("stream_index: %d", stream_index);
+ if (stream_index < 0 || stream_index >= (int)mFormatCtx->nb_streams)
return -1;
avctx = mFormatCtx->streams[stream_index]->codec;
case CODEC_ID_MP2:
case CODEC_ID_MP3:
case CODEC_ID_MPEG2VIDEO:
-#if 0
+ case CODEC_ID_WMV1:
+ case CODEC_ID_WMV2:
+ case CODEC_ID_WMV3:
case CODEC_ID_VC1:
-#endif
+ case CODEC_ID_WMAV1:
+ case CODEC_ID_WMAV2:
+ case CODEC_ID_WMAPRO:
+ case CODEC_ID_WMALOSSLESS:
+ case CODEC_ID_RV40:
+ case CODEC_ID_COOK:
+ case CODEC_ID_APE:
+ case CODEC_ID_DTS:
+ case CODEC_ID_FLAC:
supported = true;
break;
default:
}
if (!supported) {
- LOGE("unsupport the codec, id: 0x%0x", avctx->codec_id);
+ ALOGE("unsupport the codec, id: 0x%0x", avctx->codec_id);
return -1;
}
- LOGV("support the codec");
+ ALOGV("support the codec");
unsigned streamType;
ssize_t index = mTracks.indexOfKey(stream_index);
if (index >= 0) {
- LOGE("this track already exists");
+ ALOGE("this track already exists");
return 0;
}
char tagbuf[32];
av_get_codec_tag_string(tagbuf, sizeof(tagbuf), avctx->codec_tag);
- LOGV("Tag %s/0x%08x with codec id '%d'\n", tagbuf, avctx->codec_tag, avctx->codec_id);
+ ALOGV("Tag %s/0x%08x with codec id '%d'\n", tagbuf, avctx->codec_tag, avctx->codec_id);
switch (avctx->codec_type) {
case AVMEDIA_TYPE_VIDEO:
}
if (avctx->extradata) {
- LOGV("video stream extradata:");
+ ALOGV("video stream extradata:");
hexdump(avctx->extradata, avctx->extradata_size);
} else {
- LOGV("video stream no extradata, but we can ignore it.");
+ ALOGV("video stream no extradata, but we can ignore it.");
}
meta = new MetaData;
if (avctx->extradata[0] == 1 /* configurationVersion */) {
// H.264 bitstream without start codes.
isAVC = true;
- LOGV("AVC");
+ ALOGV("AVC");
if (avctx->width == 0 || avctx->height == 0) {
int32_t width, height;
} else {
// H.264 bitstream with start codes.
isAVC = false;
- LOGV("H264");
+ ALOGV("H264");
/* set NULL to release meta as we will new a meta in MakeAVCCodecSpecificData() fxn */
meta->clear();
}
break;
case CODEC_ID_MPEG4:
- LOGV("MPEG4");
+ ALOGV("MPEG4");
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
{
sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
case CODEC_ID_H263:
case CODEC_ID_H263P:
case CODEC_ID_H263I:
- LOGV("H263");
+ ALOGV("H263");
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
break;
case CODEC_ID_MPEG2VIDEO:
- LOGV("MPEG2VIDEO");
+ ALOGV("MPEG2VIDEO");
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
{
sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
}
break;
case CODEC_ID_VC1:
- LOGV("VC1");
- meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV12);
- meta->setData(kKeyESDS, kTypeESDS, avctx->extradata, avctx->extradata_size);
+ ALOGV("VC1");
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_VC1);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ break;
+ case CODEC_ID_WMV1:
+ ALOGV("WMV1");
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
+ meta->setInt32(kKeyWMVVersion, kTypeWMVVer_7);
+ break;
+ case CODEC_ID_WMV2:
+ ALOGV("WMV2");
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ meta->setInt32(kKeyWMVVersion, kTypeWMVVer_8);
+ break;
+ case CODEC_ID_WMV3:
+ ALOGV("WMV3");
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ meta->setInt32(kKeyWMVVersion, kTypeWMVVer_9);
+ break;
+ case CODEC_ID_RV40:
+ ALOGV("RV40");
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
break;
default:
CHECK(!"Should not be here. Unsupported codec.");
break;
}
- LOGI("width: %d, height: %d, bit_rate: %d", avctx->width, avctx->height, avctx->bit_rate);
+ ALOGI("width: %d, height: %d, bit_rate: %d",
+ avctx->width, avctx->height, avctx->bit_rate);
meta->setInt32(kKeyWidth, avctx->width);
meta->setInt32(kKeyHeight, avctx->height);
if (avctx->bit_rate > 0)
meta->setInt32(kKeyBitRate, avctx->bit_rate);
- if (mFormatCtx->duration != AV_NOPTS_VALUE)
+ if (mVideoStream->duration != AV_NOPTS_VALUE) {
+ int64_t duration = mVideoStream->duration * av_q2d(mVideoStream->time_base) * 1000000;
+ printTime(duration);
+ ALOGV("video startTime: %lld", mVideoStream->start_time);
+ meta->setInt64(kKeyDuration, duration);
+ } else {
+ // default when no stream duration
meta->setInt64(kKeyDuration, mFormatCtx->duration);
+ }
- LOGV("create a video track");
+ ALOGV("create a video track");
index = mTracks.add(
stream_index, new Track(this, meta, isAVC, mVideoStream, &mVideoQ));
}
if (avctx->extradata) {
- LOGV("audio stream extradata:");
+ ALOGV("audio stream extradata:");
hexdump(avctx->extradata, avctx->extradata_size);
} else {
- LOGV("audio stream no extradata, but we can ignore it.");
+ ALOGV("audio stream no extradata, but we can ignore it.");
}
switch(avctx->codec_id) {
case CODEC_ID_MP1:
- LOGV("MP1");
+ ALOGV("MP1");
meta = new MetaData;
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_I);
break;
case CODEC_ID_MP2:
- LOGV("MP2");
+ ALOGV("MP2");
meta = new MetaData;
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
break;
case CODEC_ID_MP3:
- LOGV("MP3");
+ ALOGV("MP3");
meta = new MetaData;
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
break;
case CODEC_ID_AC3:
- LOGV("AC3");
+ ALOGV("AC3");
meta = new MetaData;
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3);
break;
case CODEC_ID_AAC:
- LOGV("AAC");
+ ALOGV("AAC");
uint32_t sr;
const uint8_t *header;
uint8_t profile, sf_index, channel;
sf_index = (header[0] & 0x07) << 1 | (header[1] & 0x80) >> 7;
sr = get_sample_rate(sf_index);
if (sr == 0) {
- LOGE("unsupport the sample rate");
+ ALOGE("unsupport the sample rate");
return -1;
}
channel = (header[1] >> 3) & 0xf;
- LOGV("profile: %d, sf_index: %d, channel: %d", profile, sf_index, channel);
+ ALOGV("profile: %d, sf_index: %d, channel: %d", profile, sf_index, channel);
meta = MakeAACCodecSpecificData(profile, sf_index, channel);
meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
break;
+ case CODEC_ID_WMAV1: // TODO, version?
+ ALOGV("WMAV1");
+ meta = new MetaData;
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ break;
+ case CODEC_ID_WMAV2:
+ ALOGV("WMAV2");
+ meta = new MetaData;
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ meta->setInt32(kKeyWMAVersion, kTypeWMA);
+ break;
+ case CODEC_ID_WMAPRO:
+ ALOGV("WMAPRO");
+ meta = new MetaData;
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ meta->setInt32(kKeyWMAVersion, kTypeWMAPro);
+ break;
+ case CODEC_ID_WMALOSSLESS:
+ ALOGV("WMALOSSLESS");
+ meta = new MetaData;
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ meta->setInt32(kKeyWMAVersion, kTypeWMALossLess);
+ break;
+ case CODEC_ID_COOK: // audio codec in RMVB
+ ALOGV("COOK");
+ meta = new MetaData;
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RA);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ break;
+ case CODEC_ID_APE:
+ ALOGV("APE");
+ meta = new MetaData;
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_APE);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ break;
+ case CODEC_ID_DTS:
+ ALOGV("DTS");
+ meta = new MetaData;
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_DTS);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ break;
+ case CODEC_ID_FLAC:
+ ALOGV("FLAC");
+ meta = new MetaData;
+ meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_FLAC);
+ meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
+ break;
default:
CHECK(!"Should not be here. Unsupported codec.");
break;
}
- LOGI("bit_rate: %d, sample_rate: %d, channels: %d", avctx->bit_rate, avctx->sample_rate, avctx->channels);
+ ALOGI("bit_rate: %d, sample_rate: %d, channels: %d, bits_per_coded_sample: %d",
+ avctx->bit_rate, avctx->sample_rate, avctx->channels, avctx->bits_per_coded_sample);
meta->setInt32(kKeySampleRate, avctx->sample_rate);
meta->setInt32(kKeyChannelCount, avctx->channels);
+ meta->setInt32(kKeyBitspersample, avctx->bits_per_coded_sample);
meta->setInt32(kKeyBitRate, avctx->bit_rate);
- if (mFormatCtx->duration != AV_NOPTS_VALUE)
+ meta->setInt32(kKeyBlockAlign, avctx->block_align);
+ if (mAudioStream->duration != AV_NOPTS_VALUE) {
+ int64_t duration = mAudioStream->duration * av_q2d(mAudioStream->time_base) * 1000000;
+ printTime(duration);
+ ALOGV("audio startTime: %lld", mAudioStream->start_time);
+ meta->setInt64(kKeyDuration, duration);
+ } else {
+ // default when no stream duration
meta->setInt64(kKeyDuration, mFormatCtx->duration);
-
- if (avctx->codec_id != CODEC_ID_MP3 &&
- avctx->codec_id != CODEC_ID_MP1 &&
- avctx->codec_id != CODEC_ID_MP2 &&
- avctx->codec_id != CODEC_ID_AC3) {
- LOGV("audio meta esds:");
- CHECK(meta->findData(kKeyESDS, &type, &data, &size));
- hexdump(data, size);
}
- LOGV("create a audio track");
+ ALOGV("create a audio track");
index = mTracks.add(
stream_index, new Track(this, meta, false, mAudioStream, &mAudioQ));
{
AVCodecContext *avctx;
- if (stream_index < 0 || stream_index >= mFormatCtx->nb_streams)
+ if (stream_index < 0 || stream_index >= (int)mFormatCtx->nb_streams)
return;
avctx = mFormatCtx->streams[stream_index]->codec;
switch (avctx->codec_type) {
case AVMEDIA_TYPE_VIDEO:
- LOGV("packet_queue_abort videoq");
+ ALOGV("packet_queue_abort videoq");
packet_queue_abort(&mVideoQ);
/* wait until the end */
while (!mAbortRequest && !mVideoEOSReceived) {
- LOGV("wait for video received");
- SDL_Delay(10);
+ ALOGV("wait for video received");
+ usleep(10000);
}
- LOGV("packet_queue_end videoq");
+ ALOGV("packet_queue_end videoq");
packet_queue_end(&mVideoQ);
break;
case AVMEDIA_TYPE_AUDIO:
- LOGV("packet_queue_abort audioq");
+ ALOGV("packet_queue_abort audioq");
packet_queue_abort(&mAudioQ);
while (!mAbortRequest && !mAudioEOSReceived) {
- LOGV("wait for audio received");
- SDL_Delay(10);
+ ALOGV("wait for audio received");
+ usleep(10000);
}
- LOGV("packet_queue_end audioq");
+ ALOGV("packet_queue_end audioq");
packet_queue_end(&mAudioQ);
break;
case AVMEDIA_TYPE_SUBTITLE:
if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
errbuf_ptr = strerror(AVUNERROR(err));
- LOGI("%s: %s\n", filename, errbuf_ptr);
+ ALOGI("%s: %s\n", filename, errbuf_ptr);
+}
+
+void FFmpegExtractor::buildFileName(const sp<DataSource> &source)
+{
+#if 1
+ ALOGI("android-source:%p", &source);
+ // pass the addr of smart pointer("source")
+ snprintf(mFilename, sizeof(mFilename), "android-source:%p", &source);
+ ALOGI("build mFilename: %s", mFilename);
+#else
+ const char *url = mDataSource->getNamURI();
+ if (url == NULL) {
+ ALOGI("url is error!");
+ return;
+ }
+ // is it right?
+ if (!strcmp(url, "-")) {
+ av_strlcpy(mFilename, "pipe:", strlen("pipe:") + 1);
+ } else {
+ av_strlcpy(mFilename, url, strlen(url) + 1);
+ }
+ ALOGI("build url: %s, mFilename: %s", url, mFilename);
+#endif
}
void FFmpegExtractor::setFFmpegDefaultOpts()
{
mGenPTS = 0;
-#if DIABLE_VIDEO
+#if DEBUG_DISABLE_VIDEO
mVideoDisable = 1;
#else
mVideoDisable = 0;
#endif
-#if DIABLE_AUDIO
+#if DEBUG_DISABLE_AUDIO
mAudioDisable = 1;
#else
mAudioDisable = 0;
#endif
- mShowStatus = 1;
+ mShowStatus = 0;
mSeekByBytes = 0; /* seek by bytes 0=off 1=on -1=auto" */
- mStartTime = AV_NOPTS_VALUE;
mDuration = AV_NOPTS_VALUE;
mSeekPos = AV_NOPTS_VALUE;
mAutoExit = 1;
mEOF = false;
}
-int FFmpegExtractor::initFFmpeg()
+int FFmpegExtractor::initStreams()
{
- int err, i;
+ int err = 0;
+ int i = 0;
+ status_t status = UNKNOWN_ERROR;
int eof = 0;
int ret = 0, audio_ret = 0, video_ret = 0;
int pkt_in_play_range = 0;
- AVDictionaryEntry *t;
- AVDictionary **opts;
- int orig_nb_streams;
+ AVDictionaryEntry *t = NULL;
+ AVDictionary **opts = NULL;
+ int orig_nb_streams = 0;
int st_index[AVMEDIA_TYPE_NB] = {0};
int wanted_stream[AVMEDIA_TYPE_NB] = {0};
st_index[AVMEDIA_TYPE_AUDIO] = -1;
wanted_stream[AVMEDIA_TYPE_VIDEO] = -1;
setFFmpegDefaultOpts();
-
- //nam_av_log_set_flags(AV_LOG_SKIP_REPEATED);
- //av_log_set_level(AV_LOG_DEBUG);
- av_log_set_callback(nam_av_log_callback);
-
- /* register all codecs, demux and protocols */
- avcodec_register_all();
-#if CONFIG_AVDEVICE
- avdevice_register_all();
-#endif
- av_register_all();
- avformat_network_init();
- init_opts();
-
- if (av_lockmgr_register(lockmgr)) {
- LOGE("could not initialize lock manager!");
+ status = initFFmpeg();
+ if (status != OK) {
+ ret = -1;
+ goto fail;
}
av_init_packet(&flush_pkt);
mFormatCtx = avformat_alloc_context();
mFormatCtx->interrupt_callback.callback = decode_interrupt_cb;
mFormatCtx->interrupt_callback.opaque = this;
- LOGV("mFilename: %s", mFilename);
+ ALOGV("mFilename: %s", mFilename);
err = avformat_open_input(&mFormatCtx, mFilename, NULL, &format_opts);
if (err < 0) {
print_error_ex(mFilename, err);
goto fail;
}
if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
- LOGE("Option %s not found.\n", t->key);
+ ALOGE("Option %s not found.\n", t->key);
//ret = AVERROR_OPTION_NOT_FOUND;
ret = -1;
goto fail;
err = avformat_find_stream_info(mFormatCtx, opts);
if (err < 0) {
- LOGE("%s: could not find codec parameters\n", mFilename);
+ ALOGE("%s: could not find codec parameters\n", mFilename);
ret = -1;
goto fail;
}
if (mSeekByBytes < 0)
mSeekByBytes = !!(mFormatCtx->iformat->flags & AVFMT_TS_DISCONT);
- /* if seeking requested, we execute it */
- if (mStartTime != AV_NOPTS_VALUE) {
- int64_t timestamp;
-
- timestamp = mStartTime;
- /* add the stream start time */
- if (mFormatCtx->start_time != AV_NOPTS_VALUE)
- timestamp += mFormatCtx->start_time;
- ret = avformat_seek_file(mFormatCtx, -1, INT64_MIN, timestamp, INT64_MAX, 0);
- if (ret < 0) {
- LOGE("%s: could not seek to position %0.3f",
- mFilename, (double)timestamp / AV_TIME_BASE);
- goto fail;
- }
- }
-
- for (i = 0; i < mFormatCtx->nb_streams; i++)
+ for (i = 0; i < (int)mFormatCtx->nb_streams; i++)
mFormatCtx->streams[i]->discard = AVDISCARD_ALL;
if (!mVideoDisable)
st_index[AVMEDIA_TYPE_VIDEO] =
av_dump_format(mFormatCtx, 0, mFilename, 0);
}
- if (mFormatCtx->duration != AV_NOPTS_VALUE) {
+ if (mFormatCtx->duration != AV_NOPTS_VALUE &&
+ mFormatCtx->start_time != AV_NOPTS_VALUE) {
int hours, mins, secs, us;
- secs = mFormatCtx->duration / AV_TIME_BASE;
- us = mFormatCtx->duration % AV_TIME_BASE;
+
+ ALOGV("file startTime: %lld", mFormatCtx->start_time);
+
+ mDuration = mFormatCtx->duration;
+
+ secs = mDuration / AV_TIME_BASE;
+ us = mDuration % AV_TIME_BASE;
mins = secs / 60;
secs %= 60;
hours = mins / 60;
mins %= 60;
- LOGI("the duration is %02d:%02d:%02d.%02d", hours, mins, secs, (100 * us) / AV_TIME_BASE);
+ ALOGI("the duration is %02d:%02d:%02d.%02d",
+ hours, mins, secs, (100 * us) / AV_TIME_BASE);
}
if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
}
if ( audio_ret < 0 && video_ret < 0) {
- LOGE("%s: could not open codecs\n", mFilename);
+ ALOGE("%s: could not open codecs\n", mFilename);
ret = -1;
goto fail;
}
return ret;
}
-void FFmpegExtractor::deInitFFmpeg()
+void FFmpegExtractor::deInitStreams()
{
if (mFormatCtx) {
avformat_close_input(&mFormatCtx);
}
-
- av_lockmgr_register(NULL);
- uninit_opts();
- avformat_network_deinit();
- av_log(NULL, AV_LOG_QUIET, "%s", "");
+ deInitFFmpeg();
}
status_t FFmpegExtractor::startReaderThread() {
- LOGV("Starting reader thread");
+ ALOGV("Starting reader thread");
Mutex::Autolock autoLock(mLock);
if (mReaderThreadStarted)
pthread_create(&mReaderThread, &attr, ReaderWrapper, this);
pthread_attr_destroy(&attr);
mReaderThreadStarted = true;
- LOGD("Reader thread started");
+ ALOGD("Reader thread started");
return OK;
}
void FFmpegExtractor::stopReaderThread() {
- LOGV("Stopping reader thread");
+ ALOGV("Stopping reader thread");
Mutex::Autolock autoLock(mLock);
if (!mReaderThreadStarted) {
- LOGD("Reader thread have been stopped");
+ ALOGD("Reader thread have been stopped");
return;
}
void *dummy;
pthread_join(mReaderThread, &dummy);
mReaderThreadStarted = false;
- LOGD("Reader thread stopped");
+ ALOGD("Reader thread stopped");
}
// static
int eof = 0;
int pkt_in_play_range = 0;
- LOGV("FFmpegExtractor::readerEntry");
+ ALOGV("FFmpegExtractor::readerEntry");
mVideoEOSReceived = false;
mAudioEOSReceived = false;
(mFormatCtx->pb && !strncmp(mFilename, "mmsh:", 5)))) {
/* wait 10 ms to avoid trying to get another packet */
/* XXX: horrible */
- SDL_Delay(10);
+ usleep(10000);
continue;
}
#endif
if (mSeekReq) {
- LOGV("readerEntry, mSeekReq: %d", mSeekReq);
+ ALOGV("readerEntry, mSeekReq: %d", mSeekReq);
ret = avformat_seek_file(mFormatCtx, -1, INT64_MIN, mSeekPos, INT64_MAX, mSeekFlags);
if (ret < 0) {
- LOGE("%s: error while seeking", mFormatCtx->filename);
+ ALOGE("%s: error while seeking", mFormatCtx->filename);
} else {
if (mAudioStreamIdx >= 0) {
packet_queue_flush(&mAudioQ);
|| ( (mAudioQ .size > MIN_AUDIOQ_SIZE || mAudioStreamIdx < 0)
&& (mVideoQ .nb_packets > MIN_FRAMES || mVideoStreamIdx < 0))) {
#if DEBUG_READ_ENTRY
- LOGV("readerEntry, is full, fuck");
+ ALOGV("readerEntry, is full, fuck");
#endif
/* wait 10 ms */
- SDL_Delay(10);
+ usleep(10000);
continue;
}
pkt->stream_index = mAudioStreamIdx;
packet_queue_put(&mAudioQ, pkt);
}
- SDL_Delay(10);
+ usleep(10000);
#if DEBUG_READ_ENTRY
- LOGV("readerEntry, eof = 1, mVideoQ.size: %d, mVideoQ.nb_packets: %d, mAudioQ.size: %d, mAudioQ.nb_packets: %d",
+ ALOGV("readerEntry, eof = 1, mVideoQ.size: %d, mVideoQ.nb_packets: %d, mAudioQ.size: %d, mAudioQ.nb_packets: %d",
mVideoQ.size, mVideoQ.nb_packets, mAudioQ.size, mAudioQ.nb_packets);
#endif
if (mAudioQ.size + mVideoQ.size == 0) {
- if (mLoop != 1 && (!mLoop || --mLoop)) {
- if (mVideoStreamIdx >= 0) {
- stream_seek(mStartTime != AV_NOPTS_VALUE ? mStartTime : 0, AVMEDIA_TYPE_VIDEO);
- } else if (mAudioStreamIdx >= 0) {
- stream_seek(mStartTime != AV_NOPTS_VALUE ? mStartTime : 0, AVMEDIA_TYPE_AUDIO);
- }
- } else if (mAutoExit) {
+ if (mAutoExit) {
ret = AVERROR_EOF;
goto fail;
}
if (ret < 0) {
if (ret == AVERROR_EOF || url_feof(mFormatCtx->pb))
if (ret == AVERROR_EOF) {
- //LOGV("ret == AVERROR_EOF");
+ //ALOGV("ret == AVERROR_EOF");
}
if (url_feof(mFormatCtx->pb)) {
- //LOGV("url_feof(mFormatCtx->pb)");
+ //ALOGV("url_feof(mFormatCtx->pb)");
}
eof = 1;
mEOF = true;
if (mFormatCtx->pb && mFormatCtx->pb->error) {
- LOGE("mFormatCtx->pb->error: %d", mFormatCtx->pb->error);
+ ALOGE("mFormatCtx->pb->error: %d", mFormatCtx->pb->error);
break;
}
- SDL_Delay(100);
+ usleep(100000);
continue;
}
stream_component_open(mVideoStreamIdx);
if (!mDefersToCreateVideoTrack)
- LOGI("probe packet counter: %d when create video track ok", mProbePkts);
+ ALOGI("probe packet counter: %d when create video track ok", mProbePkts);
if (mProbePkts == EXTRACTOR_MAX_PROBE_PACKETS)
- LOGI("probe packet counter to max: %d, create video track: %d",
+ ALOGI("probe packet counter to max: %d, create video track: %d",
mProbePkts, !mDefersToCreateVideoTrack);
}
} else if (pkt->stream_index == mAudioStreamIdx) {
}
stream_component_open(mAudioStreamIdx);
if (!mDefersToCreateAudioTrack)
- LOGI("probe packet counter: %d when create audio track ok", mProbePkts);
+ ALOGI("probe packet counter: %d when create audio track ok", mProbePkts);
if (mProbePkts == EXTRACTOR_MAX_PROBE_PACKETS)
- LOGI("probe packet counter to max: %d, create audio track: %d",
+ ALOGI("probe packet counter to max: %d, create audio track: %d",
mProbePkts, !mDefersToCreateAudioTrack);
}
}
- /* check if packet is in play range specified by user, then queue, otherwise discard */
- pkt_in_play_range = mDuration == AV_NOPTS_VALUE ||
- (pkt->pts - mFormatCtx->streams[pkt->stream_index]->start_time) *
- av_q2d(mFormatCtx->streams[pkt->stream_index]->time_base) -
- (double)(mStartTime != AV_NOPTS_VALUE ? mStartTime : 0) / 1000000
- <= ((double)mDuration / 1000000);
- if (pkt->stream_index == mAudioStreamIdx && pkt_in_play_range) {
+ if (pkt->stream_index == mAudioStreamIdx) {
packet_queue_put(&mAudioQ, pkt);
- } else if (pkt->stream_index == mVideoStreamIdx && pkt_in_play_range) {
+ } else if (pkt->stream_index == mVideoStreamIdx) {
packet_queue_put(&mVideoQ, pkt);
} else {
av_free_packet(pkt);
}
/* wait until the end */
while (!mAbortRequest) {
- SDL_Delay(100);
+ usleep(100000);
}
ret = 0;
fail:
- LOGI("reader thread goto end...");
+ ALOGI("reader thread goto end...");
/* close each stream */
if (mAudioStreamIdx >= 0)
// The number of bytes used to encode the length of a NAL unit.
mNALLengthSize = 1 + (ptr[4] & 3);
- LOGV("the stream is AVC, the length of a NAL unit: %d", mNALLengthSize);
+ ALOGV("the stream is AVC, the length of a NAL unit: %d", mNALLengthSize);
mNal2AnnexB = true;
}
}
mMediaType = mStream->codec->codec_type;
+ mFirstKeyPktTimestamp = AV_NOPTS_VALUE;
}
FFmpegExtractor::Track::~Track() {
status_t status = OK;
if (options && options->getSeekTo(&seekTimeUs, &mode)) {
- LOGV("~~~%s seekTimeUs: %lld, mode: %d", av_get_media_type_string(mMediaType), seekTimeUs, mode);
+ ALOGV("~~~%s seekTimeUs: %lld, mode: %d", av_get_media_type_string(mMediaType), seekTimeUs, mode);
+ /* add the stream start time */
+ if (mStream->start_time != AV_NOPTS_VALUE)
+ seekTimeUs += mStream->start_time * av_q2d(mStream->time_base) * 1000000;
+ ALOGV("~~~%s seekTimeUs[+startTime]: %lld, mode: %d", av_get_media_type_string(mMediaType), seekTimeUs, mode);
+
if (mExtractor->stream_seek(seekTimeUs, mMediaType) == SEEK)
seeking = true;
}
}
if (pkt.data == flush_pkt.data) {
- LOGV("read %s flush pkt", av_get_media_type_string(mMediaType));
+ ALOGV("read %s flush pkt", av_get_media_type_string(mMediaType));
av_free_packet(&pkt);
+ mFirstKeyPktTimestamp = AV_NOPTS_VALUE;
goto retry;
} else if (pkt.data == NULL && pkt.size == 0) {
- LOGV("read %s eos pkt", av_get_media_type_string(mMediaType));
+ ALOGV("read %s eos pkt", av_get_media_type_string(mMediaType));
av_free_packet(&pkt);
mExtractor->reachedEOS(mMediaType);
return ERROR_END_OF_STREAM;
}
key = pkt.flags & AV_PKT_FLAG_KEY ? 1 : 0;
+ pktTS = pkt.pts;
+ // use dts when AVI
+ if (pkt.pts == AV_NOPTS_VALUE)
+ pktTS = pkt.dts;
if (waitKeyPkt) {
if (!key) {
- LOGV("drop the no key packet");
+ ALOGV("drop the no key packet");
av_free_packet(&pkt);
goto retry;
} else {
- LOGV("~~~~~~ got the key packet");
+ ALOGV("~~~~~~ got the key packet");
waitKeyPkt = false;
}
}
+
+ if (mFirstKeyPktTimestamp == AV_NOPTS_VALUE) {
+ // update the first key timestamp
+ mFirstKeyPktTimestamp = pktTS;
+ }
+ if (pktTS < mFirstKeyPktTimestamp) {
+ ALOGV("drop the packet with the backward timestamp, maybe them are B-frames after I-frame ^_^");
+ av_free_packet(&pkt);
+ goto retry;
+ }
+
MediaBuffer *mediaBuffer = new MediaBuffer(pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
mediaBuffer->meta_data()->clear();
mediaBuffer->set_range(0, pkt.size);
status = ERROR_MALFORMED;
break;
}
- dst += mNALLengthSize;
- ptr += mNALLengthSize;
+ dst += mNALLengthSize;
+ ptr += mNALLengthSize;
len -= mNALLengthSize;
memcpy(dst, ptr, nal_len);
}
if (status != OK) {
- LOGV("status != OK");
+ ALOGV("status != OK");
mediaBuffer->release();
mediaBuffer = NULL;
av_free_packet(&pkt);
memcpy(mediaBuffer->data(), pkt.data, pkt.size);
}
- pktTS = pkt.pts;
- // use dts when AVI
- if (pkt.pts == AV_NOPTS_VALUE)
- pktTS = pkt.dts;
-
-#if 0
- // TODO, Stagefright can't handle negative timestamps
- // if needed, work around this by offsetting them manually?
- if (pktTS < 0)
- pktTS = 0;
-#endif
-
- timeUs = (int64_t)(pktTS * av_q2d(mStream->time_base) * 1000000);
+ int64_t start_time = mStream->start_time != AV_NOPTS_VALUE ? mStream->start_time : 0;
+ timeUs = (int64_t)((pktTS - start_time) * av_q2d(mStream->time_base) * 1000000);
#if 0
- LOGV("read %s pkt, size: %d, key: %d, pts: %lld, dts: %lld, timeUs: %llu us (%.2f secs)",
+ ALOGV("read %s pkt, size: %d, key: %d, pts: %lld, dts: %lld, timeUs[-startTime]: %llu us (%.2f secs)",
av_get_media_type_string(mMediaType), pkt.size, key, pkt.pts, pkt.dts, timeUs, timeUs/1E6);
#endif
-#if 0
- // TODO, Stagefright can't handle negative timestamps
- // if needed, work around this by offsetting them manually?
- if (timeUs < 0)
- timeUs = 0;
-#endif
-
mediaBuffer->meta_data()->setInt64(kKeyTime, timeUs);
mediaBuffer->meta_data()->setInt32(kKeyIsSyncFrame, key);
////////////////////////////////////////////////////////////////////////////////
-// TODO: Temp hack
+// LegacySniffFFMPEG
typedef struct {
- const char *extension;
+ const char *format;
const char *container;
-} extmap;
-
-static extmap FILE_EXTS [] = {
- {".mp4", MEDIA_MIMETYPE_CONTAINER_MPEG4},
- {".3gp", MEDIA_MIMETYPE_CONTAINER_MPEG4},
- {".mp3", MEDIA_MIMETYPE_AUDIO_MPEG},
- {".mov", MEDIA_MIMETYPE_CONTAINER_MOV},
- {".mkv", MEDIA_MIMETYPE_CONTAINER_MATROSKA},
- {".ts", MEDIA_MIMETYPE_CONTAINER_TS},
- {".avi", MEDIA_MIMETYPE_CONTAINER_AVI},
-#if 0
- {".asf", MEDIA_MIMETYPE_CONTAINER_ASF},
- {".wmv", MEDIA_MIMETYPE_CONTAINER_WMV},
- {".wma", MEDIA_MIMETYPE_CONTAINER_WMA},
- {".mpg", MEDIA_MIMETYPE_CONTAINER_MPG},
- {".flv", MEDIA_MIMETYPE_CONTAINER_FLV},
- {".divx", MEDIA_MIMETYPE_CONTAINER_DIVX},
- {".mp2", MEDIA_MIMETYPE_CONTAINER_MP2},
- {".ape", MEDIA_MIMETYPE_CONTAINER_APE},
- {".rm ", MEDIA_MIMETYPE_CONTAINER_RM},
- {".ra", MEDIA_MIMETYPE_CONTAINER_RA},
-#endif
+} formatmap;
+
+static formatmap FILE_FORMATS[] = {
+ {"mpeg", MEDIA_MIMETYPE_CONTAINER_MPEG2PS },
+ {"mpegts", MEDIA_MIMETYPE_CONTAINER_TS },
+ {"mov,mp4,m4a,3gp,3g2,mj2", MEDIA_MIMETYPE_CONTAINER_MOV },
+ {"matroska,webm", MEDIA_MIMETYPE_CONTAINER_MATROSKA },
+ {"asf", MEDIA_MIMETYPE_CONTAINER_ASF },
+ {"rm", MEDIA_MIMETYPE_CONTAINER_RM },
+ {"flv", MEDIA_MIMETYPE_CONTAINER_FLV },
+ {"avi", MEDIA_MIMETYPE_CONTAINER_AVI },
+ {"wav", MEDIA_MIMETYPE_CONTAINER_WAV },
+ {"ape", MEDIA_MIMETYPE_CONTAINER_APE },
+ {"dts", MEDIA_MIMETYPE_CONTAINER_DTS },
+ {"flac", MEDIA_MIMETYPE_CONTAINER_FLAC },
};
-// TODO: check mFormatCtx->iformat->name?
+static void adjustMOVConfidence(AVFormatContext *ic, float *confidence)
+{
+ AVDictionary *tags = NULL;
+ AVDictionaryEntry *tag = NULL;
+
+ tags = ic->metadata;
+
+ //NOTE: You can use command to show these tags,
+ //e.g. "ffprobe -show_format 2012.mov"
+
+ tag = av_dict_get(tags, "major_brand", NULL, 0);
+ if (!tag) {
+ return;
+ }
+
+ ALOGV("major_brand tag is:%s", tag->value);
+
+ //when MEDIA_MIMETYPE_CONTAINER_MOV
+ //WTF, MPEG4Extractor.cpp can not extractor mov format
+ //NOTE: isCompatibleBrand(MPEG4Extractor.cpp)
+ // Won't promise that the following file types can be played.
+ // Just give these file types a chance.
+ // FOURCC('q', 't', ' ', ' '), // Apple's QuickTime
+ //So......
+ if (!strcmp(tag->value, "qt ")) {
+ ALOGI("format is mov, confidence should be larger than mpeg4");
+ //the MEDIA_MIMETYPE_CONTAINER_MPEG4 of confidence is 0.4f
+ *confidence = 0.41f;
+ }
+}
+
+static void adjustVideoCodecConfidence(AVFormatContext *ic,
+ enum AVCodecID codec_id, float *confidence)
+{
+ //add to here
+}
+
+static void adjustAudioCodecConfidence(AVFormatContext *ic,
+ enum AVCodecID codec_id, float *confidence)
+{
+ switch (codec_id) {
+ case CODEC_ID_AC3:
+ //TODO. if the other stream(e.g. mp3) is supported by stagefright
+ ALOGI("ffmpeg can demux ac3 only");
+ *confidence = 0.88f;
+ default:
+ break;
+ }
+}
+
+static void adjustCodecConfidence(AVFormatContext *ic, float *confidence)
+{
+ unsigned int idx = 0;
+ AVCodecContext *avctx = NULL;
+ AVMediaType codec_type = AVMEDIA_TYPE_UNKNOWN;
+ enum AVCodecID codec_id = AV_CODEC_ID_NONE;
+
+ for (idx = 0; idx < ic->nb_streams; idx++)
+ {
+ avctx = ic->streams[idx]->codec;
+ codec_type = avctx->codec_type;
+ codec_id = avctx->codec_id;
+
+ if (codec_type == AVMEDIA_TYPE_VIDEO) {
+ adjustVideoCodecConfidence(ic, codec_id, confidence);
+ } else if (codec_type == AVMEDIA_TYPE_AUDIO) {
+ adjustAudioCodecConfidence(ic, codec_id, confidence);
+ }
+ }
+}
+
+static void adjustConfidenceIfNeeded(const char *mime,
+ AVFormatContext *ic, float *confidence)
+{
+ //check mime
+ if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MOV)) {
+ adjustMOVConfidence(ic, confidence);
+ } else {
+ //add to here;
+ }
+
+ //check codec
+ adjustCodecConfidence(ic, confidence);
+}
+
+static const char *LegacySniffFFMPEG(const char *uri, float *confidence)
+{
+ size_t i;
+ int err;
+ const char *container = NULL;
+ AVFormatContext *ic = NULL;
+
+ status_t status = initFFmpeg();
+ if (status != OK) {
+ ALOGE("could not init ffmpeg");
+ return NULL;
+ }
+
+ ic = avformat_alloc_context();
+ err = avformat_open_input(&ic, uri, NULL, NULL);
+ if (err < 0) {
+ ALOGE("avformat_open_input faild, uri: %s err: %d", uri, err);
+ return NULL;
+ }
+
+ av_dump_format(ic, 0, uri, 0);
+
+ ALOGI("FFmpegExtrator, uri: %s, format_name: %s, format_long_name: %s",
+ uri, ic->iformat->name, ic->iformat->long_name);
+
+ ALOGI("list the formats suppoted by ffmpeg: ");
+ ALOGI("========================================");
+ for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
+ ALOGV("format_names[%02d]: %s", i, FILE_FORMATS[i].format);
+ }
+ ALOGI("========================================");
+
+ for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
+ int len = strlen(FILE_FORMATS[i].format);
+ if (!av_strncasecmp(ic->iformat->name, FILE_FORMATS[i].format, len)) {
+ container = FILE_FORMATS[i].container;
+ break;
+ }
+ }
+
+ if (container) {
+ adjustConfidenceIfNeeded(container, ic, confidence);
+ }
+
+ avformat_close_input(&ic);
+ av_free(ic);
+
+ return container;
+}
+
+static const char *BetterSniffFFMPEG(const sp<DataSource> &source, float *confidence)
+{
+ size_t i;
+ int err;
+ size_t len = 0;
+ char url[128] = {0};
+ const char *container = NULL;
+ AVFormatContext *ic = NULL;
+
+ status_t status = initFFmpeg();
+ if (status != OK) {
+ ALOGE("could not init ffmpeg");
+ return NULL;
+ }
+
+ ALOGI("android-source:%p", &source);
+
+ // pass the addr of smart pointer("source")
+ snprintf(url, sizeof(url), "android-source:%p", &source);
+
+ ic = avformat_alloc_context();
+ err = avformat_open_input(&ic, url, NULL, NULL);
+ if (err < 0) {
+ ALOGE("avformat_open_input faild, url: %s err: %d", url, err);
+ return NULL;
+ }
+
+ av_dump_format(ic, 0, url, 0);
+
+ ALOGI("FFmpegExtrator, url: %s, format_name: %s, format_long_name: %s",
+ url, ic->iformat->name, ic->iformat->long_name);
+
+ ALOGI("list the formats suppoted by ffmpeg: ");
+ ALOGI("========================================");
+ for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
+ ALOGV("format_names[%02d]: %s", i, FILE_FORMATS[i].format);
+ }
+ ALOGI("========================================");
+
+ for (i = 0; i < NELEM(FILE_FORMATS); ++i) {
+ int len = strlen(FILE_FORMATS[i].format);
+ if (!av_strncasecmp(ic->iformat->name, FILE_FORMATS[i].format, len)) {
+ container = FILE_FORMATS[i].container;
+ break;
+ }
+ }
+
+ if (container) {
+ adjustConfidenceIfNeeded(container, ic, confidence);
+ }
+
+ avformat_close_input(&ic);
+ av_free(ic);
+
+ return container;
+}
+
bool SniffFFMPEG(
const sp<DataSource> &source, String8 *mimeType, float *confidence,
sp<AMessage> *meta) {
- LOGV("SniffFFMPEG");
- size_t i;
- const char *uri, *container = NULL;
+ ALOGV("SniffFFMPEG");
- //av_find_input_format()??
- uri = source->getNamURI();
+ *confidence = 0.08f; // be the last resort, by default
- if (!uri)
- return false;
+ const char *container = BetterSniffFFMPEG(source, confidence);
+ if (!container) {
+ String8 uri = source->getUri();
+ if (uri.empty())
+ return false;
- LOGI("ffmpeg uri: %s", uri);
+ ALOGW("sniff through BetterSniffFFMPEG failed, try LegacySniffFFMPEG");
- LOGI("list the file extensions suppoted by ffmpeg: ");
- LOGI("========================================");
- for (i = 0; i < NELEM(FILE_EXTS); ++i) {
- LOGV("file_exts[%02d]: %s", i, FILE_EXTS[i].extension);
- }
- LOGI("========================================");
-
- int lenURI = strlen(uri);
- for (i = 0; i < NELEM(FILE_EXTS); ++i) {
- int len = strlen(FILE_EXTS[i].extension);
- int start = lenURI - len;
- if (start > 0) {
- if (!av_strncasecmp(uri + start, FILE_EXTS[i].extension, len)) {
- container = FILE_EXTS[i].container;
- break;
- }
+ container = LegacySniffFFMPEG(uri, confidence);
+ if (container) {
+ ALOGI("sniff through LegacySniffFFMPEG success");
}
+ } else {
+ ALOGI("sniff through BetterSniffFFMPEG success");
}
if (container == NULL)
return false;
- LOGV("found container: %s", container);
+ ALOGV("found container: %s", container);
- *confidence = 0.88f; // Slightly larger than other extractor's confidence
mimeType->setTo(container);
/* use MPEG4Extractor(not extended extractor) for HTTP source only */
if (!av_strcasecmp(container, MEDIA_MIMETYPE_CONTAINER_MPEG4)
&& (source->flags() & DataSource::kIsCachingDataSource)) {
- return true;
+ return true;
}
+ ALOGD("ffmpeg detected media content as '%s' with confidence %.2f",
+ container, *confidence);
+
*meta = new AMessage;
(*meta)->setString("extended-extractor", "extended-extractor");
(*meta)->setString("extended-extractor-subtype", "ffmpegextractor");
+ //*confidence = 0.88f;
+
+ if (*confidence > 0.08f) {
+ (*meta)->setString("extended-extractor-use", "ffmpegextractor");
+ }
+
return true;
}
+MediaExtractor *CreateFFmpegExtractor(const sp<DataSource> &source, const char *mime, const sp<AMessage> &meta) {
+ MediaExtractor *ret = NULL;
+ AString notuse;
+ if (meta.get() && meta->findString("extended-extractor", ¬use) && (
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG4) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_MPEG) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MOV) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MATROSKA) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_TS) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2PS) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_AVI) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_ASF) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WEBM) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WMV) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPG) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_FLV) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_DIVX) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_RM) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WAV) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_FLAC) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_APE) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_DTS) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MP2) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_RA) ||
+ !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_WMA))) {
+ ret = new FFmpegExtractor(source);
+ }
+
+ ALOGD("%ssupported mime: %s", (ret ? "" : "un"), mime);
+ return ret;
+}
+
} // namespace android
+
+extern "C" void getExtractorPlugin(android::MediaExtractor::Plugin *plugin)
+{
+ plugin->sniff = android::SniffFFMPEG;
+ plugin->create = android::CreateFFmpegExtractor;
+}