FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vp9_parser.c
Go to the documentation of this file.
1 /*
2  * VP9 compatible video decoder
3  *
4  * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
5  * Copyright (C) 2013 Clément Bœsch <u pkh me>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "libavutil/intreadwrite.h"
25 #include "parser.h"
26 
27 typedef struct VP9ParseContext {
28  int n_frames; // 1-8
29  int size[8];
30  int64_t pts;
32 
33 static void parse_frame(AVCodecParserContext *ctx, const uint8_t *buf, int size)
34 {
35  VP9ParseContext *s = ctx->priv_data;
36 
37  if (buf[0] & 0x4) {
39  ctx->key_frame = 0;
40  } else {
42  ctx->key_frame = 1;
43  }
44 
45  if (buf[0] & 0x2) {
46  if (ctx->pts == AV_NOPTS_VALUE)
47  ctx->pts = s->pts;
48  s->pts = AV_NOPTS_VALUE;
49  } else {
50  s->pts = ctx->pts;
51  ctx->pts = AV_NOPTS_VALUE;
52  }
53 }
54 
55 static int parse(AVCodecParserContext *ctx,
56  AVCodecContext *avctx,
57  const uint8_t **out_data, int *out_size,
58  const uint8_t *data, int size)
59 {
60  VP9ParseContext *s = ctx->priv_data;
61  int full_size = size;
62  int marker;
63 
64  if (size <= 0) {
65  *out_size = 0;
66  *out_data = data;
67 
68  return 0;
69  }
70 
71  if (s->n_frames > 0) {
72  *out_data = data;
73  *out_size = s->size[--s->n_frames];
74  parse_frame(ctx, *out_data, *out_size);
75 
76  return s->n_frames > 0 ? *out_size : size /* i.e. include idx tail */;
77  }
78 
79  marker = data[size - 1];
80  if ((marker & 0xe0) == 0xc0) {
81  int nbytes = 1 + ((marker >> 3) & 0x3);
82  int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
83 
84  if (size >= idx_sz && data[size - idx_sz] == marker) {
85  const uint8_t *idx = data + size + 1 - idx_sz;
86  int first = 1;
87 
88  switch (nbytes) {
89 #define case_n(a, rd) \
90  case a: \
91  while (n_frames--) { \
92  unsigned sz = rd; \
93  idx += a; \
94  if (sz > size) { \
95  s->n_frames = 0; \
96  *out_size = size; \
97  *out_data = data; \
98  av_log(avctx, AV_LOG_ERROR, \
99  "Superframe packet size too big: %u > %d\n", \
100  sz, size); \
101  return full_size; \
102  } \
103  if (first) { \
104  first = 0; \
105  *out_data = data; \
106  *out_size = sz; \
107  s->n_frames = n_frames; \
108  } else { \
109  s->size[n_frames] = sz; \
110  } \
111  data += sz; \
112  size -= sz; \
113  } \
114  parse_frame(ctx, *out_data, *out_size); \
115  return *out_size
116 
117  case_n(1, *idx);
118  case_n(2, AV_RL16(idx));
119  case_n(3, AV_RL24(idx));
120  case_n(4, AV_RL32(idx));
121  }
122  }
123  }
124 
125  *out_data = data;
126  *out_size = size;
127  parse_frame(ctx, data, size);
128 
129  return size;
130 }
131 
133  .codec_ids = { AV_CODEC_ID_VP9 },
134  .priv_data_size = sizeof(VP9ParseContext),
135  .parser_parse = parse,
136 };
AVCodecParser ff_vp9_parser
Definition: vp9_parser.c:132
const char * s
Definition: avisynth_c.h:631
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int codec_ids[5]
Definition: avcodec.h:4426
static int parse(AVCodecParserContext *ctx, AVCodecContext *avctx, const uint8_t **out_data, int *out_size, const uint8_t *data, int size)
Definition: vp9_parser.c:55
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:85
static void parse_frame(AVCodecParserContext *ctx, const uint8_t *buf, int size)
Definition: vp9_parser.c:33
uint8_t
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:85
main external API structure.
Definition: avcodec.h:1241
#define case_n(a, rd)
void * buf
Definition: avisynth_c.h:553
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:85
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:4296
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241
Predicted.
Definition: avutil.h:268