FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dnxuc_parser.c
Date: 2024-10-27 21:33:06
Exec Total Coverage
Lines: 0 31 0.0%
Functions: 0 1 0.0%
Branches: 0 18 0.0%

Line Branch Exec Source
1 /*
2 * Avid DNxUncomressed / SMPTE RDD 50 parser
3 * Copyright (c) 2024 Martin Schitter
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 /*
23 This parser for DNxUncompressed video data is mostly based on
24 reverse engineering of output generated by DaVinci Resolve 19
25 but was later also checked against the SMPTE RDD 50 specification.
26
27 Limitations: Multiple image planes are not supported.
28 */
29
30 #include "avcodec.h"
31 #include "libavutil/intreadwrite.h"
32
33 typedef struct DNxUcParseContext {
34 uint32_t fourcc_tag;
35 uint32_t width;
36 uint32_t height;
37 uint32_t nr_bytes;
38 } DNxUcParseContext;
39
40 /*
41 DNxUncompressed frame data comes wrapped in nested boxes of metadata
42 (box structure: len + fourcc marker + data):
43
44 [0-4] len of outer essence unit box (typically 37 bytes of header + frame data)
45 [4-7] fourcc 'pack'
46
47 [8-11] len of "signal info box" (always 21)
48 [12-15] fourcc 'sinf'
49 [16-19] frame width / line packing size
50 [20-23] frame hight / nr of lines
51 [24-27] fourcc pixel format indicator
52 [28] frame_layout (0: progressive, 1: interlaced)
53
54 [29-32] len of "signal data box" (nr of frame data bytes + 8)
55 [33-36] fourcc 'sdat'
56 [37-..] frame data
57
58 A sequence of 'signal info'+'signal data' box pairs wrapped in
59 'icmp'(=image component) boxes can be utilized to compose more
60 complex multi plane images.
61 This feature is only partially supported in the present implementation.
62 We never pick more than the first pair of info and image data enclosed
63 in this way.
64 */
65
66 static int dnxuc_parse(AVCodecParserContext *s,
67 AVCodecContext *avctx,
68 const uint8_t **poutbuf, int *poutbuf_size,
69 const uint8_t *buf, int buf_size)
70 {
71 const int HEADER_SIZE = 37;
72 int icmp_offset = 0;
73
74 DNxUcParseContext *pc;
75 pc = (DNxUcParseContext *) s->priv_data;
76
77 if (!buf_size) {
78 return 0;
79 }
80 if (buf_size > 16 && MKTAG('i','c','m','p') == AV_RL32(buf+12)){
81 icmp_offset += 8;
82 }
83 if ( buf_size < 37 + icmp_offset /* check metadata structure expectations */
84 || MKTAG('p','a','c','k') != AV_RL32(buf+4+icmp_offset)
85 || MKTAG('s','i','n','f') != AV_RL32(buf+12+icmp_offset)
86 || MKTAG('s','d','a','t') != AV_RL32(buf+33+icmp_offset)){
87 av_log(avctx, AV_LOG_ERROR, "can't read DNxUncompressed metadata.\n");
88 *poutbuf_size = 0;
89 return buf_size;
90 }
91
92 pc->fourcc_tag = AV_RL32(buf+24+icmp_offset);
93 pc->width = AV_RL32(buf+16+icmp_offset);
94 pc->height = AV_RL32(buf+20+icmp_offset);
95 pc->nr_bytes = AV_RL32(buf+29+icmp_offset) - 8;
96
97 if (!avctx->codec_tag) {
98 av_log(avctx, AV_LOG_INFO, "dnxuc_parser: '%s' %dx%d %dbpp %d\n",
99 av_fourcc2str(pc->fourcc_tag),
100 pc->width, pc->height,
101 (pc->nr_bytes*8)/(pc->width*pc->height),
102 pc->nr_bytes);
103 avctx->codec_tag = pc->fourcc_tag;
104 }
105
106 if (pc->nr_bytes > buf_size - HEADER_SIZE + icmp_offset){
107 av_log(avctx, AV_LOG_ERROR, "Insufficient size of image essence data.\n");
108 *poutbuf_size = 0;
109 return buf_size;
110 }
111
112 *poutbuf = buf + HEADER_SIZE + icmp_offset;
113 *poutbuf_size = pc->nr_bytes;
114
115 return buf_size;
116 }
117
118 const AVCodecParser ff_dnxuc_parser = {
119 .codec_ids = { AV_CODEC_ID_DNXUC },
120 .priv_data_size = sizeof(DNxUcParseContext),
121 .parser_parse = dnxuc_parse,
122 };
123