]> git.sesse.net Git - ffmpeg/blob - libavcodec/parser.c
Merge commit '54bc15d5ebfd07fd468743ba29f709ea19e840b9'
[ffmpeg] / libavcodec / parser.c
1 /*
2  * Audio and Video frame extraction
3  * Copyright (c) 2003 Fabrice Bellard
4  * Copyright (c) 2003 Michael Niedermayer
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <stdint.h>
24 #include <string.h>
25
26 #include "libavutil/atomic.h"
27 #include "libavutil/mem.h"
28
29 #include "parser.h"
30
31 static AVCodecParser *av_first_parser = NULL;
32
33 AVCodecParser *av_parser_next(const AVCodecParser *p)
34 {
35     if (p)
36         return p->next;
37     else
38         return av_first_parser;
39 }
40
41 void av_register_codec_parser(AVCodecParser *parser)
42 {
43     do {
44         parser->next = av_first_parser;
45     } while (parser->next != avpriv_atomic_ptr_cas((void * volatile *)&av_first_parser, parser->next, parser));
46 }
47
48 AVCodecParserContext *av_parser_init(int codec_id)
49 {
50     AVCodecParserContext *s = NULL;
51     AVCodecParser *parser;
52     int ret;
53
54     if (codec_id == AV_CODEC_ID_NONE)
55         return NULL;
56
57     for (parser = av_first_parser; parser; parser = parser->next) {
58         if (parser->codec_ids[0] == codec_id ||
59             parser->codec_ids[1] == codec_id ||
60             parser->codec_ids[2] == codec_id ||
61             parser->codec_ids[3] == codec_id ||
62             parser->codec_ids[4] == codec_id)
63             goto found;
64     }
65     return NULL;
66
67 found:
68     s = av_mallocz(sizeof(AVCodecParserContext));
69     if (!s)
70         goto err_out;
71     s->parser = parser;
72     s->priv_data = av_mallocz(parser->priv_data_size);
73     if (!s->priv_data)
74         goto err_out;
75     s->fetch_timestamp=1;
76     s->pict_type = AV_PICTURE_TYPE_I;
77     if (parser->parser_init) {
78         ret = parser->parser_init(s);
79         if (ret != 0)
80             goto err_out;
81     }
82     s->key_frame            = -1;
83     s->convergence_duration = 0;
84     s->dts_sync_point       = INT_MIN;
85     s->dts_ref_dts_delta    = INT_MIN;
86     s->pts_dts_delta        = INT_MIN;
87     return s;
88
89 err_out:
90     if (s)
91         av_freep(&s->priv_data);
92     av_free(s);
93     return NULL;
94 }
95
96 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
97 {
98     int i;
99
100     if (!fuzzy) {
101         s->dts    =
102         s->pts    = AV_NOPTS_VALUE;
103         s->pos    = -1;
104         s->offset = 0;
105     }
106     for (i = 0; i < AV_PARSER_PTS_NB; i++) {
107         if (s->cur_offset + off >= s->cur_frame_offset[i] &&
108             (s->frame_offset < s->cur_frame_offset[i] ||
109              (!s->frame_offset && !s->next_frame_offset)) && // first field/frame
110             // check disabled since MPEG-TS does not send complete PES packets
111             /*s->next_frame_offset + off <*/  s->cur_frame_end[i]){
112
113             if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) {
114                 s->dts    = s->cur_frame_dts[i];
115                 s->pts    = s->cur_frame_pts[i];
116                 s->pos    = s->cur_frame_pos[i];
117                 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
118             }
119             if (remove)
120                 s->cur_frame_offset[i] = INT64_MAX;
121             if (s->cur_offset + off < s->cur_frame_end[i])
122                 break;
123         }
124     }
125 }
126
127 int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
128                      uint8_t **poutbuf, int *poutbuf_size,
129                      const uint8_t *buf, int buf_size,
130                      int64_t pts, int64_t dts, int64_t pos)
131 {
132     int index, i;
133     uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
134
135     if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
136         s->next_frame_offset =
137         s->cur_offset        = pos;
138         s->flags            |= PARSER_FLAG_FETCHED_OFFSET;
139     }
140
141     if (buf_size == 0) {
142         /* padding is always necessary even if EOF, so we add it here */
143         memset(dummy_buf, 0, sizeof(dummy_buf));
144         buf = dummy_buf;
145     } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
146         /* add a new packet descriptor */
147         i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
148         s->cur_frame_start_index = i;
149         s->cur_frame_offset[i]   = s->cur_offset;
150         s->cur_frame_end[i]      = s->cur_offset + buf_size;
151         s->cur_frame_pts[i]      = pts;
152         s->cur_frame_dts[i]      = dts;
153         s->cur_frame_pos[i]      = pos;
154     }
155
156     if (s->fetch_timestamp) {
157         s->fetch_timestamp = 0;
158         s->last_pts        = s->pts;
159         s->last_dts        = s->dts;
160         s->last_pos        = s->pos;
161         ff_fetch_timestamp(s, 0, 0, 0);
162     }
163     /* WARNING: the returned index can be negative */
164     index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
165                                     poutbuf_size, buf, buf_size);
166     /* update the file pointer */
167     if (*poutbuf_size) {
168         /* fill the data for the current frame */
169         s->frame_offset = s->next_frame_offset;
170
171         /* offset of the next frame */
172         s->next_frame_offset = s->cur_offset + index;
173         s->fetch_timestamp   = 1;
174     }
175     if (index < 0)
176         index = 0;
177     s->cur_offset += index;
178     return index;
179 }
180
181 int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx,
182                      uint8_t **poutbuf, int *poutbuf_size,
183                      const uint8_t *buf, int buf_size, int keyframe)
184 {
185     if (s && s->parser->split) {
186         if (avctx->flags  & CODEC_FLAG_GLOBAL_HEADER ||
187             avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) {
188             int i = s->parser->split(avctx, buf, buf_size);
189             buf      += i;
190             buf_size -= i;
191         }
192     }
193
194     /* cast to avoid warning about discarding qualifiers */
195     *poutbuf      = (uint8_t *) buf;
196     *poutbuf_size = buf_size;
197     if (avctx->extradata) {
198         if (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) {
199             int size = buf_size + avctx->extradata_size;
200
201             *poutbuf_size = size;
202             *poutbuf      = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
203
204             memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
205             memcpy(*poutbuf + avctx->extradata_size, buf,
206                    buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
207             return 1;
208         }
209     }
210
211     return 0;
212 }
213
214 void av_parser_close(AVCodecParserContext *s)
215 {
216     if (s) {
217         if (s->parser->parser_close)
218             s->parser->parser_close(s);
219         av_freep(&s->priv_data);
220         av_free(s);
221     }
222 }
223
224 int ff_combine_frame(ParseContext *pc, int next,
225                      const uint8_t **buf, int *buf_size)
226 {
227     if (pc->overread) {
228         av_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
229                 pc->overread, pc->state, next, pc->index, pc->overread_index);
230         av_dlog(NULL, "%X %X %X %X\n",
231                 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
232     }
233
234     /* Copy overread bytes from last frame into buffer. */
235     for (; pc->overread > 0; pc->overread--)
236         pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
237
238     /* flush remaining if EOF */
239     if (!*buf_size && next == END_NOT_FOUND)
240         next = 0;
241
242     pc->last_index = pc->index;
243
244     /* copy into buffer end return */
245     if (next == END_NOT_FOUND) {
246         void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
247                                            *buf_size + pc->index +
248                                            FF_INPUT_BUFFER_PADDING_SIZE);
249
250         if (!new_buffer) {
251             pc->index = 0;
252             return AVERROR(ENOMEM);
253         }
254         pc->buffer = new_buffer;
255         memcpy(&pc->buffer[pc->index], *buf, *buf_size);
256         pc->index += *buf_size;
257         return -1;
258     }
259
260     *buf_size          =
261     pc->overread_index = pc->index + next;
262
263     /* append to buffer */
264     if (pc->index) {
265         void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
266                                            next + pc->index +
267                                            FF_INPUT_BUFFER_PADDING_SIZE);
268         if (!new_buffer) {
269             pc->overread_index =
270             pc->index = 0;
271             return AVERROR(ENOMEM);
272         }
273         pc->buffer = new_buffer;
274         if (next > -FF_INPUT_BUFFER_PADDING_SIZE)
275             memcpy(&pc->buffer[pc->index], *buf,
276                    next + FF_INPUT_BUFFER_PADDING_SIZE);
277         pc->index = 0;
278         *buf      = pc->buffer;
279     }
280
281     /* store overread bytes */
282     for (; next < 0; next++) {
283         pc->state   = pc->state   << 8 | pc->buffer[pc->last_index + next];
284         pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
285         pc->overread++;
286     }
287
288     if (pc->overread) {
289         av_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
290                 pc->overread, pc->state, next, pc->index, pc->overread_index);
291         av_dlog(NULL, "%X %X %X %X\n",
292                 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
293     }
294
295     return 0;
296 }
297
298 void ff_parse_close(AVCodecParserContext *s)
299 {
300     ParseContext *pc = s->priv_data;
301
302     av_freep(&pc->buffer);
303 }
304
305 int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
306 {
307     int i;
308     uint32_t state = -1;
309
310     for (i = 0; i < buf_size; i++) {
311         state = state << 8 | buf[i];
312         if (state == 0x1B3 || state == 0x1B6)
313             return i - 3;
314     }
315     return 0;
316 }