]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp9_parser.c
avcodec/ccaption_dec: Fix typos and cosmetics
[ffmpeg] / libavcodec / vp9_parser.c
1 /*
2  * Copyright (C) 2008 Michael Niedermayer
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/intreadwrite.h"
22 #include "parser.h"
23
24 typedef struct VP9ParseContext {
25     int n_frames; // 1-8
26     int size[8];
27 } VP9ParseContext;
28
29 static void parse_frame(AVCodecParserContext *ctx, const uint8_t *buf, int size)
30 {
31     if (buf[0] & 0x4) {
32         ctx->pict_type = AV_PICTURE_TYPE_P;
33         ctx->key_frame = 0;
34     } else {
35         ctx->pict_type = AV_PICTURE_TYPE_I;
36         ctx->key_frame = 1;
37     }
38 }
39
40 static int parse(AVCodecParserContext *ctx,
41                  AVCodecContext *avctx,
42                  const uint8_t **out_data, int *out_size,
43                  const uint8_t *data, int size)
44 {
45     VP9ParseContext *s = ctx->priv_data;
46     int full_size = size;
47     int marker;
48
49     if (size <= 0) {
50         *out_size = 0;
51         *out_data = data;
52
53         return 0;
54     }
55
56     if (s->n_frames > 0) {
57         *out_data = data;
58         *out_size = s->size[--s->n_frames];
59         parse_frame(ctx, *out_data, *out_size);
60
61         return s->n_frames > 0 ? *out_size : size /* i.e. include idx tail */;
62     }
63
64     marker = data[size - 1];
65     if ((marker & 0xe0) == 0xc0) {
66         int nbytes = 1 + ((marker >> 3) & 0x3);
67         int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
68
69         if (size >= idx_sz && data[size - idx_sz] == marker) {
70             const uint8_t *idx = data + size + 1 - idx_sz;
71             int first = 1;
72
73             switch (nbytes) {
74 #define case_n(a, rd) \
75             case a: \
76                 while (n_frames--) { \
77                     unsigned sz = rd; \
78                     idx += a; \
79                     if (sz > size) { \
80                         s->n_frames = 0; \
81                         *out_size = size; \
82                         *out_data = data; \
83                         av_log(avctx, AV_LOG_ERROR, \
84                                "Superframe packet size too big: %u > %d\n", \
85                                sz, size); \
86                         return full_size; \
87                     } \
88                     if (first) { \
89                         first = 0; \
90                         *out_data = data; \
91                         *out_size = sz; \
92                         s->n_frames = n_frames; \
93                     } else { \
94                         s->size[n_frames] = sz; \
95                     } \
96                     data += sz; \
97                     size -= sz; \
98                 } \
99                 parse_frame(ctx, *out_data, *out_size); \
100                 return *out_size
101
102                 case_n(1, *idx);
103                 case_n(2, AV_RL16(idx));
104                 case_n(3, AV_RL24(idx));
105                 case_n(4, AV_RL32(idx));
106             }
107         }
108     }
109
110     *out_data = data;
111     *out_size = size;
112     parse_frame(ctx, data, size);
113
114     return size;
115 }
116
117 AVCodecParser ff_vp9_parser = {
118     .codec_ids      = { AV_CODEC_ID_VP9 },
119     .priv_data_size = sizeof(VP9ParseContext),
120     .parser_parse   = parse,
121 };