]> git.sesse.net Git - ffmpeg/blob - libavcodec/parser.c
Merge commit 'e4cdef00263dc8b3c8de9d34ceacd00dc68979c0'
[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 <inttypes.h>
24 #include <stdint.h>
25 #include <string.h>
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/thread.h"
31
32 #include "internal.h"
33 #include "parser.h"
34
35 /* Parsers */
36 extern AVCodecParser ff_aac_parser;
37 extern AVCodecParser ff_aac_latm_parser;
38 extern AVCodecParser ff_ac3_parser;
39 extern AVCodecParser ff_adx_parser;
40 extern AVCodecParser ff_bmp_parser;
41 extern AVCodecParser ff_cavsvideo_parser;
42 extern AVCodecParser ff_cook_parser;
43 extern AVCodecParser ff_dca_parser;
44 extern AVCodecParser ff_dirac_parser;
45 extern AVCodecParser ff_dnxhd_parser;
46 extern AVCodecParser ff_dpx_parser;
47 extern AVCodecParser ff_dvaudio_parser;
48 extern AVCodecParser ff_dvbsub_parser;
49 extern AVCodecParser ff_dvdsub_parser;
50 extern AVCodecParser ff_dvd_nav_parser;
51 extern AVCodecParser ff_flac_parser;
52 extern AVCodecParser ff_g729_parser;
53 extern AVCodecParser ff_gsm_parser;
54 extern AVCodecParser ff_h261_parser;
55 extern AVCodecParser ff_h263_parser;
56 extern AVCodecParser ff_h264_parser;
57 extern AVCodecParser ff_hevc_parser;
58 extern AVCodecParser ff_mjpeg_parser;
59 extern AVCodecParser ff_mlp_parser;
60 extern AVCodecParser ff_mpeg4video_parser;
61 extern AVCodecParser ff_mpegaudio_parser;
62 extern AVCodecParser ff_mpegvideo_parser;
63 extern AVCodecParser ff_opus_parser;
64 extern AVCodecParser ff_png_parser;
65 extern AVCodecParser ff_pnm_parser;
66 extern AVCodecParser ff_rv30_parser;
67 extern AVCodecParser ff_rv40_parser;
68 extern AVCodecParser ff_sipr_parser;
69 extern AVCodecParser ff_tak_parser;
70 extern AVCodecParser ff_vc1_parser;
71 extern AVCodecParser ff_vorbis_parser;
72 extern AVCodecParser ff_vp3_parser;
73 extern AVCodecParser ff_vp8_parser;
74 extern AVCodecParser ff_vp9_parser;
75 extern AVCodecParser ff_xma_parser;
76
77 #include "libavcodec/parser_list.c"
78
79 static AVOnce av_parser_next_init = AV_ONCE_INIT;
80
81 static void av_parser_init_next(void)
82 {
83     AVCodecParser *prev = NULL, *p;
84     int i = 0;
85     while ((p = (AVCodecParser*)parser_list[i++])) {
86         if (prev)
87             prev->next = p;
88         prev = p;
89     }
90 }
91
92 AVCodecParser *av_parser_next(const AVCodecParser *p)
93 {
94     ff_thread_once(&av_parser_next_init, av_parser_init_next);
95
96     if (p)
97         return p->next;
98     else
99         return (AVCodecParser*)parser_list[0];
100 }
101
102 const AVCodecParser *av_parser_iterate(void **opaque)
103 {
104     uintptr_t i = (uintptr_t)*opaque;
105     const AVCodecParser *p = parser_list[i];
106
107     if (p)
108         *opaque = (void*)(i + 1);
109
110     return p;
111 }
112
113 void av_register_codec_parser(AVCodecParser *parser)
114 {
115     ff_thread_once(&av_parser_next_init, av_parser_init_next);
116 }
117
118 AVCodecParserContext *av_parser_init(int codec_id)
119 {
120     AVCodecParserContext *s = NULL;
121     const AVCodecParser *parser;
122     void *i = 0;
123     int ret;
124
125     if (codec_id == AV_CODEC_ID_NONE)
126         return NULL;
127
128     while ((parser = av_parser_iterate(&i))) {
129         if (parser->codec_ids[0] == codec_id ||
130             parser->codec_ids[1] == codec_id ||
131             parser->codec_ids[2] == codec_id ||
132             parser->codec_ids[3] == codec_id ||
133             parser->codec_ids[4] == codec_id)
134             goto found;
135     }
136     return NULL;
137
138 found:
139     s = av_mallocz(sizeof(AVCodecParserContext));
140     if (!s)
141         goto err_out;
142     s->parser = (AVCodecParser*)parser;
143     s->priv_data = av_mallocz(parser->priv_data_size);
144     if (!s->priv_data)
145         goto err_out;
146     s->fetch_timestamp=1;
147     s->pict_type = AV_PICTURE_TYPE_I;
148     if (parser->parser_init) {
149         ret = parser->parser_init(s);
150         if (ret != 0)
151             goto err_out;
152     }
153     s->key_frame            = -1;
154 #if FF_API_CONVERGENCE_DURATION
155 FF_DISABLE_DEPRECATION_WARNINGS
156     s->convergence_duration = 0;
157 FF_ENABLE_DEPRECATION_WARNINGS
158 #endif
159     s->dts_sync_point       = INT_MIN;
160     s->dts_ref_dts_delta    = INT_MIN;
161     s->pts_dts_delta        = INT_MIN;
162     s->format               = -1;
163
164     return s;
165
166 err_out:
167     if (s)
168         av_freep(&s->priv_data);
169     av_free(s);
170     return NULL;
171 }
172
173 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
174 {
175     int i;
176
177     if (!fuzzy) {
178         s->dts    =
179         s->pts    = AV_NOPTS_VALUE;
180         s->pos    = -1;
181         s->offset = 0;
182     }
183     for (i = 0; i < AV_PARSER_PTS_NB; i++) {
184         if (s->cur_offset + off >= s->cur_frame_offset[i] &&
185             (s->frame_offset < s->cur_frame_offset[i] ||
186              (!s->frame_offset && !s->next_frame_offset)) && // first field/frame
187             // check disabled since MPEG-TS does not send complete PES packets
188             /*s->next_frame_offset + off <*/  s->cur_frame_end[i]){
189
190             if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) {
191                 s->dts    = s->cur_frame_dts[i];
192                 s->pts    = s->cur_frame_pts[i];
193                 s->pos    = s->cur_frame_pos[i];
194                 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
195             }
196             if (remove)
197                 s->cur_frame_offset[i] = INT64_MAX;
198             if (s->cur_offset + off < s->cur_frame_end[i])
199                 break;
200         }
201     }
202 }
203
204 int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
205                      uint8_t **poutbuf, int *poutbuf_size,
206                      const uint8_t *buf, int buf_size,
207                      int64_t pts, int64_t dts, int64_t pos)
208 {
209     int index, i;
210     uint8_t dummy_buf[AV_INPUT_BUFFER_PADDING_SIZE];
211
212     av_assert1(avctx->codec_id != AV_CODEC_ID_NONE);
213
214     /* Parsers only work for the specified codec ids. */
215     av_assert1(avctx->codec_id == s->parser->codec_ids[0] ||
216                avctx->codec_id == s->parser->codec_ids[1] ||
217                avctx->codec_id == s->parser->codec_ids[2] ||
218                avctx->codec_id == s->parser->codec_ids[3] ||
219                avctx->codec_id == s->parser->codec_ids[4]);
220
221     if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
222         s->next_frame_offset =
223         s->cur_offset        = pos;
224         s->flags            |= PARSER_FLAG_FETCHED_OFFSET;
225     }
226
227     if (buf_size == 0) {
228         /* padding is always necessary even if EOF, so we add it here */
229         memset(dummy_buf, 0, sizeof(dummy_buf));
230         buf = dummy_buf;
231     } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
232         /* add a new packet descriptor */
233         i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
234         s->cur_frame_start_index = i;
235         s->cur_frame_offset[i]   = s->cur_offset;
236         s->cur_frame_end[i]      = s->cur_offset + buf_size;
237         s->cur_frame_pts[i]      = pts;
238         s->cur_frame_dts[i]      = dts;
239         s->cur_frame_pos[i]      = pos;
240     }
241
242     if (s->fetch_timestamp) {
243         s->fetch_timestamp = 0;
244         s->last_pts        = s->pts;
245         s->last_dts        = s->dts;
246         s->last_pos        = s->pos;
247         ff_fetch_timestamp(s, 0, 0, 0);
248     }
249     /* WARNING: the returned index can be negative */
250     index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
251                                     poutbuf_size, buf, buf_size);
252     av_assert0(index > -0x20000000); // The API does not allow returning AVERROR codes
253 #define FILL(name) if(s->name > 0 && avctx->name <= 0) avctx->name = s->name
254     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
255         FILL(field_order);
256     }
257
258     /* update the file pointer */
259     if (*poutbuf_size) {
260         /* fill the data for the current frame */
261         s->frame_offset = s->next_frame_offset;
262
263         /* offset of the next frame */
264         s->next_frame_offset = s->cur_offset + index;
265         s->fetch_timestamp   = 1;
266     }
267     if (index < 0)
268         index = 0;
269     s->cur_offset += index;
270     return index;
271 }
272
273 int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx,
274                      uint8_t **poutbuf, int *poutbuf_size,
275                      const uint8_t *buf, int buf_size, int keyframe)
276 {
277     if (s && s->parser->split) {
278         if (avctx->flags  & AV_CODEC_FLAG_GLOBAL_HEADER ||
279             avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER) {
280             int i = s->parser->split(avctx, buf, buf_size);
281             buf      += i;
282             buf_size -= i;
283         }
284     }
285
286     /* cast to avoid warning about discarding qualifiers */
287     *poutbuf      = (uint8_t *) buf;
288     *poutbuf_size = buf_size;
289     if (avctx->extradata) {
290         if (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER)) {
291             int size = buf_size + avctx->extradata_size;
292
293             *poutbuf_size = size;
294             *poutbuf      = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
295             if (!*poutbuf)
296                 return AVERROR(ENOMEM);
297
298             memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
299             memcpy(*poutbuf + avctx->extradata_size, buf,
300                    buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
301             return 1;
302         }
303     }
304
305     return 0;
306 }
307
308 void av_parser_close(AVCodecParserContext *s)
309 {
310     if (s) {
311         if (s->parser->parser_close)
312             s->parser->parser_close(s);
313         av_freep(&s->priv_data);
314         av_free(s);
315     }
316 }
317
318 int ff_combine_frame(ParseContext *pc, int next,
319                      const uint8_t **buf, int *buf_size)
320 {
321     if (pc->overread) {
322         ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
323                 pc->overread, pc->state, next, pc->index, pc->overread_index);
324         ff_dlog(NULL, "%X %X %X %X\n",
325                 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
326     }
327
328     /* Copy overread bytes from last frame into buffer. */
329     for (; pc->overread > 0; pc->overread--)
330         pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
331
332     /* flush remaining if EOF */
333     if (!*buf_size && next == END_NOT_FOUND)
334         next = 0;
335
336     pc->last_index = pc->index;
337
338     /* copy into buffer end return */
339     if (next == END_NOT_FOUND) {
340         void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
341                                            *buf_size + pc->index +
342                                            AV_INPUT_BUFFER_PADDING_SIZE);
343
344         if (!new_buffer) {
345             av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", *buf_size + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
346             pc->index = 0;
347             return AVERROR(ENOMEM);
348         }
349         pc->buffer = new_buffer;
350         memcpy(&pc->buffer[pc->index], *buf, *buf_size);
351         pc->index += *buf_size;
352         return -1;
353     }
354
355     av_assert0(next >= 0 || pc->buffer);
356
357     *buf_size          =
358     pc->overread_index = pc->index + next;
359
360     /* append to buffer */
361     if (pc->index) {
362         void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
363                                            next + pc->index +
364                                            AV_INPUT_BUFFER_PADDING_SIZE);
365         if (!new_buffer) {
366             av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", next + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
367             pc->overread_index =
368             pc->index = 0;
369             return AVERROR(ENOMEM);
370         }
371         pc->buffer = new_buffer;
372         if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
373             memcpy(&pc->buffer[pc->index], *buf,
374                    next + AV_INPUT_BUFFER_PADDING_SIZE);
375         pc->index = 0;
376         *buf      = pc->buffer;
377     }
378
379     /* store overread bytes */
380     for (; next < 0; next++) {
381         pc->state   = pc->state   << 8 | pc->buffer[pc->last_index + next];
382         pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
383         pc->overread++;
384     }
385
386     if (pc->overread) {
387         ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
388                 pc->overread, pc->state, next, pc->index, pc->overread_index);
389         ff_dlog(NULL, "%X %X %X %X\n",
390                 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
391     }
392
393     return 0;
394 }
395
396 void ff_parse_close(AVCodecParserContext *s)
397 {
398     ParseContext *pc = s->priv_data;
399
400     av_freep(&pc->buffer);
401 }
402
403 int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
404 {
405     uint32_t state = -1;
406     const uint8_t *ptr = buf, *end = buf + buf_size;
407
408     while (ptr < end) {
409         ptr = avpriv_find_start_code(ptr, end, &state);
410         if (state == 0x1B3 || state == 0x1B6)
411             return ptr - 4 - buf;
412     }
413
414     return 0;
415 }