2 * Copyright 2012 Michael Chen <omxcodec@gmail.com>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "codec_utils"
19 #include <utils/Log.h>
26 #include "libavcodec/xiph.h"
27 #include "libavutil/intreadwrite.h"
33 #include <utils/Errors.h>
34 #include <media/stagefright/foundation/ABitReader.h>
35 #include <media/stagefright/foundation/ADebug.h>
36 #include <media/stagefright/MediaDefs.h>
37 #include <media/stagefright/MediaErrors.h>
38 #include <media/stagefright/MetaData.h>
39 #include "include/avc_utils.h"
41 #include "codec_utils.h"
45 static void EncodeSize14(uint8_t **_ptr, size_t size) {
46 CHECK_LE(size, 0x3fff);
50 *ptr++ = 0x80 | (size >> 7);
56 static sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) {
57 sp<ABuffer> esds = new ABuffer(csd->size() + 25);
59 uint8_t *ptr = esds->data();
61 EncodeSize14(&ptr, 22 + csd->size());
63 *ptr++ = 0x00; // ES_ID
66 *ptr++ = 0x00; // streamDependenceFlag, URL_Flag, OCRstreamFlag
69 EncodeSize14(&ptr, 16 + csd->size());
71 *ptr++ = 0x40; // Audio ISO/IEC 14496-3
73 for (size_t i = 0; i < 12; ++i) {
78 EncodeSize14(&ptr, csd->size());
80 memcpy(ptr, csd->data(), csd->size());
88 //http://msdn.microsoft.com/en-us/library/dd757808(v=vs.85).aspx
90 // H.264 bitstream without start codes.
91 sp<MetaData> setAVCFormat(AVCodecContext *avctx)
95 CHECK_EQ(avctx->codec_id, AV_CODEC_ID_H264);
96 CHECK_GT(avctx->extradata_size, 0);
97 CHECK_EQ(avctx->extradata[0], 1); //configurationVersion
99 if (avctx->width == 0 || avctx->height == 0) {
100 int32_t width, height;
101 sp<ABuffer> seqParamSet = new ABuffer(avctx->extradata_size - 8);
102 memcpy(seqParamSet->data(), avctx->extradata + 8, avctx->extradata_size - 8);
103 FindAVCDimensions(seqParamSet, &width, &height);
104 avctx->width = width;
105 avctx->height = height;
108 sp<MetaData> meta = new MetaData;
109 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
110 meta->setData(kKeyAVCC, kTypeAVCC, avctx->extradata, avctx->extradata_size);
115 // H.264 bitstream with start codes.
116 sp<MetaData> setH264Format(AVCodecContext *avctx)
120 CHECK_EQ(avctx->codec_id, AV_CODEC_ID_H264);
121 CHECK_NE(avctx->extradata[0], 1); //configurationVersion
123 sp<ABuffer> buffer = new ABuffer(avctx->extradata_size);
124 memcpy(buffer->data(), avctx->extradata, avctx->extradata_size);
125 return MakeAVCCodecSpecificData(buffer);
128 sp<MetaData> setMPEG4Format(AVCodecContext *avctx)
132 sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
133 memcpy(csd->data(), avctx->extradata, avctx->extradata_size);
134 sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
136 sp<MetaData> meta = new MetaData;
137 meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
139 int divxVersion = getDivXVersion(avctx);
140 if (divxVersion >= 0) {
141 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_DIVX);
142 meta->setInt32(kKeyDivXVersion, divxVersion);
144 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
149 sp<MetaData> setH263Format(AVCodecContext *avctx __unused)
153 sp<MetaData> meta = new MetaData;
154 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
159 sp<MetaData> setMPEG2VIDEOFormat(AVCodecContext *avctx)
161 ALOGV("MPEG%uVIDEO", avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 2 : 1);
163 sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
164 memcpy(csd->data(), avctx->extradata, avctx->extradata_size);
165 sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
167 sp<MetaData> meta = new MetaData;
168 meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
169 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
174 sp<MetaData> setVC1Format(AVCodecContext *avctx)
178 sp<MetaData> meta = new MetaData;
179 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_VC1);
180 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
185 sp<MetaData> setWMV1Format(AVCodecContext *avctx __unused)
189 sp<MetaData> meta = new MetaData;
190 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
191 meta->setInt32(kKeyWMVVersion, kTypeWMVVer_7);
196 sp<MetaData> setWMV2Format(AVCodecContext *avctx)
200 sp<MetaData> meta = new MetaData;
201 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
202 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
203 meta->setInt32(kKeyWMVVersion, kTypeWMVVer_8);
208 sp<MetaData> setWMV3Format(AVCodecContext *avctx)
212 sp<MetaData> meta = new MetaData;
213 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
214 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
215 meta->setInt32(kKeyWMVVersion, kTypeWMVVer_9);
220 sp<MetaData> setRV20Format(AVCodecContext *avctx)
224 sp<MetaData> meta = new MetaData;
225 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
226 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
227 meta->setInt32(kKeyRVVersion, kTypeRVVer_G2); //http://en.wikipedia.org/wiki/RealVide
232 sp<MetaData> setRV30Format(AVCodecContext *avctx)
236 sp<MetaData> meta = new MetaData;
237 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
238 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
239 meta->setInt32(kKeyRVVersion, kTypeRVVer_8); //http://en.wikipedia.org/wiki/RealVide
244 sp<MetaData> setRV40Format(AVCodecContext *avctx)
248 sp<MetaData> meta = new MetaData;
249 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
250 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
251 meta->setInt32(kKeyRVVersion, kTypeRVVer_9); //http://en.wikipedia.org/wiki/RealVide
256 sp<MetaData> setFLV1Format(AVCodecContext *avctx)
258 ALOGV("FLV1(Sorenson H263)");
260 sp<MetaData> meta = new MetaData;
261 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_FLV1);
262 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
267 sp<MetaData> setHEVCFormat(AVCodecContext *avctx)
271 sp<MetaData> meta = new MetaData;
272 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_HEVC);
273 meta->setData(kKeyHVCC, kTypeHVCC, avctx->extradata, avctx->extradata_size);
278 sp<MetaData> setVP8Format(AVCodecContext *avctx __unused)
282 sp<MetaData> meta = new MetaData;
283 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_VP8);
288 sp<MetaData> setVP9Format(AVCodecContext *avctx __unused)
292 sp<MetaData> meta = new MetaData;
293 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_VP9);
300 sp<MetaData> setMP2Format(AVCodecContext *avctx __unused)
304 sp<MetaData> meta = new MetaData;
305 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
310 sp<MetaData> setMP3Format(AVCodecContext *avctx __unused)
314 sp<MetaData> meta = new MetaData;
315 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
320 sp<MetaData> setVORBISFormat(AVCodecContext *avctx)
324 const uint8_t *header_start[3];
326 if (avpriv_split_xiph_headers(avctx->extradata,
327 avctx->extradata_size, 30,
328 header_start, header_len) < 0) {
329 ALOGE("vorbis extradata corrupt.");
333 sp<MetaData> meta = new MetaData;
334 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_VORBIS);
335 //identification header
336 meta->setData(kKeyVorbisInfo, 0, header_start[0], header_len[0]);
338 meta->setData(kKeyVorbisBooks, 0, header_start[2], header_len[2]);
343 sp<MetaData> setAC3Format(AVCodecContext *avctx __unused)
347 sp<MetaData> meta = new MetaData;
348 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3);
353 sp<MetaData> setAACFormat(AVCodecContext *avctx)
357 sp<MetaData> meta = new MetaData;
358 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
359 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
360 meta->setInt32(kKeyAACAOT, avctx->profile + 1);
364 sp<MetaData> setWMAV1Format(AVCodecContext *avctx)
368 sp<MetaData> meta = new MetaData;
369 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
370 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
371 meta->setInt32(kKeyWMAVersion, kTypeWMA); //FIXME version?
376 sp<MetaData> setWMAV2Format(AVCodecContext *avctx)
380 sp<MetaData> meta = new MetaData;
381 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
382 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
383 meta->setInt32(kKeyWMAVersion, kTypeWMA);
388 sp<MetaData> setWMAProFormat(AVCodecContext *avctx)
392 sp<MetaData> meta = new MetaData;
393 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
394 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
395 meta->setInt32(kKeyWMAVersion, kTypeWMAPro);
400 sp<MetaData> setWMALossLessFormat(AVCodecContext *avctx)
402 ALOGV("WMALOSSLESS");
404 sp<MetaData> meta = new MetaData;
405 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
406 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
407 meta->setInt32(kKeyWMAVersion, kTypeWMALossLess);
412 sp<MetaData> setRAFormat(AVCodecContext *avctx)
416 sp<MetaData> meta = new MetaData;
417 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RA);
418 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
423 sp<MetaData> setALACFormat(AVCodecContext *avctx)
427 sp<MetaData> meta = new MetaData;
428 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_ALAC);
429 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
434 sp<MetaData> setAPEFormat(AVCodecContext *avctx)
438 sp<MetaData> meta = new MetaData;
439 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_APE);
440 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
445 sp<MetaData> setDTSFormat(AVCodecContext *avctx)
449 sp<MetaData> meta = new MetaData;
450 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_DTS);
451 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
456 sp<MetaData> setFLACFormat(AVCodecContext *avctx)
460 sp<MetaData> meta = new MetaData;
461 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_FLAC);
462 meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
464 if (avctx->extradata_size < 10) {
465 ALOGE("Invalid extradata in FLAC file! (size=%d)", avctx->extradata_size);
469 ABitReader br(avctx->extradata, avctx->extradata_size);
470 int32_t minBlockSize = br.getBits(16);
471 int32_t maxBlockSize = br.getBits(16);
472 int32_t minFrameSize = br.getBits(24);
473 int32_t maxFrameSize = br.getBits(24);
475 meta->setInt32('mibs', minBlockSize);
476 meta->setInt32('mabs', maxBlockSize);
477 meta->setInt32('mifs', minFrameSize);
478 meta->setInt32('mafs', maxFrameSize);
483 //Convert H.264 NAL format to annex b
484 status_t convertNal2AnnexB(uint8_t *dst, size_t dst_size,
485 uint8_t *src, size_t src_size, size_t nal_len_size)
489 status_t status = OK;
491 CHECK_EQ(dst_size, src_size);
492 CHECK(nal_len_size == 3 || nal_len_size == 4);
494 while (src_size >= nal_len_size) {
496 for( i = 0; i < nal_len_size; i++ ) {
497 nal_len = (nal_len << 8) | src[i];
500 dst[nal_len_size - 1] = 1;
501 if (nal_len > INT_MAX || nal_len > src_size) {
502 status = ERROR_MALFORMED;
507 src_size -= nal_len_size;
509 memcpy(dst, src, nal_len);
519 int getDivXVersion(AVCodecContext *avctx)
521 if (avctx->codec_tag == AV_RL32("DIV3")
522 || avctx->codec_tag == AV_RL32("div3")
523 || avctx->codec_tag == AV_RL32("DIV4")
524 || avctx->codec_tag == AV_RL32("div4")) {
525 return kTypeDivXVer_3_11;
527 if (avctx->codec_tag == AV_RL32("DIVX")
528 || avctx->codec_tag == AV_RL32("divx")) {
529 return kTypeDivXVer_4;
531 if (avctx->codec_tag == AV_RL32("DX50")
532 || avctx->codec_tag == AV_RL32("dx50")) {
533 return kTypeDivXVer_5;
538 status_t parseMetadataTags(AVFormatContext *ctx, const sp<MetaData> &meta) {
539 if (meta == NULL || ctx == NULL) {
543 AVDictionary *dict = ctx->metadata;
548 struct MetadataMapping {
553 // avformat -> android mapping
554 static const MetadataMapping kMap[] = {
555 { "track", kKeyCDTrackNumber },
556 { "disc", kKeyDiscNumber },
557 { "album", kKeyAlbum },
558 { "artist", kKeyArtist },
559 { "album_artist", kKeyAlbumArtist },
560 { "composer", kKeyComposer },
561 { "date", kKeyDate },
562 { "genre", kKeyGenre },
563 { "title", kKeyTitle },
564 { "year", kKeyYear },
565 { "compilation", kKeyCompilation },
566 { "location", kKeyLocation },
569 static const size_t kNumEntries = sizeof(kMap) / sizeof(kMap[0]);
571 for (size_t i = 0; i < kNumEntries; ++i) {
572 AVDictionaryEntry *entry = av_dict_get(dict, kMap[i].from, NULL, 0);
574 ALOGV("found key %s with value %s", entry->key, entry->value);
575 meta->setCString(kMap[i].to, entry->value);
579 // now look for album art- this will be in a separate stream
580 for (size_t i = 0; i < ctx->nb_streams; i++) {
581 if (ctx->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC) {
582 AVPacket pkt = ctx->streams[i]->attached_pic;
584 if (ctx->streams[i]->codec != NULL) {
586 if (ctx->streams[i]->codec->codec_id == AV_CODEC_ID_MJPEG) {
587 mime = MEDIA_MIMETYPE_IMAGE_JPEG;
588 } else if (ctx->streams[i]->codec->codec_id == AV_CODEC_ID_PNG) {
594 ALOGV("found albumart in stream %zu with type %s len %d", i, mime, pkt.size);
595 meta->setData(kKeyAlbumArt, MetaData::TYPE_NONE, pkt.data, pkt.size);
596 meta->setCString(kKeyAlbumArtMIME, mime);
605 AudioEncoding sampleFormatToEncoding(AVSampleFormat fmt) {
607 // we resample planar formats to interleaved
609 case AV_SAMPLE_FMT_U8:
610 case AV_SAMPLE_FMT_U8P:
611 return kAudioEncodingPcm8bit;
612 case AV_SAMPLE_FMT_S16:
613 case AV_SAMPLE_FMT_S16P:
614 return kAudioEncodingPcm16bit;
615 case AV_SAMPLE_FMT_S32:
616 case AV_SAMPLE_FMT_S32P:
617 return kAudioEncodingPcm32bit;
618 case AV_SAMPLE_FMT_FLT:
619 case AV_SAMPLE_FMT_FLTP:
620 return kAudioEncodingPcmFloat;
621 case AV_SAMPLE_FMT_DBL:
622 case AV_SAMPLE_FMT_DBLP:
623 return kAudioEncodingPcmFloat;
625 return kAudioEncodingInvalid;
630 AVSampleFormat encodingToSampleFormat(AudioEncoding encoding) {
632 case kAudioEncodingPcm8bit:
633 return AV_SAMPLE_FMT_U8;
634 case kAudioEncodingPcm16bit:
635 return AV_SAMPLE_FMT_S16;
636 case kAudioEncodingPcm24bitPacked:
637 case kAudioEncodingPcm32bit:
638 return AV_SAMPLE_FMT_S32;
639 case kAudioEncodingPcmFloat:
640 return AV_SAMPLE_FMT_FLT;
642 return AV_SAMPLE_FMT_NONE;
646 } // namespace android