| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * FFmpeg is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with FFmpeg; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include <stdint.h> | ||
| 20 | |||
| 21 | #include "libavutil/attributes.h" | ||
| 22 | #include "libavutil/mem.h" | ||
| 23 | |||
| 24 | #include "avcodec.h" | ||
| 25 | #include "bytestream.h" | ||
| 26 | #include "get_bits.h" | ||
| 27 | #include "h2645_parse.h" | ||
| 28 | #include "lcevc.h" | ||
| 29 | #include "lcevc_parse.h" | ||
| 30 | #include "lcevctab.h" | ||
| 31 | #include "parser.h" | ||
| 32 | #include "parser_internal.h" | ||
| 33 | |||
| 34 | #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes | ||
| 35 | |||
| 36 | typedef struct LCEVCParserContext { | ||
| 37 | ParseContext pc; | ||
| 38 | |||
| 39 | H2645Packet pkt; | ||
| 40 | |||
| 41 | int parsed_extradata; | ||
| 42 | int is_lvcc; | ||
| 43 | int nal_length_size; | ||
| 44 | } LCEVCParserContext; | ||
| 45 | |||
| 46 | 549 | static int lcevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf, | |
| 47 | int buf_size) | ||
| 48 | { | ||
| 49 | 549 | LCEVCParserContext *ctx = s->priv_data; | |
| 50 | 549 | ParseContext *pc = &ctx->pc; | |
| 51 | |||
| 52 |
2/2✓ Branch 0 taken 159402 times.
✓ Branch 1 taken 288 times.
|
159690 | for (int i = 0; i < buf_size; i++) { |
| 53 | int nut; | ||
| 54 | |||
| 55 | 159402 | pc->state = (pc->state << 8) | buf[i]; | |
| 56 | |||
| 57 |
2/2✓ Branch 0 taken 158871 times.
✓ Branch 1 taken 531 times.
|
159402 | if (((pc->state >> 8) & 0xFFFFFF) != START_CODE) |
| 58 | 158871 | continue; | |
| 59 | |||
| 60 | 531 | nut = (pc->state >> 1) & 0x1F; | |
| 61 | |||
| 62 | // Beginning of access unit | ||
| 63 |
3/4✓ Branch 0 taken 522 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 522 times.
✗ Branch 3 not taken.
|
531 | if (nut == LCEVC_IDR_NUT || nut == LCEVC_NON_IDR_NUT) { |
| 64 |
2/2✓ Branch 0 taken 270 times.
✓ Branch 1 taken 261 times.
|
531 | if (!pc->frame_start_found) |
| 65 | 270 | pc->frame_start_found = 1; | |
| 66 | else { | ||
| 67 | 261 | pc->frame_start_found = 0; | |
| 68 | 261 | return i - 3; | |
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | 288 | return END_NOT_FOUND; | |
| 74 | } | ||
| 75 | |||
| 76 | 270 | static int parse_nal_unit(AVCodecParserContext *s, AVCodecContext *avctx, | |
| 77 | const H2645NAL *nal) | ||
| 78 | { | ||
| 79 | GetByteContext gbc; | ||
| 80 | 270 | bytestream2_init(&gbc, nal->data, nal->size); | |
| 81 | 270 | bytestream2_skip(&gbc, 2); | |
| 82 | |||
| 83 |
2/2✓ Branch 1 taken 576 times.
✓ Branch 2 taken 270 times.
|
846 | while (bytestream2_get_bytes_left(&gbc) > 1) { |
| 84 | GetBitContext gb; | ||
| 85 | uint64_t payload_size; | ||
| 86 | int payload_size_type, payload_type; | ||
| 87 | int block_size; | ||
| 88 | |||
| 89 | 576 | int ret = init_get_bits8(&gb, gbc.buffer, bytestream2_get_bytes_left(&gbc)); | |
| 90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 576 times.
|
576 | if (ret < 0) |
| 91 | ✗ | return ret; | |
| 92 | |||
| 93 | 576 | payload_size_type = get_bits(&gb, 3); | |
| 94 | 576 | payload_type = get_bits(&gb, 5); | |
| 95 | 576 | payload_size = payload_size_type; | |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 576 times.
|
576 | if (payload_size_type == 6) |
| 97 | ✗ | return AVERROR_PATCHWELCOME; | |
| 98 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 288 times.
|
576 | if (payload_size_type == 7) |
| 99 | 288 | payload_size = get_mb(&gb); | |
| 100 | |||
| 101 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 576 times.
|
576 | if (payload_size > INT_MAX - (get_bits_count(&gb) >> 3)) |
| 102 | ✗ | return AVERROR_INVALIDDATA; | |
| 103 | |||
| 104 | 576 | block_size = payload_size + (get_bits_count(&gb) >> 3); | |
| 105 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 576 times.
|
576 | if (block_size >= bytestream2_get_bytes_left(&gbc)) |
| 106 | ✗ | return AVERROR_INVALIDDATA; | |
| 107 | |||
| 108 |
3/3✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 558 times.
|
576 | switch (payload_type) { |
| 109 | 9 | case LCEVC_PAYLOAD_TYPE_SEQUENCE_CONFIG: | |
| 110 | 9 | avctx->profile = get_bits(&gb, 4); | |
| 111 | 9 | avctx->level = get_bits(&gb, 4); | |
| 112 | 9 | break; | |
| 113 | 9 | case LCEVC_PAYLOAD_TYPE_GLOBAL_CONFIG: { | |
| 114 | int resolution_type, chroma_format_idc, bit_depth; | ||
| 115 | int processed_planes_type_flag; | ||
| 116 | |||
| 117 | 9 | processed_planes_type_flag = get_bits1(&gb); | |
| 118 | 9 | resolution_type = get_bits(&gb, 6); | |
| 119 | 9 | skip_bits1(&gb); | |
| 120 | 9 | chroma_format_idc = get_bits(&gb, 2); | |
| 121 | 9 | skip_bits(&gb, 2); | |
| 122 | 9 | bit_depth = get_bits(&gb, 2); // enhancement_depth_type | |
| 123 | |||
| 124 | 9 | s->format = ff_lcevc_depth_type[bit_depth][chroma_format_idc]; | |
| 125 | |||
| 126 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | if (resolution_type < 63) { |
| 127 | 9 | s->width = ff_lcevc_resolution_type[resolution_type].width; | |
| 128 | 9 | s->height = ff_lcevc_resolution_type[resolution_type].height; | |
| 129 | } else { | ||
| 130 | int upsample_type, tile_dimensions_type; | ||
| 131 | int temporal_step_width_modifier_signalled_flag, level1_filtering_signalled_flag; | ||
| 132 | // Skip syntax elements until we get to the custom dimension ones | ||
| 133 | ✗ | temporal_step_width_modifier_signalled_flag = get_bits1(&gb); | |
| 134 | ✗ | skip_bits(&gb, 3); | |
| 135 | ✗ | upsample_type = get_bits(&gb, 3); | |
| 136 | ✗ | level1_filtering_signalled_flag = get_bits1(&gb); | |
| 137 | ✗ | skip_bits(&gb, 4); | |
| 138 | ✗ | tile_dimensions_type = get_bits(&gb, 2); | |
| 139 | ✗ | skip_bits(&gb, 4); | |
| 140 | ✗ | if (processed_planes_type_flag) | |
| 141 | ✗ | skip_bits(&gb, 4); | |
| 142 | ✗ | if (temporal_step_width_modifier_signalled_flag) | |
| 143 | ✗ | skip_bits(&gb, 8); | |
| 144 | ✗ | if (upsample_type) | |
| 145 | ✗ | skip_bits_long(&gb, 64); | |
| 146 | ✗ | if (level1_filtering_signalled_flag) | |
| 147 | ✗ | skip_bits(&gb, 8); | |
| 148 | ✗ | if (tile_dimensions_type) { | |
| 149 | ✗ | if (tile_dimensions_type == 3) | |
| 150 | ✗ | skip_bits_long(&gb, 32); | |
| 151 | ✗ | skip_bits(&gb, 8); | |
| 152 | } | ||
| 153 | |||
| 154 | ✗ | s->width = get_bits(&gb, 16); | |
| 155 | ✗ | s->height = get_bits(&gb, 16); | |
| 156 | } | ||
| 157 | 9 | break; | |
| 158 | } | ||
| 159 | 558 | default: | |
| 160 | 558 | break; | |
| 161 | } | ||
| 162 | |||
| 163 | 576 | bytestream2_skip(&gbc, block_size); | |
| 164 | } | ||
| 165 | |||
| 166 | 270 | return 0; | |
| 167 | } | ||
| 168 | |||
| 169 | 279 | static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, | |
| 170 | int buf_size, AVCodecContext *avctx) | ||
| 171 | { | ||
| 172 | 279 | LCEVCParserContext *ctx = s->priv_data; | |
| 173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
|
279 | int flags = (H2645_FLAG_IS_NALFF * !!ctx->is_lvcc) | H2645_FLAG_SMALL_PADDING; |
| 174 | int ret, i; | ||
| 175 | |||
| 176 | /* set some sane default values */ | ||
| 177 | 279 | s->pict_type = AV_PICTURE_TYPE_NONE; | |
| 178 | 279 | s->key_frame = 0; | |
| 179 | 279 | s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN; | |
| 180 | |||
| 181 | 279 | ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, | |
| 182 | ctx->nal_length_size, AV_CODEC_ID_LCEVC, flags); | ||
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
|
279 | if (ret < 0) |
| 184 | ✗ | return ret; | |
| 185 | |||
| 186 |
2/2✓ Branch 0 taken 270 times.
✓ Branch 1 taken 279 times.
|
549 | for (i = 0; i < ctx->pkt.nb_nals; i++) { |
| 187 | 270 | H2645NAL *nal = &ctx->pkt.nals[i]; | |
| 188 | |||
| 189 |
2/3✓ Branch 0 taken 9 times.
✓ Branch 1 taken 261 times.
✗ Branch 2 not taken.
|
270 | switch (nal->type) { |
| 190 | 9 | case LCEVC_IDR_NUT: | |
| 191 | 9 | s->key_frame = 1; | |
| 192 | av_fallthrough; | ||
| 193 | 270 | case LCEVC_NON_IDR_NUT: | |
| 194 | 270 | parse_nal_unit(s, avctx, nal); | |
| 195 | 270 | break; | |
| 196 | ✗ | default: | |
| 197 | ✗ | break; | |
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | 279 | return 0; | |
| 202 | } | ||
| 203 | |||
| 204 | 549 | static int lcevc_parse(AVCodecParserContext *s, | |
| 205 | AVCodecContext *avctx, | ||
| 206 | const uint8_t **poutbuf, int *poutbuf_size, | ||
| 207 | const uint8_t *buf, int buf_size) | ||
| 208 | { | ||
| 209 | 549 | LCEVCParserContext *ctx = s->priv_data; | |
| 210 | 549 | ParseContext *pc = &ctx->pc; | |
| 211 | int next; | ||
| 212 | |||
| 213 |
4/4✓ Branch 0 taken 27 times.
✓ Branch 1 taken 522 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 18 times.
|
549 | if (!ctx->parsed_extradata && avctx->extradata_size > 4) { |
| 214 | 9 | ctx->parsed_extradata = 1; | |
| 215 | 9 | ctx->is_lvcc = !!avctx->extradata[0]; | |
| 216 | |||
| 217 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (ctx->is_lvcc) |
| 218 | ✗ | ctx->nal_length_size = (avctx->extradata[4] >> 6) + 1; | |
| 219 | } | ||
| 220 | |||
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 549 times.
|
549 | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
| 222 | ✗ | next = buf_size; | |
| 223 | } else { | ||
| 224 | 549 | next = lcevc_find_frame_end(s, buf, buf_size); | |
| 225 |
2/2✓ Branch 1 taken 270 times.
✓ Branch 2 taken 279 times.
|
549 | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
| 226 | 270 | *poutbuf = NULL; | |
| 227 | 270 | *poutbuf_size = 0; | |
| 228 | 270 | return buf_size; | |
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | 279 | parse_nal_units(s, buf, buf_size, avctx); | |
| 233 | |||
| 234 | 279 | *poutbuf = buf; | |
| 235 | 279 | *poutbuf_size = buf_size; | |
| 236 | 279 | return next; | |
| 237 | } | ||
| 238 | |||
| 239 | 9 | static void lcevc_parser_close(AVCodecParserContext *s) | |
| 240 | { | ||
| 241 | 9 | LCEVCParserContext *ctx = s->priv_data; | |
| 242 | |||
| 243 | 9 | ff_h2645_packet_uninit(&ctx->pkt); | |
| 244 | |||
| 245 | 9 | av_freep(&ctx->pc.buffer); | |
| 246 | 9 | } | |
| 247 | |||
| 248 | const FFCodecParser ff_lcevc_parser = { | ||
| 249 | PARSER_CODEC_LIST(AV_CODEC_ID_LCEVC), | ||
| 250 | .priv_data_size = sizeof(LCEVCParserContext), | ||
| 251 | .parse = lcevc_parse, | ||
| 252 | .close = lcevc_parser_close, | ||
| 253 | }; | ||
| 254 |