FFmpeg
dnxuc_parser.c
Go to the documentation of this file.
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;
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 
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 
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",
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 
120  .priv_data_size = sizeof(DNxUcParseContext),
121  .parser_parse = dnxuc_parse,
122 };
ff_dnxuc_parser
const AVCodecParser ff_dnxuc_parser
Definition: dnxuc_parser.c:118
dnxuc_parse
static int dnxuc_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: dnxuc_parser.c:66
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
if
if(ret)
Definition: filter_design.txt:179
DNxUcParseContext::height
uint32_t height
Definition: dnxuc_parser.c:36
DNxUcParseContext::fourcc_tag
uint32_t fourcc_tag
Definition: dnxuc_parser.c:34
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2908
HEADER_SIZE
#define HEADER_SIZE
Definition: adxenc.c:99
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
DNxUcParseContext
Definition: dnxuc_parser.c:33
avcodec.h
AVCodecParserContext
Definition: avcodec.h:2748
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecContext
main external API structure.
Definition: avcodec.h:451
DNxUcParseContext::width
uint32_t width
Definition: dnxuc_parser.c:35
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:476
AV_CODEC_ID_DNXUC
@ AV_CODEC_ID_DNXUC
Definition: codec_id.h:329
AVCodecParser
Definition: avcodec.h:2907
DNxUcParseContext::nr_bytes
uint32_t nr_bytes
Definition: dnxuc_parser.c:37
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
av_fourcc2str
#define av_fourcc2str(fourcc)
Definition: avutil.h:348