avc_utils.cpp revision 5bc087c573c70c84c6a39946457590b42d392a33
1/* 2 * Copyright (C) 2010 The Android Open Source Project 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 "avc_utils" 19#include <utils/Log.h> 20 21#include "include/avc_utils.h" 22 23#include <media/stagefright/foundation/ABitReader.h> 24#include <media/stagefright/foundation/ADebug.h> 25#include <media/stagefright/MediaDefs.h> 26#include <media/stagefright/MediaErrors.h> 27#include <media/stagefright/MetaData.h> 28 29namespace android { 30 31unsigned parseUE(ABitReader *br) { 32 unsigned numZeroes = 0; 33 while (br->getBits(1) == 0) { 34 ++numZeroes; 35 } 36 37 unsigned x = br->getBits(numZeroes); 38 39 return x + (1u << numZeroes) - 1; 40} 41 42// Determine video dimensions from the sequence parameterset. 43void FindAVCDimensions( 44 const sp<ABuffer> &seqParamSet, int32_t *width, int32_t *height) { 45 ABitReader br(seqParamSet->data() + 1, seqParamSet->size() - 1); 46 47 unsigned profile_idc = br.getBits(8); 48 br.skipBits(16); 49 parseUE(&br); // seq_parameter_set_id 50 51 unsigned chroma_format_idc = 1; // 4:2:0 chroma format 52 53 if (profile_idc == 100 || profile_idc == 110 54 || profile_idc == 122 || profile_idc == 244 55 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86) { 56 chroma_format_idc = parseUE(&br); 57 if (chroma_format_idc == 3) { 58 br.skipBits(1); // residual_colour_transform_flag 59 } 60 parseUE(&br); // bit_depth_luma_minus8 61 parseUE(&br); // bit_depth_chroma_minus8 62 br.skipBits(1); // qpprime_y_zero_transform_bypass_flag 63 CHECK_EQ(br.getBits(1), 0u); // seq_scaling_matrix_present_flag 64 } 65 66 parseUE(&br); // log2_max_frame_num_minus4 67 unsigned pic_order_cnt_type = parseUE(&br); 68 69 if (pic_order_cnt_type == 0) { 70 parseUE(&br); // log2_max_pic_order_cnt_lsb_minus4 71 } else if (pic_order_cnt_type == 1) { 72 // offset_for_non_ref_pic, offset_for_top_to_bottom_field and 73 // offset_for_ref_frame are technically se(v), but since we are 74 // just skipping over them the midpoint does not matter. 75 76 br.getBits(1); // delta_pic_order_always_zero_flag 77 parseUE(&br); // offset_for_non_ref_pic 78 parseUE(&br); // offset_for_top_to_bottom_field 79 80 unsigned num_ref_frames_in_pic_order_cnt_cycle = parseUE(&br); 81 for (unsigned i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; ++i) { 82 parseUE(&br); // offset_for_ref_frame 83 } 84 } 85 86 parseUE(&br); // num_ref_frames 87 br.getBits(1); // gaps_in_frame_num_value_allowed_flag 88 89 unsigned pic_width_in_mbs_minus1 = parseUE(&br); 90 unsigned pic_height_in_map_units_minus1 = parseUE(&br); 91 unsigned frame_mbs_only_flag = br.getBits(1); 92 93 *width = pic_width_in_mbs_minus1 * 16 + 16; 94 95 *height = (2 - frame_mbs_only_flag) 96 * (pic_height_in_map_units_minus1 * 16 + 16); 97 98 if (!frame_mbs_only_flag) { 99 br.getBits(1); // mb_adaptive_frame_field_flag 100 } 101 102 br.getBits(1); // direct_8x8_inference_flag 103 104 if (br.getBits(1)) { // frame_cropping_flag 105 unsigned frame_crop_left_offset = parseUE(&br); 106 unsigned frame_crop_right_offset = parseUE(&br); 107 unsigned frame_crop_top_offset = parseUE(&br); 108 unsigned frame_crop_bottom_offset = parseUE(&br); 109 110 unsigned cropUnitX, cropUnitY; 111 if (chroma_format_idc == 0 /* monochrome */) { 112 cropUnitX = 1; 113 cropUnitY = 2 - frame_mbs_only_flag; 114 } else { 115 unsigned subWidthC = (chroma_format_idc == 3) ? 1 : 2; 116 unsigned subHeightC = (chroma_format_idc == 1) ? 2 : 1; 117 118 cropUnitX = subWidthC; 119 cropUnitY = subHeightC * (2 - frame_mbs_only_flag); 120 } 121 122 LOGV("frame_crop = (%u, %u, %u, %u), cropUnitX = %u, cropUnitY = %u", 123 frame_crop_left_offset, frame_crop_right_offset, 124 frame_crop_top_offset, frame_crop_bottom_offset, 125 cropUnitX, cropUnitY); 126 127 *width -= 128 (frame_crop_left_offset + frame_crop_right_offset) * cropUnitX; 129 *height -= 130 (frame_crop_top_offset + frame_crop_bottom_offset) * cropUnitY; 131 } 132} 133 134status_t getNextNALUnit( 135 const uint8_t **_data, size_t *_size, 136 const uint8_t **nalStart, size_t *nalSize, 137 bool startCodeFollows) { 138 const uint8_t *data = *_data; 139 size_t size = *_size; 140 141 *nalStart = NULL; 142 *nalSize = 0; 143 144 if (size == 0) { 145 return -EAGAIN; 146 } 147 148 // Skip any number of leading 0x00. 149 150 size_t offset = 0; 151 while (offset < size && data[offset] == 0x00) { 152 ++offset; 153 } 154 155 if (offset == size) { 156 return -EAGAIN; 157 } 158 159 // A valid startcode consists of at least two 0x00 bytes followed by 0x01. 160 161 if (offset < 2 || data[offset] != 0x01) { 162 return ERROR_MALFORMED; 163 } 164 165 ++offset; 166 167 size_t startOffset = offset; 168 169 for (;;) { 170 while (offset < size && data[offset] != 0x01) { 171 ++offset; 172 } 173 174 if (offset == size) { 175 if (startCodeFollows) { 176 offset = size + 2; 177 break; 178 } 179 180 return -EAGAIN; 181 } 182 183 if (data[offset - 1] == 0x00 && data[offset - 2] == 0x00) { 184 break; 185 } 186 187 ++offset; 188 } 189 190 size_t endOffset = offset - 2; 191 while (data[endOffset - 1] == 0x00) { 192 --endOffset; 193 } 194 195 *nalStart = &data[startOffset]; 196 *nalSize = endOffset - startOffset; 197 198 if (offset + 2 < size) { 199 *_data = &data[offset - 2]; 200 *_size = size - offset + 2; 201 } else { 202 *_data = NULL; 203 *_size = 0; 204 } 205 206 return OK; 207} 208 209static sp<ABuffer> FindNAL( 210 const uint8_t *data, size_t size, unsigned nalType, 211 size_t *stopOffset) { 212 const uint8_t *nalStart; 213 size_t nalSize; 214 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) { 215 if ((nalStart[0] & 0x1f) == nalType) { 216 sp<ABuffer> buffer = new ABuffer(nalSize); 217 memcpy(buffer->data(), nalStart, nalSize); 218 return buffer; 219 } 220 } 221 222 return NULL; 223} 224 225const char *AVCProfileToString(uint8_t profile) { 226 switch (profile) { 227 case kAVCProfileBaseline: 228 return "Baseline"; 229 case kAVCProfileMain: 230 return "Main"; 231 case kAVCProfileExtended: 232 return "Extended"; 233 case kAVCProfileHigh: 234 return "High"; 235 case kAVCProfileHigh10: 236 return "High 10"; 237 case kAVCProfileHigh422: 238 return "High 422"; 239 case kAVCProfileHigh444: 240 return "High 444"; 241 case kAVCProfileCAVLC444Intra: 242 return "CAVLC 444 Intra"; 243 default: return "Unknown"; 244 } 245} 246 247sp<MetaData> MakeAVCCodecSpecificData(const sp<ABuffer> &accessUnit) { 248 const uint8_t *data = accessUnit->data(); 249 size_t size = accessUnit->size(); 250 251 sp<ABuffer> seqParamSet = FindNAL(data, size, 7, NULL); 252 if (seqParamSet == NULL) { 253 return NULL; 254 } 255 256 int32_t width, height; 257 FindAVCDimensions(seqParamSet, &width, &height); 258 259 size_t stopOffset; 260 sp<ABuffer> picParamSet = FindNAL(data, size, 8, &stopOffset); 261 CHECK(picParamSet != NULL); 262 263 size_t csdSize = 264 1 + 3 + 1 + 1 265 + 2 * 1 + seqParamSet->size() 266 + 1 + 2 * 1 + picParamSet->size(); 267 268 sp<ABuffer> csd = new ABuffer(csdSize); 269 uint8_t *out = csd->data(); 270 271 *out++ = 0x01; // configurationVersion 272 memcpy(out, seqParamSet->data() + 1, 3); // profile/level... 273 274 uint8_t profile = out[0]; 275 uint8_t level = out[2]; 276 277 out += 3; 278 *out++ = (0x3f << 2) | 1; // lengthSize == 2 bytes 279 *out++ = 0xe0 | 1; 280 281 *out++ = seqParamSet->size() >> 8; 282 *out++ = seqParamSet->size() & 0xff; 283 memcpy(out, seqParamSet->data(), seqParamSet->size()); 284 out += seqParamSet->size(); 285 286 *out++ = 1; 287 288 *out++ = picParamSet->size() >> 8; 289 *out++ = picParamSet->size() & 0xff; 290 memcpy(out, picParamSet->data(), picParamSet->size()); 291 292#if 0 293 LOGI("AVC seq param set"); 294 hexdump(seqParamSet->data(), seqParamSet->size()); 295#endif 296 297 sp<MetaData> meta = new MetaData; 298 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC); 299 300 meta->setData(kKeyAVCC, 0, csd->data(), csd->size()); 301 meta->setInt32(kKeyWidth, width); 302 meta->setInt32(kKeyHeight, height); 303 304 LOGI("found AVC codec config (%d x %d, %s-profile level %d.%d)", 305 width, height, AVCProfileToString(profile), level / 10, level % 10); 306 307 return meta; 308} 309 310bool IsIDR(const sp<ABuffer> &buffer) { 311 const uint8_t *data = buffer->data(); 312 size_t size = buffer->size(); 313 314 bool foundIDR = false; 315 316 const uint8_t *nalStart; 317 size_t nalSize; 318 while (getNextNALUnit(&data, &size, &nalStart, &nalSize, true) == OK) { 319 CHECK_GT(nalSize, 0u); 320 321 unsigned nalType = nalStart[0] & 0x1f; 322 323 if (nalType == 5) { 324 foundIDR = true; 325 break; 326 } 327 } 328 329 return foundIDR; 330} 331 332} // namespace android 333 334