| @@ -0,0 +1,133 @@ |
| 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 "avcodec.h" |
| 20 | +#include "apv.h" |
| 21 | +#include "cbs.h" |
| 22 | +#include "cbs_apv.h" |
| 23 | + |
| 24 | +typedef struct APVParseContext { |
| 25 | + CodedBitstreamContext *cbc; |
| 26 | + CodedBitstreamFragment au; |
| 27 | +} APVParseContext; |
| 28 | + |
| 29 | +static const enum AVPixelFormat apv_format_table[5][5] = { |
| 30 | + { AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16 }, |
| 31 | + { 0 }, // 4:2:0 is not valid. |
| 32 | + { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV422P16 }, |
| 33 | + { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV444P16 }, |
| 34 | + { AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUVA444P16 }, |
| 35 | +}; |
| 36 | + |
| 37 | +static int parse(AVCodecParserContext *s, |
| 38 | + AVCodecContext *avctx, |
| 39 | + const uint8_t **poutbuf, int *poutbuf_size, |
| 40 | + const uint8_t *buf, int buf_size) |
| 41 | +{ |
| 42 | + APVParseContext *p = s->priv_data; |
| 43 | + CodedBitstreamFragment *au = &p->au; |
| 44 | + int ret; |
| 45 | + |
| 46 | + *poutbuf = buf; |
| 47 | + *poutbuf_size = buf_size; |
| 48 | + |
| 49 | + p->cbc->log_ctx = avctx; |
| 50 | + |
| 51 | + ret = ff_cbs_read(p->cbc, au, buf, buf_size); |
| 52 | + if (ret < 0) { |
| 53 | + av_log(avctx, AV_LOG_ERROR, "Failed to parse access unit.\n"); |
| 54 | + goto end; |
| 55 | + } |
| 56 | + |
| 57 | + s->key_frame = 1; |
| 58 | + s->pict_type = AV_PICTURE_TYPE_I; |
| 59 | + s->field_order = AV_FIELD_UNKNOWN; |
| 60 | + s->picture_structure = AV_PICTURE_STRUCTURE_FRAME; |
| 61 | + |
| 62 | + for (int i = 0; i < au->nb_units; i++) { |
| 63 | + const CodedBitstreamUnit *pbu = &au->units[i]; |
| 64 | + |
| 65 | + switch (pbu->type) { |
| 66 | + case APV_PBU_PRIMARY_FRAME: { |
| 67 | + const APVRawFrame *frame = pbu->content; |
| 68 | + const APVRawFrameHeader *header = &frame->frame_header; |
| 69 | + const APVRawFrameInfo *info = &header->frame_info; |
| 70 | + int bit_depth = info->bit_depth_minus8 + 8; |
| 71 | + |
| 72 | + if (bit_depth < 8 || bit_depth > 16 || bit_depth % 2) |
| 73 | + break; |
| 74 | + |
| 75 | + s->width = info->frame_width; |
| 76 | + s->height = info->frame_height; |
| 77 | + s->format = apv_format_table[info->chroma_format_idc][bit_depth - 4 >> 2]; |
| 78 | + avctx->profile = info->profile_idc; |
| 79 | + avctx->level = info->level_idc; |
| 80 | + avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT; |
| 81 | + avctx->color_primaries = header->color_primaries; |
| 82 | + avctx->color_trc = header->transfer_characteristics; |
| 83 | + avctx->colorspace = header->matrix_coefficients; |
| 84 | + avctx->color_range = header->full_range_flag ? AVCOL_RANGE_JPEG |
| 85 | + : AVCOL_RANGE_MPEG; |
| 86 | + goto end; |
| 87 | + } |
| 88 | + default: |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +end: |
| 94 | + ff_cbs_fragment_reset(au); |
| 95 | + p->cbc->log_ctx = NULL; |
| 96 | + |
| 97 | + return buf_size; |
| 98 | +} |
| 99 | + |
| 100 | +static const CodedBitstreamUnitType decompose_unit_types[] = { |
| 101 | + APV_PBU_PRIMARY_FRAME, |
| 102 | +}; |
| 103 | + |
| 104 | +static av_cold int init(AVCodecParserContext *s) |
| 105 | +{ |
| 106 | + APVParseContext *p = s->priv_data; |
| 107 | + int ret; |
| 108 | + |
| 109 | + ret = ff_cbs_init(&p->cbc, AV_CODEC_ID_APV, NULL); |
| 110 | + if (ret < 0) |
| 111 | + return ret; |
| 112 | + |
| 113 | + p->cbc->decompose_unit_types = decompose_unit_types; |
| 114 | + p->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types); |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
| 118 | + |
| 119 | +static void close(AVCodecParserContext *s) |
| 120 | +{ |
| 121 | + APVParseContext *p = s->priv_data; |
| 122 | + |
| 123 | + ff_cbs_fragment_free(&p->au); |
| 124 | + ff_cbs_close(&p->cbc); |
| 125 | +} |
| 126 | + |
| 127 | +const AVCodecParser ff_apv_parser = { |
| 128 | + .codec_ids = { AV_CODEC_ID_APV }, |
| 129 | + .priv_data_size = sizeof(APVParseContext), |
| 130 | + .parser_init = init, |
| 131 | + .parser_parse = parse, |
| 132 | + .parser_close = close, |
| 133 | +}; |