OSDN Git Service

move some codec utils to codec_utils.cpp
[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 #include <utils/Errors.h>
22
23 #include <media/stagefright/foundation/ADebug.h>
24 #include <media/stagefright/MediaDefs.h>
25 #include <media/stagefright/MediaErrors.h>
26 #include <media/stagefright/MetaData.h>
27
28 #include "codec_utils.h"
29
30 namespace android {
31
32 static void EncodeSize14(uint8_t **_ptr, size_t size) {
33     CHECK_LE(size, 0x3fff);
34
35     uint8_t *ptr = *_ptr;
36
37     *ptr++ = 0x80 | (size >> 7);
38     *ptr++ = size & 0x7f;
39
40     *_ptr = ptr;
41 }
42
43 sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) {
44     sp<ABuffer> esds = new ABuffer(csd->size() + 25);
45
46     uint8_t *ptr = esds->data();
47     *ptr++ = 0x03;
48     EncodeSize14(&ptr, 22 + csd->size());
49
50     *ptr++ = 0x00;  // ES_ID
51     *ptr++ = 0x00;
52
53     *ptr++ = 0x00;  // streamDependenceFlag, URL_Flag, OCRstreamFlag
54
55     *ptr++ = 0x04;
56     EncodeSize14(&ptr, 16 + csd->size());
57
58     *ptr++ = 0x40;  // Audio ISO/IEC 14496-3
59
60     for (size_t i = 0; i < 12; ++i) {
61         *ptr++ = 0x00;
62     }
63
64     *ptr++ = 0x05;
65     EncodeSize14(&ptr, csd->size());
66
67     memcpy(ptr, csd->data(), csd->size());
68
69     return esds;
70 }
71
72 //Returns the sample rate based on the sampling frequency index
73 uint32_t getAACSampleRate(const uint8_t sf_index)
74 {
75     static const uint32_t sample_rates[] =
76     {
77         96000, 88200, 64000, 48000, 44100, 32000,
78         24000, 22050, 16000, 12000, 11025, 8000
79     };
80
81     if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
82         return sample_rates[sf_index];
83     }
84
85     return 0;
86 }
87
88 //Convert H.264 NAL format to annex b
89 status_t convertNal2AnnexB(uint8_t *dst, size_t dst_size,
90         uint8_t *src, size_t src_size, size_t nal_len_size)
91 {
92     size_t i = 0;
93     size_t nal_len = 0;
94     status_t status = OK;
95
96     CHECK_EQ(dst_size, src_size);
97     CHECK(nal_len_size == 3 || nal_len_size == 4);
98
99     while (src_size >= nal_len_size) {
100         nal_len = 0;
101         for( i = 0; i < nal_len_size; i++ ) {
102             nal_len = (nal_len << 8) | src[i];
103             dst[i] = 0;
104         }
105         dst[nal_len_size - 1] = 1;
106         if (nal_len > INT_MAX || nal_len > src_size) {
107             status = ERROR_MALFORMED;
108             break;
109         }
110         dst += nal_len_size;
111         src += nal_len_size;
112         src_size -= nal_len_size;
113
114         memcpy(dst, src, nal_len);
115
116         dst += nal_len;
117         src += nal_len;
118         src_size -= nal_len;
119     }
120
121     return status;
122 }
123
124 }  // namespace android
125