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.
18 #define LOG_TAG "codec_utils"
19 #include <utils/Log.h>
21 #include <utils/Errors.h>
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>
28 #include "codec_utils.h"
32 static void EncodeSize14(uint8_t **_ptr, size_t size) {
33 CHECK_LE(size, 0x3fff);
37 *ptr++ = 0x80 | (size >> 7);
43 sp<ABuffer> MakeMPEGVideoESDS(const sp<ABuffer> &csd) {
44 sp<ABuffer> esds = new ABuffer(csd->size() + 25);
46 uint8_t *ptr = esds->data();
48 EncodeSize14(&ptr, 22 + csd->size());
50 *ptr++ = 0x00; // ES_ID
53 *ptr++ = 0x00; // streamDependenceFlag, URL_Flag, OCRstreamFlag
56 EncodeSize14(&ptr, 16 + csd->size());
58 *ptr++ = 0x40; // Audio ISO/IEC 14496-3
60 for (size_t i = 0; i < 12; ++i) {
65 EncodeSize14(&ptr, csd->size());
67 memcpy(ptr, csd->data(), csd->size());
72 //Returns the sample rate based on the sampling frequency index
73 uint32_t getAACSampleRate(const uint8_t sf_index)
75 static const uint32_t sample_rates[] =
77 96000, 88200, 64000, 48000, 44100, 32000,
78 24000, 22050, 16000, 12000, 11025, 8000
81 if (sf_index < sizeof(sample_rates) / sizeof(sample_rates[0])) {
82 return sample_rates[sf_index];
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)
96 CHECK_EQ(dst_size, src_size);
97 CHECK(nal_len_size == 3 || nal_len_size == 4);
99 while (src_size >= nal_len_size) {
101 for( i = 0; i < nal_len_size; i++ ) {
102 nal_len = (nal_len << 8) | src[i];
105 dst[nal_len_size - 1] = 1;
106 if (nal_len > INT_MAX || nal_len > src_size) {
107 status = ERROR_MALFORMED;
112 src_size -= nal_len_size;
114 memcpy(dst, src, nal_len);
124 } // namespace android