| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * AVC helper functions for muxers | ||
| 3 | * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com> | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "libavutil/intreadwrite.h" | ||
| 23 | #include "libavutil/mem.h" | ||
| 24 | #include "libavcodec/h264.h" | ||
| 25 | #include "libavcodec/get_bits.h" | ||
| 26 | #include "avio.h" | ||
| 27 | #include "avc.h" | ||
| 28 | #include "avio_internal.h" | ||
| 29 | #include "nal.h" | ||
| 30 | |||
| 31 | 62 | int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len) | |
| 32 | { | ||
| 33 | 62 | AVIOContext *sps_pb = NULL, *pps_pb = NULL, *sps_ext_pb = NULL; | |
| 34 | uint8_t *buf, *end, *start; | ||
| 35 | uint8_t *sps, *pps, *sps_ext; | ||
| 36 | 62 | uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0; | |
| 37 | 62 | int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0; | |
| 38 | |||
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if (len <= 6) |
| 40 | ✗ | return AVERROR_INVALIDDATA; | |
| 41 | |||
| 42 | /* check for H.264 start code */ | ||
| 43 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 4 times.
|
62 | if (AV_RB32(data) != 0x00000001 && |
| 44 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | AV_RB24(data) != 0x000001) { |
| 45 | 58 | avio_write(pb, data, len); | |
| 46 | 58 | return 0; | |
| 47 | } | ||
| 48 | |||
| 49 | 4 | ret = ff_nal_parse_units_buf(data, &buf, &len); | |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
| 51 | ✗ | return ret; | |
| 52 | 4 | start = buf; | |
| 53 | 4 | end = buf + len; | |
| 54 | |||
| 55 | 4 | ret = avio_open_dyn_buf(&sps_pb); | |
| 56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
| 57 | ✗ | goto fail; | |
| 58 | 4 | ret = avio_open_dyn_buf(&pps_pb); | |
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
| 60 | ✗ | goto fail; | |
| 61 | 4 | ret = avio_open_dyn_buf(&sps_ext_pb); | |
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
| 63 | ✗ | goto fail; | |
| 64 | |||
| 65 | /* look for sps and pps */ | ||
| 66 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
|
12 | while (end - buf > 4) { |
| 67 | uint32_t size; | ||
| 68 | uint8_t nal_type; | ||
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | size = FFMIN(AV_RB32(buf), end - buf - 4); |
| 70 | 8 | buf += 4; | |
| 71 | 8 | nal_type = buf[0] & 0x1f; | |
| 72 | |||
| 73 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (nal_type == 7) { /* SPS */ |
| 74 | 4 | nb_sps++; | |
| 75 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (size > UINT16_MAX || nb_sps >= H264_MAX_SPS_COUNT) { |
| 76 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 77 | ✗ | goto fail; | |
| 78 | } | ||
| 79 | 4 | avio_wb16(sps_pb, size); | |
| 80 | 4 | avio_write(sps_pb, buf, size); | |
| 81 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | } else if (nal_type == 8) { /* PPS */ |
| 82 | 4 | nb_pps++; | |
| 83 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (size > UINT16_MAX || nb_pps >= H264_MAX_PPS_COUNT) { |
| 84 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 85 | ✗ | goto fail; | |
| 86 | } | ||
| 87 | 4 | avio_wb16(pps_pb, size); | |
| 88 | 4 | avio_write(pps_pb, buf, size); | |
| 89 | ✗ | } else if (nal_type == 13) { /* SPS_EXT */ | |
| 90 | ✗ | nb_sps_ext++; | |
| 91 | ✗ | if (size > UINT16_MAX || nb_sps_ext >= 256) { | |
| 92 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 93 | ✗ | goto fail; | |
| 94 | } | ||
| 95 | ✗ | avio_wb16(sps_ext_pb, size); | |
| 96 | ✗ | avio_write(sps_ext_pb, buf, size); | |
| 97 | } | ||
| 98 | |||
| 99 | 8 | buf += size; | |
| 100 | } | ||
| 101 | 4 | sps_size = avio_get_dyn_buf(sps_pb, &sps); | |
| 102 | 4 | pps_size = avio_get_dyn_buf(pps_pb, &pps); | |
| 103 | 4 | sps_ext_size = avio_get_dyn_buf(sps_ext_pb, &sps_ext); | |
| 104 | |||
| 105 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (sps_size < 6 || !pps_size) { |
| 106 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 107 | ✗ | goto fail; | |
| 108 | } | ||
| 109 | |||
| 110 | 4 | avio_w8(pb, 1); /* version */ | |
| 111 | 4 | avio_w8(pb, sps[3]); /* profile */ | |
| 112 | 4 | avio_w8(pb, sps[4]); /* profile compat */ | |
| 113 | 4 | avio_w8(pb, sps[5]); /* level */ | |
| 114 | 4 | avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */ | |
| 115 | 4 | avio_w8(pb, 0xe0 | nb_sps); /* 3 bits reserved (111) + 5 bits number of sps */ | |
| 116 | |||
| 117 | 4 | avio_write(pb, sps, sps_size); | |
| 118 | 4 | avio_w8(pb, nb_pps); /* number of pps */ | |
| 119 | 4 | avio_write(pb, pps, pps_size); | |
| 120 | |||
| 121 |
4/6✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
4 | if (sps[3] != 66 && sps[3] != 77 && sps[3] != 88) { |
| 122 | H264SPS seq; | ||
| 123 | 3 | ret = ff_avc_decode_sps(&seq, sps + 3, sps_size - 3); | |
| 124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
| 125 | ✗ | goto fail; | |
| 126 | |||
| 127 | 3 | avio_w8(pb, 0xfc | seq.chroma_format_idc); /* 6 bits reserved (111111) + chroma_format_idc */ | |
| 128 | 3 | avio_w8(pb, 0xf8 | (seq.bit_depth_luma - 8)); /* 5 bits reserved (11111) + bit_depth_luma_minus8 */ | |
| 129 | 3 | avio_w8(pb, 0xf8 | (seq.bit_depth_chroma - 8)); /* 5 bits reserved (11111) + bit_depth_chroma_minus8 */ | |
| 130 | 3 | avio_w8(pb, nb_sps_ext); /* number of sps ext */ | |
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (nb_sps_ext) |
| 132 | ✗ | avio_write(pb, sps_ext, sps_ext_size); | |
| 133 | } | ||
| 134 | |||
| 135 | 1 | fail: | |
| 136 | 4 | ffio_free_dyn_buf(&sps_pb); | |
| 137 | 4 | ffio_free_dyn_buf(&pps_pb); | |
| 138 | 4 | ffio_free_dyn_buf(&sps_ext_pb); | |
| 139 | 4 | av_free(start); | |
| 140 | |||
| 141 | 4 | return ret; | |
| 142 | } | ||
| 143 | |||
| 144 | ✗ | int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size) | |
| 145 | { | ||
| 146 | uint16_t sps_size, pps_size; | ||
| 147 | uint8_t *out; | ||
| 148 | int out_size; | ||
| 149 | |||
| 150 | ✗ | *buf = NULL; | |
| 151 | ✗ | if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001)) | |
| 152 | ✗ | return 0; | |
| 153 | ✗ | if (*size < 11 || in[0] != 1) | |
| 154 | ✗ | return AVERROR_INVALIDDATA; | |
| 155 | |||
| 156 | ✗ | sps_size = AV_RB16(&in[6]); | |
| 157 | ✗ | if (11 + sps_size > *size) | |
| 158 | ✗ | return AVERROR_INVALIDDATA; | |
| 159 | ✗ | pps_size = AV_RB16(&in[9 + sps_size]); | |
| 160 | ✗ | if (11 + sps_size + pps_size > *size) | |
| 161 | ✗ | return AVERROR_INVALIDDATA; | |
| 162 | ✗ | out_size = 8 + sps_size + pps_size; | |
| 163 | ✗ | out = av_mallocz(out_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 164 | ✗ | if (!out) | |
| 165 | ✗ | return AVERROR(ENOMEM); | |
| 166 | ✗ | AV_WB32(&out[0], 0x00000001); | |
| 167 | ✗ | memcpy(out + 4, &in[8], sps_size); | |
| 168 | ✗ | AV_WB32(&out[4 + sps_size], 0x00000001); | |
| 169 | ✗ | memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size); | |
| 170 | ✗ | *buf = out; | |
| 171 | ✗ | *size = out_size; | |
| 172 | ✗ | return 0; | |
| 173 | } | ||
| 174 | |||
| 175 | static const AVRational avc_sample_aspect_ratio[17] = { | ||
| 176 | { 0, 1 }, | ||
| 177 | { 1, 1 }, | ||
| 178 | { 12, 11 }, | ||
| 179 | { 10, 11 }, | ||
| 180 | { 16, 11 }, | ||
| 181 | { 40, 33 }, | ||
| 182 | { 24, 11 }, | ||
| 183 | { 20, 11 }, | ||
| 184 | { 32, 11 }, | ||
| 185 | { 80, 33 }, | ||
| 186 | { 18, 11 }, | ||
| 187 | { 15, 11 }, | ||
| 188 | { 64, 33 }, | ||
| 189 | { 160, 99 }, | ||
| 190 | { 4, 3 }, | ||
| 191 | { 3, 2 }, | ||
| 192 | { 2, 1 }, | ||
| 193 | }; | ||
| 194 | |||
| 195 | 38 | static inline int get_ue_golomb(GetBitContext *gb) { | |
| 196 | int i; | ||
| 197 |
3/4✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 38 times.
|
87 | for (i = 0; i < 32 && !get_bits1(gb); i++) |
| 198 | ; | ||
| 199 | 38 | return get_bitsz(gb, i) + (1 << i) - 1; | |
| 200 | } | ||
| 201 | |||
| 202 | ✗ | static inline int get_se_golomb(GetBitContext *gb) { | |
| 203 | ✗ | int v = get_ue_golomb(gb) + 1; | |
| 204 | ✗ | int sign = -(v & 1); | |
| 205 | ✗ | return ((v >> 1) ^ sign) - sign; | |
| 206 | } | ||
| 207 | |||
| 208 | 3 | int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size) | |
| 209 | { | ||
| 210 | int i, j, ret, rbsp_size, aspect_ratio_idc, pic_order_cnt_type; | ||
| 211 | int num_ref_frames_in_pic_order_cnt_cycle; | ||
| 212 | 3 | int delta_scale, lastScale = 8, nextScale = 8; | |
| 213 | int sizeOfScalingList; | ||
| 214 | GetBitContext gb; | ||
| 215 | uint8_t *rbsp_buf; | ||
| 216 | |||
| 217 | 3 | rbsp_buf = ff_nal_unit_extract_rbsp(buf, buf_size, &rbsp_size, 0); | |
| 218 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!rbsp_buf) |
| 219 | ✗ | return AVERROR(ENOMEM); | |
| 220 | |||
| 221 | 3 | ret = init_get_bits8(&gb, rbsp_buf, rbsp_size); | |
| 222 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
| 223 | ✗ | goto end; | |
| 224 | |||
| 225 | 3 | memset(sps, 0, sizeof(*sps)); | |
| 226 | |||
| 227 | 3 | sps->profile_idc = get_bits(&gb, 8); | |
| 228 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 0; // constraint_set0_flag | |
| 229 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 1; // constraint_set1_flag | |
| 230 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 2; // constraint_set2_flag | |
| 231 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 3; // constraint_set3_flag | |
| 232 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 4; // constraint_set4_flag | |
| 233 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 5; // constraint_set5_flag | |
| 234 | 3 | skip_bits(&gb, 2); // reserved_zero_2bits | |
| 235 | 3 | sps->level_idc = get_bits(&gb, 8); | |
| 236 | 3 | sps->id = get_ue_golomb(&gb); | |
| 237 | |||
| 238 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
3 | if (sps->profile_idc == 100 || sps->profile_idc == 110 || |
| 239 | ✗ | sps->profile_idc == 122 || sps->profile_idc == 244 || sps->profile_idc == 44 || | |
| 240 | ✗ | sps->profile_idc == 83 || sps->profile_idc == 86 || sps->profile_idc == 118 || | |
| 241 | ✗ | sps->profile_idc == 128 || sps->profile_idc == 138 || sps->profile_idc == 139 || | |
| 242 | ✗ | sps->profile_idc == 134) { | |
| 243 | 3 | sps->chroma_format_idc = get_ue_golomb(&gb); // chroma_format_idc | |
| 244 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (sps->chroma_format_idc == 3) { |
| 245 | ✗ | skip_bits1(&gb); // separate_colour_plane_flag | |
| 246 | } | ||
| 247 | 3 | sps->bit_depth_luma = get_ue_golomb(&gb) + 8; | |
| 248 | 3 | sps->bit_depth_chroma = get_ue_golomb(&gb) + 8; | |
| 249 | 3 | skip_bits1(&gb); // qpprime_y_zero_transform_bypass_flag | |
| 250 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (get_bits1(&gb)) { // seq_scaling_matrix_present_flag |
| 251 | ✗ | for (i = 0; i < ((sps->chroma_format_idc != 3) ? 8 : 12); i++) { | |
| 252 | ✗ | if (!get_bits1(&gb)) // seq_scaling_list_present_flag | |
| 253 | ✗ | continue; | |
| 254 | ✗ | lastScale = 8; | |
| 255 | ✗ | nextScale = 8; | |
| 256 | ✗ | sizeOfScalingList = i < 6 ? 16 : 64; | |
| 257 | ✗ | for (j = 0; j < sizeOfScalingList; j++) { | |
| 258 | ✗ | if (nextScale != 0) { | |
| 259 | ✗ | delta_scale = get_se_golomb(&gb); | |
| 260 | ✗ | nextScale = (lastScale + delta_scale) & 0xff; | |
| 261 | } | ||
| 262 | ✗ | lastScale = nextScale == 0 ? lastScale : nextScale; | |
| 263 | } | ||
| 264 | } | ||
| 265 | } | ||
| 266 | } else { | ||
| 267 | ✗ | sps->chroma_format_idc = 1; | |
| 268 | ✗ | sps->bit_depth_luma = 8; | |
| 269 | ✗ | sps->bit_depth_chroma = 8; | |
| 270 | } | ||
| 271 | |||
| 272 | 3 | get_ue_golomb(&gb); // log2_max_frame_num_minus4 | |
| 273 | 3 | pic_order_cnt_type = get_ue_golomb(&gb); | |
| 274 | |||
| 275 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (pic_order_cnt_type == 0) { |
| 276 | 3 | get_ue_golomb(&gb); // log2_max_pic_order_cnt_lsb_minus4 | |
| 277 | ✗ | } else if (pic_order_cnt_type == 1) { | |
| 278 | ✗ | skip_bits1(&gb); // delta_pic_order_always_zero | |
| 279 | ✗ | get_se_golomb(&gb); // offset_for_non_ref_pic | |
| 280 | ✗ | get_se_golomb(&gb); // offset_for_top_to_bottom_field | |
| 281 | ✗ | num_ref_frames_in_pic_order_cnt_cycle = get_ue_golomb(&gb); | |
| 282 | ✗ | for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) | |
| 283 | ✗ | get_se_golomb(&gb); // offset_for_ref_frame | |
| 284 | } | ||
| 285 | |||
| 286 | 3 | get_ue_golomb(&gb); // max_num_ref_frames | |
| 287 | 3 | skip_bits1(&gb); // gaps_in_frame_num_value_allowed_flag | |
| 288 | 3 | get_ue_golomb(&gb); // pic_width_in_mbs_minus1 | |
| 289 | 3 | get_ue_golomb(&gb); // pic_height_in_map_units_minus1 | |
| 290 | |||
| 291 | 3 | sps->frame_mbs_only_flag = get_bits1(&gb); | |
| 292 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (!sps->frame_mbs_only_flag) |
| 293 | 2 | skip_bits1(&gb); // mb_adaptive_frame_field_flag | |
| 294 | |||
| 295 | 3 | skip_bits1(&gb); // direct_8x8_inference_flag | |
| 296 | |||
| 297 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if (get_bits1(&gb)) { // frame_cropping_flag |
| 298 | 2 | get_ue_golomb(&gb); // frame_crop_left_offset | |
| 299 | 2 | get_ue_golomb(&gb); // frame_crop_right_offset | |
| 300 | 2 | get_ue_golomb(&gb); // frame_crop_top_offset | |
| 301 | 2 | get_ue_golomb(&gb); // frame_crop_bottom_offset | |
| 302 | } | ||
| 303 | |||
| 304 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if (get_bits1(&gb)) { // vui_parameters_present_flag |
| 305 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if (get_bits1(&gb)) { // aspect_ratio_info_present_flag |
| 306 | 2 | aspect_ratio_idc = get_bits(&gb, 8); | |
| 307 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (aspect_ratio_idc == 0xff) { |
| 308 | ✗ | sps->sar.num = get_bits(&gb, 16); | |
| 309 | ✗ | sps->sar.den = get_bits(&gb, 16); | |
| 310 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(avc_sample_aspect_ratio)) { |
| 311 | 2 | sps->sar = avc_sample_aspect_ratio[aspect_ratio_idc]; | |
| 312 | } | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (!sps->sar.den) { |
| 317 | 1 | sps->sar.num = 1; | |
| 318 | 1 | sps->sar.den = 1; | |
| 319 | } | ||
| 320 | |||
| 321 | 3 | ret = 0; | |
| 322 | 3 | end: | |
| 323 | 3 | av_free(rbsp_buf); | |
| 324 | 3 | return ret; | |
| 325 | } | ||
| 326 |