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