]> git.sesse.net Git - ffmpeg/blob - libavcodec/tak_parser.c
decode: flush the internal bsfs instead of constantly reinitalizing them
[ffmpeg] / libavcodec / tak_parser.c
1 /*
2  * TAK parser
3  * Copyright (c) 2012 Michael Niedermayer
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * TAK parser
25  **/
26
27 #define BITSTREAM_READER_LE
28 #include "bitstream.h"
29 #include "parser.h"
30 #include "tak.h"
31
32 typedef struct TAKParseContext {
33     ParseContext  pc;
34     TAKStreamInfo ti;
35     int           index;
36 } TAKParseContext;
37
38 static av_cold int tak_init(AVCodecParserContext *s)
39 {
40     ff_tak_init_crc();
41     return 0;
42 }
43
44 static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
45                      const uint8_t **poutbuf, int *poutbuf_size,
46                      const uint8_t *buf, int buf_size)
47 {
48     TAKParseContext *t = s->priv_data;
49     ParseContext *pc   = &t->pc;
50     int next           = END_NOT_FOUND;
51     BitstreamContext bc;
52     int consumed = 0;
53     int needed   = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
54
55     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
56         TAKStreamInfo ti;
57         bitstream_init(&bc, buf, buf_size);
58         if (!ff_tak_decode_frame_header(avctx, &bc, &ti, 127))
59             s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
60                                                    : t->ti.frame_samples;
61         *poutbuf      = buf;
62         *poutbuf_size = buf_size;
63         return buf_size;
64     }
65
66     while (buf_size || t->index + needed <= pc->index) {
67         if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
68             int tmp_buf_size       = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES,
69                                            buf_size);
70             const uint8_t *tmp_buf = buf;
71
72             ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size);
73             consumed += tmp_buf_size;
74             buf      += tmp_buf_size;
75             buf_size -= tmp_buf_size;
76         }
77
78         for (; t->index + needed <= pc->index; t->index++)
79             if (pc->buffer[t->index]     == 0xFF &&
80                 pc->buffer[t->index + 1] == 0xA0) {
81                 TAKStreamInfo ti;
82
83                 bitstream_init8(&bc, pc->buffer + t->index,
84                                 pc->index - t->index);
85                 if (!ff_tak_decode_frame_header(avctx, &bc,
86                                                 pc->frame_start_found ? &ti
87                                                                       : &t->ti,
88                                                 127) &&
89                     !ff_tak_check_crc(pc->buffer + t->index,
90                                       bitstream_tell(&bc) / 8)) {
91                     if (!pc->frame_start_found) {
92                         pc->frame_start_found = 1;
93                         s->duration           = t->ti.last_frame_samples ?
94                                                 t->ti.last_frame_samples :
95                                                 t->ti.frame_samples;
96                     } else {
97                         pc->frame_start_found = 0;
98                         next                  = t->index - pc->index;
99                         t->index              = 0;
100                         goto found;
101                     }
102                 }
103             }
104     }
105
106 found:
107     if (consumed && !buf_size && next == END_NOT_FOUND ||
108         ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
109         *poutbuf      = NULL;
110         *poutbuf_size = 0;
111         return buf_size + consumed;
112     }
113
114     if (next != END_NOT_FOUND) {
115         next        += consumed;
116         pc->overread = FFMAX(0, -next);
117     }
118
119     *poutbuf      = buf;
120     *poutbuf_size = buf_size;
121     return next;
122 }
123
124 AVCodecParser ff_tak_parser = {
125     .codec_ids      = { AV_CODEC_ID_TAK },
126     .priv_data_size = sizeof(TAKParseContext),
127     .parser_init    = tak_init,
128     .parser_parse   = tak_parse,
129     .parser_close   = ff_parse_close,
130 };