OSDN Git Service

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