FFmpeg
prores_parser.c
Go to the documentation of this file.
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 "libavutil/intreadwrite.h"
20 #include "bytestream.h"
21 
22 #include "avcodec.h"
23 
25  AVCodecContext *avctx,
26  const uint8_t **poutbuf, int *poutbuf_size,
27  const uint8_t *buf, int buf_size)
28 {
29  GetByteContext gb;
30  uint8_t flags, depth, chroma_format, alpha_channel_type;
31 
32  *poutbuf = buf;
33  *poutbuf_size = buf_size;
34 
35  /* Frame fields + frame header size */
36  if (buf_size < 28)
37  return buf_size;
38 
39  bytestream2_init(&gb, buf, buf_size);
40 
41  /* Frame size */
42  if (bytestream2_get_be32(&gb) != buf_size)
43  return buf_size;
44 
45  /* Frame identifier */
46  if (bytestream2_get_le32(&gb) != MKTAG('i','c','p','f'))
47  return buf_size;
48 
49  /* Frame header size */
50  if (bytestream2_get_be16(&gb) < 20)
51  return buf_size;
52 
53  bytestream2_skip(&gb, 6); /* Bitstream version, encoder identifier */
54 
55  s->key_frame = 1;
56  s->pict_type = AV_PICTURE_TYPE_I;
57 
58  s->width = bytestream2_get_be16(&gb);
59  s->height = bytestream2_get_be16(&gb);
60  s->coded_width = FFALIGN(s->width, 16);
61  s->coded_height = FFALIGN(s->height, 16);
62 
63  flags = bytestream2_get_byte(&gb);
64 
65  /* Interlace mode */
66  switch (flags >> 2 & 3) {
67  case 0:
68  s->field_order = AV_FIELD_PROGRESSIVE;
69  s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
70  break;
71  case 1:
72  s->field_order = AV_FIELD_TT;
73  s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
74  break;
75  case 2:
76  s->field_order = AV_FIELD_BB;
77  s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
78  break;
79  default:
80  break;
81  }
82 
83  bytestream2_skip(&gb, 4); /* Aspect ratio information, frame rate code, color primaries, transfer characteristic, matrix coefficients */
84 
85  /* Determine pixel format based on color depth, chroma format and alpha type */
86  switch (avctx->codec_tag) {
87  case MKTAG('a','p','c','o'):
88  case MKTAG('a','p','c','s'):
89  case MKTAG('a','p','c','n'):
90  case MKTAG('a','p','c','h'):
91  depth = 10;
92  break;
93  case MKTAG('a','p','4','h'):
94  case MKTAG('a','p','4','x'):
95  depth = 12;
96  break;
97  default:
98  return buf_size;
99  }
100 
101  chroma_format = flags >> 6 & 3;
102  if (chroma_format < 2)
103  return buf_size;
104 
105  alpha_channel_type = bytestream2_get_byte(&gb) & 0xf;
106 
107  switch (depth | (chroma_format << 4) | (alpha_channel_type << 8)) {
108  case 10 | (2 << 4) | (0 << 8): s->format = AV_PIX_FMT_YUV422P10; break;
109  case 10 | (2 << 4) | (1 << 8):
110  case 10 | (2 << 4) | (2 << 8): s->format = AV_PIX_FMT_YUVA422P10; break;
111  case 10 | (3 << 4) | (0 << 8): s->format = AV_PIX_FMT_YUV444P10; break;
112  case 10 | (3 << 4) | (1 << 8):
113  case 10 | (3 << 4) | (2 << 8): s->format = AV_PIX_FMT_YUVA444P10; break;
114  case 12 | (2 << 4) | (0 << 8): s->format = AV_PIX_FMT_YUV422P12; break;
115  case 12 | (2 << 4) | (1 << 8):
116  case 12 | (2 << 4) | (2 << 8): s->format = AV_PIX_FMT_YUVA422P12; break;
117  case 12 | (3 << 4) | (0 << 8): s->format = AV_PIX_FMT_YUV444P12; break;
118  case 12 | (3 << 4) | (1 << 8):
119  case 12 | (3 << 4) | (2 << 8): s->format = AV_PIX_FMT_YUVA444P12; break;
120  }
121 
122  return buf_size;
123 }
124 
127  .parser_parse = parse,
128 };
flags
const SwsFlags flags[]
Definition: swscale.c:61
ff_prores_parser
const AVCodecParser ff_prores_parser
Definition: prores_parser.c:125
GetByteContext
Definition: bytestream.h:33
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: defs.h:213
AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:610
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
AV_FIELD_TT
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition: defs.h:214
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:555
AV_PICTURE_STRUCTURE_FRAME
@ AV_PICTURE_STRUCTURE_FRAME
coded as frame
Definition: avcodec.h:2572
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PIX_FMT_YUVA444P12
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:613
AV_PICTURE_STRUCTURE_BOTTOM_FIELD
@ AV_PICTURE_STRUCTURE_BOTTOM_FIELD
coded as bottom field
Definition: avcodec.h:2571
AV_PICTURE_STRUCTURE_TOP_FIELD
@ AV_PICTURE_STRUCTURE_TOP_FIELD
coded as top field
Definition: avcodec.h:2570
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:553
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2735
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:557
parse
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: prores_parser.c:24
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:559
AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:611
AV_FIELD_BB
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition: defs.h:215
avcodec.h
AVCodecParserContext
Definition: avcodec.h:2575
AVCodecContext
main external API structure.
Definition: avcodec.h:431
AV_PIX_FMT_YUVA422P12
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:612
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:456
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecParser
Definition: avcodec.h:2734
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
AV_CODEC_ID_PRORES
@ AV_CODEC_ID_PRORES
Definition: codec_id.h:200