]> git.sesse.net Git - ffmpeg/blob - libavcodec/parser.c
Merge commit '8a9641a652ed1546fedfda22584f79d3d423096e'
[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             if (!*poutbuf)
204                 return AVERROR(ENOMEM);
205
206             memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
207             memcpy(*poutbuf + avctx->extradata_size, buf,
208                    buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
209             return 1;
210         }
211     }
212
213     return 0;
214 }
215
216 void av_parser_close(AVCodecParserContext *s)
217 {
218     if (s) {
219         if (s->parser->parser_close)
220             s->parser->parser_close(s);
221         av_freep(&s->priv_data);
222         av_free(s);
223     }
224 }
225
226 int ff_combine_frame(ParseContext *pc, int next,
227                      const uint8_t **buf, int *buf_size)
228 {
229     if (pc->overread) {
230         av_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
231                 pc->overread, pc->state, next, pc->index, pc->overread_index);
232         av_dlog(NULL, "%X %X %X %X\n",
233                 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
234     }
235
236     /* Copy overread bytes from last frame into buffer. */
237     for (; pc->overread > 0; pc->overread--)
238         pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
239
240     /* flush remaining if EOF */
241     if (!*buf_size && next == END_NOT_FOUND)
242         next = 0;
243
244     pc->last_index = pc->index;
245
246     /* copy into buffer end return */
247     if (next == END_NOT_FOUND) {
248         void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
249                                            *buf_size + pc->index +
250                                            FF_INPUT_BUFFER_PADDING_SIZE);
251
252         if (!new_buffer) {
253             pc->index = 0;
254             return AVERROR(ENOMEM);
255         }
256         pc->buffer = new_buffer;
257         memcpy(&pc->buffer[pc->index], *buf, *buf_size);
258         pc->index += *buf_size;
259         return -1;
260     }
261
262     *buf_size          =
263     pc->overread_index = pc->index + next;
264
265     /* append to buffer */
266     if (pc->index) {
267         void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
268                                            next + pc->index +
269                                            FF_INPUT_BUFFER_PADDING_SIZE);
270         if (!new_buffer) {
271             pc->overread_index =
272             pc->index = 0;
273             return AVERROR(ENOMEM);
274         }
275         pc->buffer = new_buffer;
276         if (next > -FF_INPUT_BUFFER_PADDING_SIZE)
277             memcpy(&pc->buffer[pc->index], *buf,
278                    next + FF_INPUT_BUFFER_PADDING_SIZE);
279         pc->index = 0;
280         *buf      = pc->buffer;
281     }
282
283     /* store overread bytes */
284     for (; next < 0; next++) {
285         pc->state   = pc->state   << 8 | pc->buffer[pc->last_index + next];
286         pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
287         pc->overread++;
288     }
289
290     if (pc->overread) {
291         av_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
292                 pc->overread, pc->state, next, pc->index, pc->overread_index);
293         av_dlog(NULL, "%X %X %X %X\n",
294                 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
295     }
296
297     return 0;
298 }
299
300 void ff_parse_close(AVCodecParserContext *s)
301 {
302     ParseContext *pc = s->priv_data;
303
304     av_freep(&pc->buffer);
305 }
306
307 int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
308 {
309     int i;
310     uint32_t state = -1;
311
312     for (i = 0; i < buf_size; i++) {
313         state = state << 8 | buf[i];
314         if (state == 0x1B3 || state == 0x1B6)
315             return i - 3;
316     }
317     return 0;
318 }