OSDN Git Service

disable verbose log
[android-x86/external-stagefright-plugins.git] / utils / codec_utils.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_TAG "codec_utils"
18 #include <utils/Log.h>
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #include "config.h"
25 #include "libavcodec/xiph.h"
26
27 #ifdef __cplusplus
28 }
29 #endif
30
31 #include <utils/Errors.h>
32 #include <media/stagefright/foundation/ADebug.h>
33 #include <media/stagefright/MediaDefs.h>
34 #include <media/stagefright/MediaErrors.h>
35 #include <media/stagefright/MetaData.h>
36 #include "include/avc_utils.h"
37
38 #include "codec_utils.h"
39
40 namespace android {
41
42 static void EncodeSize14(uint8_t **_ptr, size_t size) {
43     CHECK_LE(size, 0x3fff);
44
45     uint8_t *ptr = *_ptr;
46
47     *ptr++ = 0x80 | (size >> 7);
48     *ptr++ = size & 0x7f;
49
50     *_ptr = ptr;
51 }
52
53 static sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) {
54     sp<ABuffer> esds = new ABuffer(csd->size() + 25);
55
56     uint8_t *ptr = esds->data();
57     *ptr++ = 0x03;
58     EncodeSize14(&ptr, 22 + csd->size());
59
60     *ptr++ = 0x00;  // ES_ID
61     *ptr++ = 0x00;
62
63     *ptr++ = 0x00;  // streamDependenceFlag, URL_Flag, OCRstreamFlag
64
65     *ptr++ = 0x04;
66     EncodeSize14(&ptr, 16 + csd->size());
67
68     *ptr++ = 0x40;  // Audio ISO/IEC 14496-3
69
70     for (size_t i = 0; i < 12; ++i) {
71         *ptr++ = 0x00;
72     }
73
74     *ptr++ = 0x05;
75     EncodeSize14(&ptr, csd->size());
76
77     memcpy(ptr, csd->data(), csd->size());
78
79     return esds;
80 }
81
82 //Returns the sample rate based on the sampling frequency index
83 static uint32_t getAACSampleRate(const uint8_t sf_index)
84 {
85     static const uint32_t sample_rates[] =
86     {
87         96000, 88200, 64000, 48000, 44100, 32000,
88         24000, 22050, 16000, 12000, 11025, 8000
89     };
90
91     if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
92         return sample_rates[sf_index];
93     }
94
95     return 0;
96 }
97
98 //video
99
100 //H.264 Video Types
101 //http://msdn.microsoft.com/en-us/library/dd757808(v=vs.85).aspx
102
103 // H.264 bitstream without start codes.
104 sp<MetaData> setAVCFormat(AVCodecContext *avctx)
105 {
106     ALOGV("AVC");
107
108         CHECK_EQ(avctx->codec_id, AV_CODEC_ID_H264);
109         CHECK_GT(avctx->extradata_size, 0);
110         CHECK_EQ(avctx->extradata[0], 1); //configurationVersion
111
112     if (avctx->width == 0 || avctx->height == 0) {
113          int32_t width, height;
114          sp<ABuffer> seqParamSet = new ABuffer(avctx->extradata_size - 8);
115          memcpy(seqParamSet->data(), avctx->extradata + 8, avctx->extradata_size - 8);
116          FindAVCDimensions(seqParamSet, &width, &height);
117          avctx->width  = width;
118          avctx->height = height;
119      }
120
121     sp<MetaData> meta = new MetaData;
122     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
123     meta->setData(kKeyAVCC, kTypeAVCC, avctx->extradata, avctx->extradata_size);
124
125         return meta;
126 }
127
128 // H.264 bitstream with start codes.
129 sp<MetaData> setH264Format(AVCodecContext *avctx)
130 {
131     ALOGV("H264");
132
133         CHECK_EQ(avctx->codec_id, AV_CODEC_ID_H264);
134         CHECK_NE(avctx->extradata[0], 1); //configurationVersion
135
136     sp<ABuffer> buffer = new ABuffer(avctx->extradata_size);
137     memcpy(buffer->data(), avctx->extradata, avctx->extradata_size);
138     return MakeAVCCodecSpecificData(buffer);
139 }
140
141 sp<MetaData> setMPEG4Format(AVCodecContext *avctx)
142 {
143     ALOGV("MPEG4");
144
145     sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
146     memcpy(csd->data(), avctx->extradata, avctx->extradata_size);
147     sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
148
149     sp<MetaData> meta = new MetaData;
150     meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
151     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
152
153     return meta;
154 }
155
156 sp<MetaData> setH263Format(AVCodecContext *avctx)
157 {
158     ALOGV("H263");
159
160     sp<MetaData> meta = new MetaData;
161     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
162
163     return meta;
164 }
165
166 sp<MetaData> setMPEG2VIDEOFormat(AVCodecContext *avctx)
167 {
168     ALOGV("MPEG%uVIDEO", avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 2 : 1);
169
170     sp<ABuffer> csd = new ABuffer(avctx->extradata_size);
171     memcpy(csd->data(), avctx->extradata, avctx->extradata_size);
172     sp<ABuffer> esds = MakeMPEGVideoESDS(csd);
173
174     sp<MetaData> meta = new MetaData;
175     meta->setData(kKeyESDS, kTypeESDS, esds->data(), esds->size());
176     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG2);
177
178     return meta;
179 }
180
181 sp<MetaData> setVC1Format(AVCodecContext *avctx)
182 {
183     ALOGV("VC1");
184
185     sp<MetaData> meta = new MetaData;
186     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_VC1);
187     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
188
189     return meta;
190 }
191
192 sp<MetaData> setWMV1Format(AVCodecContext *avctx)
193 {
194     ALOGV("WMV1");
195
196     sp<MetaData> meta = new MetaData;
197     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
198     meta->setInt32(kKeyWMVVersion, kTypeWMVVer_7);
199
200     return meta;
201 }
202
203 sp<MetaData> setWMV2Format(AVCodecContext *avctx)
204 {
205     ALOGV("WMV2");
206
207     sp<MetaData> meta = new MetaData;
208     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
209     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
210     meta->setInt32(kKeyWMVVersion, kTypeWMVVer_8);
211
212     return meta;
213 }
214
215 sp<MetaData> setWMV3Format(AVCodecContext *avctx)
216 {
217     ALOGV("WMV3");
218
219     sp<MetaData> meta = new MetaData;
220     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_WMV);
221     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
222     meta->setInt32(kKeyWMVVersion, kTypeWMVVer_9);
223
224     return meta;
225 }
226
227 sp<MetaData> setRV20Format(AVCodecContext *avctx)
228 {
229     ALOGV("RV20");
230
231     sp<MetaData> meta = new MetaData;
232     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
233     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
234     meta->setInt32(kKeyRVVersion, kTypeRVVer_G2); //http://en.wikipedia.org/wiki/RealVide
235
236     return meta;
237 }
238
239 sp<MetaData> setRV30Format(AVCodecContext *avctx)
240 {
241     ALOGV("RV30");
242
243     sp<MetaData> meta = new MetaData;
244     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
245     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
246     meta->setInt32(kKeyRVVersion, kTypeRVVer_8); //http://en.wikipedia.org/wiki/RealVide
247
248     return meta;
249 }
250
251 sp<MetaData> setRV40Format(AVCodecContext *avctx)
252 {
253     ALOGV("RV40");
254
255     sp<MetaData> meta = new MetaData;
256     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_RV);
257     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
258     meta->setInt32(kKeyRVVersion, kTypeRVVer_9); //http://en.wikipedia.org/wiki/RealVide
259
260     return meta;
261 }
262
263 sp<MetaData> setFLV1Format(AVCodecContext *avctx)
264 {
265     ALOGV("FLV1(Sorenson H263)");
266
267     sp<MetaData> meta = new MetaData;
268     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_FLV1);
269     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
270
271     return meta;
272 }
273
274 sp<MetaData> setHEVCFormat(AVCodecContext *avctx)
275 {
276     ALOGV("HEVC");
277
278     sp<MetaData> meta = new MetaData;
279     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_HEVC);
280     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
281
282     return meta;
283 }
284
285 //audio
286
287 sp<MetaData> setMP2Format(AVCodecContext *avctx)
288 {
289     ALOGV("MP2");
290
291     sp<MetaData> meta = new MetaData;
292     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG_LAYER_II);
293
294     return meta;
295 }
296
297 sp<MetaData> setMP3Format(AVCodecContext *avctx)
298 {
299     ALOGV("MP3");
300
301     sp<MetaData> meta = new MetaData;
302     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_MPEG);
303
304     return meta;
305 }
306
307 sp<MetaData> setVORBISFormat(AVCodecContext *avctx)
308 {
309     ALOGV("VORBIS");
310
311     uint8_t *header_start[3];
312     int header_len[3];
313     if (avpriv_split_xiph_headers(avctx->extradata,
314                 avctx->extradata_size, 30,
315                 header_start, header_len) < 0) {
316         ALOGE("vorbis extradata corrupt.");
317         return NULL;
318     }
319
320     sp<MetaData> meta = new MetaData;
321     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_VORBIS);
322     //identification header
323     meta->setData(kKeyVorbisInfo,  0, header_start[0], header_len[0]);
324     //setup header
325     meta->setData(kKeyVorbisBooks, 0, header_start[2], header_len[2]);
326
327     return meta;
328 }
329
330 sp<MetaData> setAC3Format(AVCodecContext *avctx)
331 {
332     ALOGV("AC3");
333
334     sp<MetaData> meta = new MetaData;
335     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AC3);
336
337     return meta;
338 }
339
340 sp<MetaData> setAACFormat(AVCodecContext *avctx)
341 {
342     ALOGV("AAC");
343
344     uint32_t sr;
345     const uint8_t *header;
346     uint8_t profile, sf_index, channel;
347
348     header = avctx->extradata;
349     CHECK(header != NULL);
350
351     // AudioSpecificInfo follows
352     // oooo offf fccc c000
353     // o - audioObjectType
354     // f - samplingFreqIndex
355     // c - channelConfig
356     profile = ((header[0] & 0xf8) >> 3) - 1;
357     sf_index = (header[0] & 0x07) << 1 | (header[1] & 0x80) >> 7;
358     sr = getAACSampleRate(sf_index);
359     if (sr == 0) {
360         ALOGE("unsupport the aac sample rate");
361         return NULL;
362     }
363     channel = (header[1] >> 3) & 0xf;
364     ALOGV("aac profile: %d, sf_index: %d, channel: %d", profile, sf_index, channel);
365
366     sp<MetaData> meta = MakeAACCodecSpecificData(profile, sf_index, channel);
367     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
368
369     return meta;
370 }
371
372 sp<MetaData> setWMAV1Format(AVCodecContext *avctx)
373 {
374     ALOGV("WMAV1");
375
376     sp<MetaData> meta = new MetaData;
377     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
378     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
379     meta->setInt32(kKeyWMAVersion, kTypeWMA); //FIXME version?
380
381     return meta;
382 }
383
384 sp<MetaData> setWMAV2Format(AVCodecContext *avctx)
385 {
386     ALOGV("WMAV2");
387
388     sp<MetaData> meta = new MetaData;
389     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
390     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
391     meta->setInt32(kKeyWMAVersion, kTypeWMA);
392
393     return meta;
394 }
395
396 sp<MetaData> setWMAProFormat(AVCodecContext *avctx)
397 {
398     ALOGV("WMAPro");
399
400     sp<MetaData> meta = new MetaData;
401     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
402     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
403     meta->setInt32(kKeyWMAVersion, kTypeWMAPro);
404
405     return meta;
406 }
407
408 sp<MetaData> setWMALossLessFormat(AVCodecContext *avctx)
409 {
410     ALOGV("WMALOSSLESS");
411
412     sp<MetaData> meta = new MetaData;
413     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_WMA);
414     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
415     meta->setInt32(kKeyWMAVersion, kTypeWMALossLess);
416
417     return meta;
418 }
419
420 sp<MetaData> setRAFormat(AVCodecContext *avctx)
421 {
422     ALOGV("COOK");
423
424     sp<MetaData> meta = new MetaData;
425     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RA);
426     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
427
428     return meta;
429 }
430
431 sp<MetaData> setAPEFormat(AVCodecContext *avctx)
432 {
433     ALOGV("APE");
434
435     sp<MetaData> meta = new MetaData;
436     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_APE);
437     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
438
439     return meta;
440 }
441
442 sp<MetaData> setDTSFormat(AVCodecContext *avctx)
443 {
444     ALOGV("DTS");
445
446     sp<MetaData> meta = new MetaData;
447     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_DTS);
448     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
449
450     return meta;
451 }
452
453 sp<MetaData> setFLACFormat(AVCodecContext *avctx)
454 {
455     ALOGV("FLAC");
456
457     sp<MetaData> meta = new MetaData;
458     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_FLAC);
459     meta->setData(kKeyRawCodecSpecificData, 0, avctx->extradata, avctx->extradata_size);
460
461     return meta;
462 }
463
464 //Convert H.264 NAL format to annex b
465 status_t convertNal2AnnexB(uint8_t *dst, size_t dst_size,
466         uint8_t *src, size_t src_size, size_t nal_len_size)
467 {
468     size_t i = 0;
469     size_t nal_len = 0;
470     status_t status = OK;
471
472     CHECK_EQ(dst_size, src_size);
473     CHECK(nal_len_size == 3 || nal_len_size == 4);
474
475     while (src_size >= nal_len_size) {
476         nal_len = 0;
477         for( i = 0; i < nal_len_size; i++ ) {
478             nal_len = (nal_len << 8) | src[i];
479             dst[i] = 0;
480         }
481         dst[nal_len_size - 1] = 1;
482         if (nal_len > INT_MAX || nal_len > src_size) {
483             status = ERROR_MALFORMED;
484             break;
485         }
486         dst += nal_len_size;
487         src += nal_len_size;
488         src_size -= nal_len_size;
489
490         memcpy(dst, src, nal_len);
491
492         dst += nal_len;
493         src += nal_len;
494         src_size -= nal_len;
495     }
496
497     return status;
498 }
499
500 }  // namespace android
501