| 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/mem.h" | ||
| 22 | |||
| 23 | #include "avcodec.h" | ||
| 24 | #include "bytestream.h" | ||
| 25 | #include "get_bits.h" | ||
| 26 | #include "h2645_parse.h" | ||
| 27 | #include "lcevc.h" | ||
| 28 | #include "lcevc_parse.h" | ||
| 29 | #include "lcevctab.h" | ||
| 30 | #include "parser.h" | ||
| 31 | #include "parser_internal.h" | ||
| 32 | |||
| 33 | #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes | ||
| 34 | |||
| 35 | typedef struct LCEVCParserContext { | ||
| 36 | ParseContext pc; | ||
| 37 | |||
| 38 | H2645Packet pkt; | ||
| 39 | |||
| 40 | int parsed_extradata; | ||
| 41 | int is_lvcc; | ||
| 42 | int nal_length_size; | ||
| 43 | } LCEVCParserContext; | ||
| 44 | |||
| 45 | ✗ | static int lcevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf, | |
| 46 | int buf_size) | ||
| 47 | { | ||
| 48 | ✗ | LCEVCParserContext *ctx = s->priv_data; | |
| 49 | ✗ | ParseContext *pc = &ctx->pc; | |
| 50 | |||
| 51 | ✗ | for (int i = 0; i < buf_size; i++) { | |
| 52 | int nut; | ||
| 53 | |||
| 54 | ✗ | pc->state = (pc->state << 8) | buf[i]; | |
| 55 | |||
| 56 | ✗ | if (((pc->state >> 8) & 0xFFFFFF) != START_CODE) | |
| 57 | ✗ | continue; | |
| 58 | |||
| 59 | ✗ | nut = (pc->state >> 1) & 0x1F; | |
| 60 | |||
| 61 | // Beginning of access unit | ||
| 62 | ✗ | if (nut == LCEVC_IDR_NUT || nut == LCEVC_NON_IDR_NUT) { | |
| 63 | ✗ | if (!pc->frame_start_found) | |
| 64 | ✗ | pc->frame_start_found = 1; | |
| 65 | else { | ||
| 66 | ✗ | pc->frame_start_found = 0; | |
| 67 | ✗ | return i - 3; | |
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | ✗ | return END_NOT_FOUND; | |
| 73 | } | ||
| 74 | |||
| 75 | ✗ | static int parse_nal_unit(AVCodecParserContext *s, AVCodecContext *avctx, | |
| 76 | const H2645NAL *nal) | ||
| 77 | { | ||
| 78 | GetByteContext gbc; | ||
| 79 | ✗ | bytestream2_init(&gbc, nal->data, nal->size); | |
| 80 | ✗ | bytestream2_skip(&gbc, 2); | |
| 81 | |||
| 82 | ✗ | while (bytestream2_get_bytes_left(&gbc) > 1) { | |
| 83 | GetBitContext gb; | ||
| 84 | uint64_t payload_size; | ||
| 85 | int payload_size_type, payload_type; | ||
| 86 | int block_size; | ||
| 87 | |||
| 88 | ✗ | int ret = init_get_bits8(&gb, gbc.buffer, bytestream2_get_bytes_left(&gbc)); | |
| 89 | ✗ | if (ret < 0) | |
| 90 | ✗ | return ret; | |
| 91 | |||
| 92 | ✗ | payload_size_type = get_bits(&gb, 3); | |
| 93 | ✗ | payload_type = get_bits(&gb, 5); | |
| 94 | ✗ | payload_size = payload_size_type; | |
| 95 | ✗ | if (payload_size_type == 6) | |
| 96 | ✗ | return AVERROR_PATCHWELCOME; | |
| 97 | ✗ | if (payload_size_type == 7) | |
| 98 | ✗ | payload_size = get_mb(&gb); | |
| 99 | |||
| 100 | ✗ | if (payload_size > INT_MAX - (get_bits_count(&gb) >> 3)) | |
| 101 | ✗ | return AVERROR_INVALIDDATA; | |
| 102 | |||
| 103 | ✗ | block_size = payload_size + (get_bits_count(&gb) >> 3); | |
| 104 | ✗ | if (block_size >= bytestream2_get_bytes_left(&gbc)) | |
| 105 | ✗ | return AVERROR_INVALIDDATA; | |
| 106 | |||
| 107 | ✗ | switch (payload_type) { | |
| 108 | ✗ | case LCEVC_PAYLOAD_TYPE_SEQUENCE_CONFIG: | |
| 109 | ✗ | avctx->profile = get_bits(&gb, 4); | |
| 110 | ✗ | avctx->level = get_bits(&gb, 4); | |
| 111 | ✗ | break; | |
| 112 | ✗ | case LCEVC_PAYLOAD_TYPE_GLOBAL_CONFIG: { | |
| 113 | int resolution_type, chroma_format_idc, bit_depth; | ||
| 114 | int processed_planes_type_flag; | ||
| 115 | |||
| 116 | ✗ | processed_planes_type_flag = get_bits1(&gb); | |
| 117 | ✗ | resolution_type = get_bits(&gb, 6); | |
| 118 | ✗ | skip_bits1(&gb); | |
| 119 | ✗ | chroma_format_idc = get_bits(&gb, 2); | |
| 120 | ✗ | skip_bits(&gb, 2); | |
| 121 | ✗ | bit_depth = get_bits(&gb, 2); // enhancement_depth_type | |
| 122 | |||
| 123 | ✗ | s->format = ff_lcevc_depth_type[bit_depth][chroma_format_idc]; | |
| 124 | |||
| 125 | ✗ | if (resolution_type < 63) { | |
| 126 | ✗ | s->width = ff_lcevc_resolution_type[resolution_type].width; | |
| 127 | ✗ | s->height = ff_lcevc_resolution_type[resolution_type].height; | |
| 128 | } else { | ||
| 129 | int upsample_type, tile_dimensions_type; | ||
| 130 | int temporal_step_width_modifier_signalled_flag, level1_filtering_signalled_flag; | ||
| 131 | // Skip syntax elements until we get to the custom dimension ones | ||
| 132 | ✗ | temporal_step_width_modifier_signalled_flag = get_bits1(&gb); | |
| 133 | ✗ | skip_bits(&gb, 3); | |
| 134 | ✗ | upsample_type = get_bits(&gb, 3); | |
| 135 | ✗ | level1_filtering_signalled_flag = get_bits1(&gb); | |
| 136 | ✗ | skip_bits(&gb, 4); | |
| 137 | ✗ | tile_dimensions_type = get_bits(&gb, 2); | |
| 138 | ✗ | skip_bits(&gb, 4); | |
| 139 | ✗ | if (processed_planes_type_flag) | |
| 140 | ✗ | skip_bits(&gb, 4); | |
| 141 | ✗ | if (temporal_step_width_modifier_signalled_flag) | |
| 142 | ✗ | skip_bits(&gb, 8); | |
| 143 | ✗ | if (upsample_type) | |
| 144 | ✗ | skip_bits_long(&gb, 64); | |
| 145 | ✗ | if (level1_filtering_signalled_flag) | |
| 146 | ✗ | skip_bits(&gb, 8); | |
| 147 | ✗ | if (tile_dimensions_type) { | |
| 148 | ✗ | if (tile_dimensions_type == 3) | |
| 149 | ✗ | skip_bits_long(&gb, 32); | |
| 150 | ✗ | skip_bits(&gb, 8); | |
| 151 | } | ||
| 152 | |||
| 153 | ✗ | s->width = get_bits(&gb, 16); | |
| 154 | ✗ | s->height = get_bits(&gb, 16); | |
| 155 | } | ||
| 156 | ✗ | break; | |
| 157 | } | ||
| 158 | ✗ | default: | |
| 159 | ✗ | break; | |
| 160 | } | ||
| 161 | |||
| 162 | ✗ | bytestream2_skip(&gbc, block_size); | |
| 163 | } | ||
| 164 | |||
| 165 | ✗ | return 0; | |
| 166 | } | ||
| 167 | |||
| 168 | ✗ | static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, | |
| 169 | int buf_size, AVCodecContext *avctx) | ||
| 170 | { | ||
| 171 | ✗ | LCEVCParserContext *ctx = s->priv_data; | |
| 172 | ✗ | int flags = (H2645_FLAG_IS_NALFF * !!ctx->is_lvcc) | H2645_FLAG_SMALL_PADDING; | |
| 173 | int ret, i; | ||
| 174 | |||
| 175 | /* set some sane default values */ | ||
| 176 | ✗ | s->pict_type = AV_PICTURE_TYPE_NONE; | |
| 177 | ✗ | s->key_frame = 0; | |
| 178 | ✗ | s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN; | |
| 179 | |||
| 180 | ✗ | ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, | |
| 181 | ctx->nal_length_size, AV_CODEC_ID_LCEVC, flags); | ||
| 182 | ✗ | if (ret < 0) | |
| 183 | ✗ | return ret; | |
| 184 | |||
| 185 | ✗ | for (i = 0; i < ctx->pkt.nb_nals; i++) { | |
| 186 | ✗ | H2645NAL *nal = &ctx->pkt.nals[i]; | |
| 187 | |||
| 188 | ✗ | switch (nal->type) { | |
| 189 | ✗ | case LCEVC_IDR_NUT: | |
| 190 | ✗ | s->key_frame = 1; | |
| 191 | // fall-through | ||
| 192 | ✗ | case LCEVC_NON_IDR_NUT: | |
| 193 | ✗ | parse_nal_unit(s, avctx, nal); | |
| 194 | ✗ | break; | |
| 195 | ✗ | default: | |
| 196 | ✗ | break; | |
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | ✗ | return 0; | |
| 201 | } | ||
| 202 | |||
| 203 | ✗ | static int lcevc_parse(AVCodecParserContext *s, | |
| 204 | AVCodecContext *avctx, | ||
| 205 | const uint8_t **poutbuf, int *poutbuf_size, | ||
| 206 | const uint8_t *buf, int buf_size) | ||
| 207 | { | ||
| 208 | ✗ | LCEVCParserContext *ctx = s->priv_data; | |
| 209 | ✗ | ParseContext *pc = &ctx->pc; | |
| 210 | int next; | ||
| 211 | |||
| 212 | ✗ | if (!ctx->parsed_extradata && avctx->extradata_size > 4) { | |
| 213 | ✗ | ctx->parsed_extradata = 1; | |
| 214 | ✗ | ctx->is_lvcc = !!avctx->extradata[0]; | |
| 215 | |||
| 216 | ✗ | if (ctx->is_lvcc) | |
| 217 | ✗ | ctx->nal_length_size = (avctx->extradata[4] >> 6) + 1; | |
| 218 | } | ||
| 219 | |||
| 220 | ✗ | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { | |
| 221 | ✗ | next = buf_size; | |
| 222 | } else { | ||
| 223 | ✗ | next = lcevc_find_frame_end(s, buf, buf_size); | |
| 224 | ✗ | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { | |
| 225 | ✗ | *poutbuf = NULL; | |
| 226 | ✗ | *poutbuf_size = 0; | |
| 227 | ✗ | return buf_size; | |
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | ✗ | parse_nal_units(s, buf, buf_size, avctx); | |
| 232 | |||
| 233 | ✗ | *poutbuf = buf; | |
| 234 | ✗ | *poutbuf_size = buf_size; | |
| 235 | ✗ | return next; | |
| 236 | } | ||
| 237 | |||
| 238 | ✗ | static void lcevc_parser_close(AVCodecParserContext *s) | |
| 239 | { | ||
| 240 | ✗ | LCEVCParserContext *ctx = s->priv_data; | |
| 241 | |||
| 242 | ✗ | ff_h2645_packet_uninit(&ctx->pkt); | |
| 243 | |||
| 244 | ✗ | av_freep(&ctx->pc.buffer); | |
| 245 | ✗ | } | |
| 246 | |||
| 247 | const FFCodecParser ff_lcevc_parser = { | ||
| 248 | PARSER_CODEC_LIST(AV_CODEC_ID_LCEVC), | ||
| 249 | .priv_data_size = sizeof(LCEVCParserContext), | ||
| 250 | .parse = lcevc_parse, | ||
| 251 | .close = lcevc_parser_close, | ||
| 252 | }; | ||
| 253 |