Commit | Line | Data |
---|---|---|
8424cf50 FB |
1 | /* |
2 | * Audio and Video frame extraction | |
3 | * Copyright (c) 2003 Fabrice Bellard. | |
4 | * Copyright (c) 2003 Michael Niedermayer. | |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
20 | #include "avcodec.h" | |
21 | #include "mpegvideo.h" | |
22 | #include "mpegaudio.h" | |
23 | ||
24 | AVCodecParser *av_first_parser = NULL; | |
25 | ||
26 | void av_register_codec_parser(AVCodecParser *parser) | |
27 | { | |
28 | parser->next = av_first_parser; | |
29 | av_first_parser = parser; | |
30 | } | |
31 | ||
32 | AVCodecParserContext *av_parser_init(int codec_id) | |
33 | { | |
34 | AVCodecParserContext *s; | |
35 | AVCodecParser *parser; | |
36 | int ret; | |
8845e427 MN |
37 | |
38 | if(codec_id == CODEC_ID_NONE) | |
39 | return NULL; | |
8424cf50 FB |
40 | |
41 | for(parser = av_first_parser; parser != NULL; parser = parser->next) { | |
42 | if (parser->codec_ids[0] == codec_id || | |
43 | parser->codec_ids[1] == codec_id || | |
99f06236 MN |
44 | parser->codec_ids[2] == codec_id || |
45 | parser->codec_ids[3] == codec_id || | |
46 | parser->codec_ids[4] == codec_id) | |
8424cf50 FB |
47 | goto found; |
48 | } | |
49 | return NULL; | |
50 | found: | |
51 | s = av_mallocz(sizeof(AVCodecParserContext)); | |
52 | if (!s) | |
53 | return NULL; | |
54 | s->parser = parser; | |
55 | s->priv_data = av_mallocz(parser->priv_data_size); | |
56 | if (!s->priv_data) { | |
57 | av_free(s); | |
58 | return NULL; | |
59 | } | |
60 | if (parser->parser_init) { | |
61 | ret = parser->parser_init(s); | |
62 | if (ret != 0) { | |
63 | av_free(s->priv_data); | |
64 | av_free(s); | |
65 | return NULL; | |
66 | } | |
67 | } | |
a62aecce | 68 | s->fetch_timestamp=1; |
8424cf50 FB |
69 | return s; |
70 | } | |
71 | ||
6e45e928 FB |
72 | /* NOTE: buf_size == 0 is used to signal EOF so that the last frame |
73 | can be returned if necessary */ | |
8424cf50 FB |
74 | int av_parser_parse(AVCodecParserContext *s, |
75 | AVCodecContext *avctx, | |
76 | uint8_t **poutbuf, int *poutbuf_size, | |
b84f2a35 FB |
77 | const uint8_t *buf, int buf_size, |
78 | int64_t pts, int64_t dts) | |
8424cf50 | 79 | { |
b84f2a35 | 80 | int index, i, k; |
6e45e928 FB |
81 | uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; |
82 | ||
83 | if (buf_size == 0) { | |
84 | /* padding is always necessary even if EOF, so we add it here */ | |
85 | memset(dummy_buf, 0, sizeof(dummy_buf)); | |
86 | buf = dummy_buf; | |
b84f2a35 FB |
87 | } else { |
88 | /* add a new packet descriptor */ | |
89 | k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1); | |
90 | s->cur_frame_start_index = k; | |
91 | s->cur_frame_offset[k] = s->cur_offset; | |
92 | s->cur_frame_pts[k] = pts; | |
93 | s->cur_frame_dts[k] = dts; | |
94 | ||
95 | /* fill first PTS/DTS */ | |
a62aecce MN |
96 | if (s->fetch_timestamp){ |
97 | s->fetch_timestamp=0; | |
b84f2a35 FB |
98 | s->last_pts = pts; |
99 | s->last_dts = dts; | |
c77a9a0e MN |
100 | s->cur_frame_pts[k] = |
101 | s->cur_frame_dts[k] = AV_NOPTS_VALUE; | |
b84f2a35 | 102 | } |
6e45e928 FB |
103 | } |
104 | ||
8424cf50 FB |
105 | /* WARNING: the returned index can be negative */ |
106 | index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size); | |
c77a9a0e | 107 | //av_log(NULL, AV_LOG_DEBUG, "parser: in:%lld, %lld, out:%lld, %lld, in:%d out:%d id:%d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id); |
8424cf50 FB |
108 | /* update the file pointer */ |
109 | if (*poutbuf_size) { | |
b84f2a35 | 110 | /* fill the data for the current frame */ |
8424cf50 | 111 | s->frame_offset = s->last_frame_offset; |
b84f2a35 FB |
112 | s->pts = s->last_pts; |
113 | s->dts = s->last_dts; | |
114 | ||
115 | /* offset of the next frame */ | |
8424cf50 | 116 | s->last_frame_offset = s->cur_offset + index; |
b84f2a35 FB |
117 | /* find the packet in which the new frame starts. It |
118 | is tricky because of MPEG video start codes | |
119 | which can begin in one packet and finish in | |
120 | another packet. In the worst case, an MPEG | |
121 | video start code could be in 4 different | |
122 | packets. */ | |
123 | k = s->cur_frame_start_index; | |
124 | for(i = 0; i < AV_PARSER_PTS_NB; i++) { | |
125 | if (s->last_frame_offset >= s->cur_frame_offset[k]) | |
126 | break; | |
127 | k = (k - 1) & (AV_PARSER_PTS_NB - 1); | |
128 | } | |
a62aecce | 129 | |
b84f2a35 FB |
130 | s->last_pts = s->cur_frame_pts[k]; |
131 | s->last_dts = s->cur_frame_dts[k]; | |
a62aecce MN |
132 | |
133 | /* some parsers tell us the packet size even before seeing the first byte of the next packet, | |
134 | so the next pts/dts is in the next chunk */ | |
135 | if(index == buf_size){ | |
136 | s->fetch_timestamp=1; | |
137 | } | |
8424cf50 FB |
138 | } |
139 | if (index < 0) | |
140 | index = 0; | |
141 | s->cur_offset += index; | |
142 | return index; | |
143 | } | |
144 | ||
90ad92b3 MN |
145 | int av_parser_change(AVCodecParserContext *s, |
146 | AVCodecContext *avctx, | |
147 | uint8_t **poutbuf, int *poutbuf_size, | |
148 | const uint8_t *buf, int buf_size, int keyframe){ | |
149 | ||
150 | if(s && s->parser->split){ | |
151 | if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) && !(avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){ | |
152 | int i= s->parser->split(avctx, buf, buf_size); | |
153 | buf += i; | |
154 | buf_size -= i; | |
155 | } | |
156 | } | |
157 | ||
158 | *poutbuf= buf; | |
159 | *poutbuf_size= buf_size; | |
160 | if(avctx->extradata){ | |
161 | if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) | |
162 | /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/ | |
163 | /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){ | |
164 | int size= buf_size + avctx->extradata_size; | |
165 | *poutbuf_size= size; | |
166 | *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); | |
167 | ||
168 | memcpy(*poutbuf, avctx->extradata, avctx->extradata_size); | |
169 | memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size); | |
170 | return 1; | |
171 | } | |
172 | } | |
173 | ||
174 | return 0; | |
175 | } | |
176 | ||
8424cf50 FB |
177 | void av_parser_close(AVCodecParserContext *s) |
178 | { | |
179 | if (s->parser->parser_close) | |
180 | s->parser->parser_close(s); | |
181 | av_free(s->priv_data); | |
182 | av_free(s); | |
183 | } | |
184 | ||
185 | /*****************************************************/ | |
186 | ||
187 | //#define END_NOT_FOUND (-100) | |
188 | ||
189 | #define PICTURE_START_CODE 0x00000100 | |
190 | #define SEQ_START_CODE 0x000001b3 | |
191 | #define EXT_START_CODE 0x000001b5 | |
192 | #define SLICE_MIN_START_CODE 0x00000101 | |
193 | #define SLICE_MAX_START_CODE 0x000001af | |
194 | ||
195 | typedef struct ParseContext1{ | |
e4cb187d MN |
196 | ParseContext pc; |
197 | /* XXX/FIXME PC1 vs. PC */ | |
8424cf50 FB |
198 | /* MPEG2 specific */ |
199 | int frame_rate; | |
200 | int progressive_sequence; | |
201 | int width, height; | |
c6f353ff | 202 | |
8424cf50 FB |
203 | /* XXX: suppress that, needed by MPEG4 */ |
204 | MpegEncContext *enc; | |
c6f353ff | 205 | int first_picture; |
8424cf50 FB |
206 | } ParseContext1; |
207 | ||
208 | /** | |
209 | * combines the (truncated) bitstream to a complete frame | |
210 | * @returns -1 if no complete frame could be created | |
211 | */ | |
e4cb187d | 212 | int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size) |
8424cf50 FB |
213 | { |
214 | #if 0 | |
215 | if(pc->overread){ | |
216 | printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
217 | printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
218 | } | |
219 | #endif | |
220 | ||
221 | /* copy overreaded bytes from last frame into buffer */ | |
222 | for(; pc->overread>0; pc->overread--){ | |
223 | pc->buffer[pc->index++]= pc->buffer[pc->overread_index++]; | |
224 | } | |
f48c0551 MN |
225 | |
226 | /* flush remaining if EOF */ | |
227 | if(!*buf_size && next == END_NOT_FOUND){ | |
228 | next= 0; | |
229 | } | |
230 | ||
8424cf50 FB |
231 | pc->last_index= pc->index; |
232 | ||
233 | /* copy into buffer end return */ | |
234 | if(next == END_NOT_FOUND){ | |
235 | pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
236 | ||
237 | memcpy(&pc->buffer[pc->index], *buf, *buf_size); | |
238 | pc->index += *buf_size; | |
239 | return -1; | |
240 | } | |
241 | ||
242 | *buf_size= | |
243 | pc->overread_index= pc->index + next; | |
244 | ||
245 | /* append to buffer */ | |
246 | if(pc->index){ | |
247 | pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
248 | ||
249 | memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE ); | |
250 | pc->index = 0; | |
251 | *buf= pc->buffer; | |
252 | } | |
253 | ||
254 | /* store overread bytes */ | |
255 | for(;next < 0; next++){ | |
256 | pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next]; | |
257 | pc->overread++; | |
258 | } | |
259 | ||
260 | #if 0 | |
261 | if(pc->overread){ | |
262 | printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
263 | printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
264 | } | |
265 | #endif | |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
8424cf50 FB |
270 | static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end) |
271 | { | |
272 | const uint8_t *buf_ptr; | |
273 | unsigned int state=0xFFFFFFFF, v; | |
274 | int val; | |
275 | ||
276 | buf_ptr = *pbuf_ptr; | |
277 | while (buf_ptr < buf_end) { | |
278 | v = *buf_ptr++; | |
279 | if (state == 0x000001) { | |
280 | state = ((state << 8) | v) & 0xffffff; | |
281 | val = state; | |
282 | goto found; | |
283 | } | |
284 | state = ((state << 8) | v) & 0xffffff; | |
285 | } | |
286 | val = -1; | |
287 | found: | |
288 | *pbuf_ptr = buf_ptr; | |
289 | return val; | |
290 | } | |
291 | ||
292 | /* XXX: merge with libavcodec ? */ | |
293 | #define MPEG1_FRAME_RATE_BASE 1001 | |
294 | ||
295 | static const int frame_rate_tab[16] = { | |
296 | 0, | |
297 | 24000, | |
298 | 24024, | |
299 | 25025, | |
300 | 30000, | |
301 | 30030, | |
302 | 50050, | |
303 | 60000, | |
304 | 60060, | |
305 | // Xing's 15fps: (9) | |
306 | 15015, | |
307 | // libmpeg3's "Unofficial economy rates": (10-13) | |
308 | 5005, | |
309 | 10010, | |
310 | 12012, | |
311 | 15015, | |
312 | // random, just to avoid segfault !never encode these | |
313 | 25025, | |
314 | 25025, | |
315 | }; | |
316 | ||
c0df9d75 | 317 | //FIXME move into mpeg12.c |
8424cf50 FB |
318 | static void mpegvideo_extract_headers(AVCodecParserContext *s, |
319 | AVCodecContext *avctx, | |
320 | const uint8_t *buf, int buf_size) | |
321 | { | |
322 | ParseContext1 *pc = s->priv_data; | |
323 | const uint8_t *buf_end; | |
324 | int32_t start_code; | |
325 | int frame_rate_index, ext_type, bytes_left; | |
326 | int frame_rate_ext_n, frame_rate_ext_d; | |
8dab64b6 | 327 | int picture_structure, top_field_first, repeat_first_field, progressive_frame; |
8c787559 | 328 | int horiz_size_ext, vert_size_ext, bit_rate_ext; |
90ad92b3 | 329 | //FIXME replace the crap with get_bits() |
8424cf50 FB |
330 | s->repeat_pict = 0; |
331 | buf_end = buf + buf_size; | |
332 | while (buf < buf_end) { | |
333 | start_code = find_start_code(&buf, buf_end); | |
334 | bytes_left = buf_end - buf; | |
335 | switch(start_code) { | |
336 | case PICTURE_START_CODE: | |
337 | if (bytes_left >= 2) { | |
338 | s->pict_type = (buf[1] >> 3) & 7; | |
339 | } | |
340 | break; | |
341 | case SEQ_START_CODE: | |
8c787559 | 342 | if (bytes_left >= 7) { |
0b2346d3 MN |
343 | pc->width = (buf[0] << 4) | (buf[1] >> 4); |
344 | pc->height = ((buf[1] & 0x0f) << 8) | buf[2]; | |
21adafec | 345 | avcodec_set_dimensions(avctx, pc->width, pc->height); |
8424cf50 | 346 | frame_rate_index = buf[3] & 0xf; |
c0df9d75 MN |
347 | pc->frame_rate = avctx->time_base.den = frame_rate_tab[frame_rate_index]; |
348 | avctx->time_base.num = MPEG1_FRAME_RATE_BASE; | |
8c787559 | 349 | avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400; |
3e9d718e FB |
350 | avctx->codec_id = CODEC_ID_MPEG1VIDEO; |
351 | avctx->sub_id = 1; | |
8424cf50 FB |
352 | } |
353 | break; | |
354 | case EXT_START_CODE: | |
355 | if (bytes_left >= 1) { | |
356 | ext_type = (buf[0] >> 4); | |
357 | switch(ext_type) { | |
358 | case 0x1: /* sequence extension */ | |
359 | if (bytes_left >= 6) { | |
360 | horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7); | |
361 | vert_size_ext = (buf[2] >> 5) & 3; | |
8c787559 | 362 | bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1); |
8424cf50 FB |
363 | frame_rate_ext_n = (buf[5] >> 5) & 3; |
364 | frame_rate_ext_d = (buf[5] & 0x1f); | |
365 | pc->progressive_sequence = buf[1] & (1 << 3); | |
8ca5d3bb | 366 | avctx->has_b_frames= !(buf[5] >> 7); |
8424cf50 | 367 | |
0b2346d3 MN |
368 | pc->width |=(horiz_size_ext << 12); |
369 | pc->height |=( vert_size_ext << 12); | |
8c787559 | 370 | avctx->bit_rate += (bit_rate_ext << 18) * 400; |
21adafec | 371 | avcodec_set_dimensions(avctx, pc->width, pc->height); |
c0df9d75 MN |
372 | avctx->time_base.den = pc->frame_rate * (frame_rate_ext_n + 1); |
373 | avctx->time_base.num = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1); | |
3e9d718e | 374 | avctx->codec_id = CODEC_ID_MPEG2VIDEO; |
8424cf50 FB |
375 | avctx->sub_id = 2; /* forces MPEG2 */ |
376 | } | |
377 | break; | |
378 | case 0x8: /* picture coding extension */ | |
379 | if (bytes_left >= 5) { | |
5bb994e2 | 380 | picture_structure = buf[2]&3; |
8424cf50 FB |
381 | top_field_first = buf[3] & (1 << 7); |
382 | repeat_first_field = buf[3] & (1 << 1); | |
383 | progressive_frame = buf[4] & (1 << 7); | |
384 | ||
385 | /* check if we must repeat the frame */ | |
386 | if (repeat_first_field) { | |
387 | if (pc->progressive_sequence) { | |
388 | if (top_field_first) | |
389 | s->repeat_pict = 4; | |
390 | else | |
391 | s->repeat_pict = 2; | |
392 | } else if (progressive_frame) { | |
393 | s->repeat_pict = 1; | |
394 | } | |
395 | } | |
8dab64b6 MN |
396 | |
397 | /* the packet only represents half a frame | |
398 | XXX,FIXME maybe find a different solution */ | |
399 | if(picture_structure != 3) | |
400 | s->repeat_pict = -1; | |
8424cf50 FB |
401 | } |
402 | break; | |
403 | } | |
404 | } | |
405 | break; | |
406 | case -1: | |
407 | goto the_end; | |
408 | default: | |
409 | /* we stop parsing when we encounter a slice. It ensures | |
410 | that this function takes a negligible amount of time */ | |
411 | if (start_code >= SLICE_MIN_START_CODE && | |
412 | start_code <= SLICE_MAX_START_CODE) | |
413 | goto the_end; | |
414 | break; | |
415 | } | |
416 | } | |
417 | the_end: ; | |
418 | } | |
419 | ||
420 | static int mpegvideo_parse(AVCodecParserContext *s, | |
421 | AVCodecContext *avctx, | |
422 | uint8_t **poutbuf, int *poutbuf_size, | |
423 | const uint8_t *buf, int buf_size) | |
424 | { | |
e4cb187d MN |
425 | ParseContext1 *pc1 = s->priv_data; |
426 | ParseContext *pc= &pc1->pc; | |
8424cf50 FB |
427 | int next; |
428 | ||
e4cb187d | 429 | next= ff_mpeg1_find_frame_end(pc, buf, buf_size); |
8424cf50 | 430 | |
e4cb187d | 431 | if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
8424cf50 FB |
432 | *poutbuf = NULL; |
433 | *poutbuf_size = 0; | |
434 | return buf_size; | |
435 | } | |
436 | /* we have a full frame : we just parse the first few MPEG headers | |
437 | to have the full timing information. The time take by this | |
438 | function should be negligible for uncorrupted streams */ | |
439 | mpegvideo_extract_headers(s, avctx, buf, buf_size); | |
440 | #if 0 | |
441 | printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", | |
c0df9d75 | 442 | s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict); |
8424cf50 FB |
443 | #endif |
444 | ||
445 | *poutbuf = (uint8_t *)buf; | |
446 | *poutbuf_size = buf_size; | |
447 | return next; | |
448 | } | |
449 | ||
90ad92b3 MN |
450 | static int mpegvideo_split(AVCodecContext *avctx, |
451 | const uint8_t *buf, int buf_size) | |
452 | { | |
453 | int i; | |
454 | uint32_t state= -1; | |
455 | ||
456 | for(i=0; i<buf_size; i++){ | |
457 | state= (state<<8) | buf[i]; | |
458 | if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) | |
459 | return i-4; | |
460 | } | |
461 | return 0; | |
462 | } | |
463 | ||
e4cb187d | 464 | void ff_parse_close(AVCodecParserContext *s) |
8424cf50 | 465 | { |
e4cb187d | 466 | ParseContext *pc = s->priv_data; |
8424cf50 FB |
467 | |
468 | av_free(pc->buffer); | |
8424cf50 FB |
469 | } |
470 | ||
e4cb187d | 471 | static void parse1_close(AVCodecParserContext *s) |
8424cf50 | 472 | { |
e4cb187d | 473 | ParseContext1 *pc1 = s->priv_data; |
8424cf50 | 474 | |
e4cb187d MN |
475 | av_free(pc1->pc.buffer); |
476 | av_free(pc1->enc); | |
8424cf50 FB |
477 | } |
478 | ||
e4cb187d MN |
479 | /*************************/ |
480 | ||
8424cf50 FB |
481 | /* used by parser */ |
482 | /* XXX: make it use less memory */ | |
483 | static int av_mpeg4_decode_header(AVCodecParserContext *s1, | |
484 | AVCodecContext *avctx, | |
485 | const uint8_t *buf, int buf_size) | |
486 | { | |
487 | ParseContext1 *pc = s1->priv_data; | |
488 | MpegEncContext *s = pc->enc; | |
489 | GetBitContext gb1, *gb = &gb1; | |
490 | int ret; | |
491 | ||
492 | s->avctx = avctx; | |
c6f353ff FB |
493 | s->current_picture_ptr = &s->current_picture; |
494 | ||
495 | if (avctx->extradata_size && pc->first_picture){ | |
496 | init_get_bits(gb, avctx->extradata, avctx->extradata_size*8); | |
497 | ret = ff_mpeg4_decode_picture_header(s, gb); | |
498 | } | |
499 | ||
8424cf50 FB |
500 | init_get_bits(gb, buf, 8 * buf_size); |
501 | ret = ff_mpeg4_decode_picture_header(s, gb); | |
502 | if (s->width) { | |
21adafec | 503 | avcodec_set_dimensions(avctx, s->width, s->height); |
8424cf50 | 504 | } |
c6f353ff | 505 | pc->first_picture = 0; |
8424cf50 FB |
506 | return ret; |
507 | } | |
508 | ||
e96682e6 | 509 | static int mpeg4video_parse_init(AVCodecParserContext *s) |
8424cf50 FB |
510 | { |
511 | ParseContext1 *pc = s->priv_data; | |
c6f353ff | 512 | |
8424cf50 FB |
513 | pc->enc = av_mallocz(sizeof(MpegEncContext)); |
514 | if (!pc->enc) | |
515 | return -1; | |
c6f353ff | 516 | pc->first_picture = 1; |
8424cf50 FB |
517 | return 0; |
518 | } | |
519 | ||
520 | static int mpeg4video_parse(AVCodecParserContext *s, | |
521 | AVCodecContext *avctx, | |
522 | uint8_t **poutbuf, int *poutbuf_size, | |
523 | const uint8_t *buf, int buf_size) | |
524 | { | |
e4cb187d | 525 | ParseContext *pc = s->priv_data; |
8424cf50 FB |
526 | int next; |
527 | ||
e4cb187d | 528 | next= ff_mpeg4_find_frame_end(pc, buf, buf_size); |
8424cf50 | 529 | |
e4cb187d | 530 | if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
8424cf50 FB |
531 | *poutbuf = NULL; |
532 | *poutbuf_size = 0; | |
533 | return buf_size; | |
534 | } | |
535 | av_mpeg4_decode_header(s, avctx, buf, buf_size); | |
536 | ||
537 | *poutbuf = (uint8_t *)buf; | |
538 | *poutbuf_size = buf_size; | |
539 | return next; | |
540 | } | |
541 | ||
90ad92b3 MN |
542 | static int mpeg4video_split(AVCodecContext *avctx, |
543 | const uint8_t *buf, int buf_size) | |
544 | { | |
545 | int i; | |
546 | uint32_t state= -1; | |
547 | ||
548 | for(i=0; i<buf_size; i++){ | |
549 | state= (state<<8) | buf[i]; | |
550 | if(state == 0x1B3 || state == 0x1B6) | |
551 | return i-4; | |
552 | } | |
553 | return 0; | |
554 | } | |
555 | ||
8424cf50 FB |
556 | /*************************/ |
557 | ||
8424cf50 FB |
558 | typedef struct MpegAudioParseContext { |
559 | uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */ | |
560 | uint8_t *inbuf_ptr; | |
561 | int frame_size; | |
562 | int free_format_frame_size; | |
563 | int free_format_next_header; | |
4b9ac0b5 MN |
564 | uint32_t header; |
565 | int header_count; | |
8424cf50 FB |
566 | } MpegAudioParseContext; |
567 | ||
568 | #define MPA_HEADER_SIZE 4 | |
569 | ||
570 | /* header + layer + bitrate + freq + lsf/mpeg25 */ | |
88730be6 | 571 | #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */ |
8424cf50 | 572 | #define SAME_HEADER_MASK \ |
e993bc03 | 573 | (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)) |
8424cf50 FB |
574 | |
575 | static int mpegaudio_parse_init(AVCodecParserContext *s1) | |
576 | { | |
577 | MpegAudioParseContext *s = s1->priv_data; | |
578 | s->inbuf_ptr = s->inbuf; | |
579 | return 0; | |
580 | } | |
581 | ||
582 | static int mpegaudio_parse(AVCodecParserContext *s1, | |
583 | AVCodecContext *avctx, | |
584 | uint8_t **poutbuf, int *poutbuf_size, | |
585 | const uint8_t *buf, int buf_size) | |
586 | { | |
587 | MpegAudioParseContext *s = s1->priv_data; | |
4b9ac0b5 | 588 | int len, ret, sr; |
8424cf50 FB |
589 | uint32_t header; |
590 | const uint8_t *buf_ptr; | |
591 | ||
592 | *poutbuf = NULL; | |
593 | *poutbuf_size = 0; | |
594 | buf_ptr = buf; | |
595 | while (buf_size > 0) { | |
596 | len = s->inbuf_ptr - s->inbuf; | |
597 | if (s->frame_size == 0) { | |
598 | /* special case for next header for first frame in free | |
599 | format case (XXX: find a simpler method) */ | |
600 | if (s->free_format_next_header != 0) { | |
601 | s->inbuf[0] = s->free_format_next_header >> 24; | |
602 | s->inbuf[1] = s->free_format_next_header >> 16; | |
603 | s->inbuf[2] = s->free_format_next_header >> 8; | |
604 | s->inbuf[3] = s->free_format_next_header; | |
605 | s->inbuf_ptr = s->inbuf + 4; | |
606 | s->free_format_next_header = 0; | |
607 | goto got_header; | |
608 | } | |
609 | /* no header seen : find one. We need at least MPA_HEADER_SIZE | |
610 | bytes to parse it */ | |
611 | len = MPA_HEADER_SIZE - len; | |
612 | if (len > buf_size) | |
613 | len = buf_size; | |
614 | if (len > 0) { | |
615 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
616 | buf_ptr += len; | |
617 | buf_size -= len; | |
618 | s->inbuf_ptr += len; | |
619 | } | |
620 | if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) { | |
621 | got_header: | |
4b9ac0b5 | 622 | sr= avctx->sample_rate; |
8424cf50 FB |
623 | header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | |
624 | (s->inbuf[2] << 8) | s->inbuf[3]; | |
625 | ||
626 | ret = mpa_decode_header(avctx, header); | |
627 | if (ret < 0) { | |
4b9ac0b5 | 628 | s->header_count= -2; |
8424cf50 FB |
629 | /* no sync found : move by one byte (inefficient, but simple!) */ |
630 | memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
631 | s->inbuf_ptr--; | |
632 | dprintf("skip %x\n", header); | |
633 | /* reset free format frame size to give a chance | |
634 | to get a new bitrate */ | |
635 | s->free_format_frame_size = 0; | |
636 | } else { | |
4b9ac0b5 MN |
637 | if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header) |
638 | s->header_count= -3; | |
639 | s->header= header; | |
640 | s->header_count++; | |
8424cf50 | 641 | s->frame_size = ret; |
4b9ac0b5 | 642 | |
8424cf50 FB |
643 | #if 0 |
644 | /* free format: prepare to compute frame size */ | |
645 | if (decode_header(s, header) == 1) { | |
646 | s->frame_size = -1; | |
647 | } | |
648 | #endif | |
649 | } | |
4b9ac0b5 MN |
650 | if(s->header_count <= 0) |
651 | avctx->sample_rate= sr; //FIXME ugly | |
8424cf50 FB |
652 | } |
653 | } else | |
654 | #if 0 | |
655 | if (s->frame_size == -1) { | |
656 | /* free format : find next sync to compute frame size */ | |
657 | len = MPA_MAX_CODED_FRAME_SIZE - len; | |
658 | if (len > buf_size) | |
659 | len = buf_size; | |
660 | if (len == 0) { | |
661 | /* frame too long: resync */ | |
662 | s->frame_size = 0; | |
663 | memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
664 | s->inbuf_ptr--; | |
665 | } else { | |
666 | uint8_t *p, *pend; | |
667 | uint32_t header1; | |
668 | int padding; | |
669 | ||
670 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
671 | /* check for header */ | |
672 | p = s->inbuf_ptr - 3; | |
673 | pend = s->inbuf_ptr + len - 4; | |
674 | while (p <= pend) { | |
675 | header = (p[0] << 24) | (p[1] << 16) | | |
676 | (p[2] << 8) | p[3]; | |
677 | header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
678 | (s->inbuf[2] << 8) | s->inbuf[3]; | |
679 | /* check with high probability that we have a | |
680 | valid header */ | |
681 | if ((header & SAME_HEADER_MASK) == | |
682 | (header1 & SAME_HEADER_MASK)) { | |
683 | /* header found: update pointers */ | |
684 | len = (p + 4) - s->inbuf_ptr; | |
685 | buf_ptr += len; | |
686 | buf_size -= len; | |
687 | s->inbuf_ptr = p; | |
688 | /* compute frame size */ | |
689 | s->free_format_next_header = header; | |
690 | s->free_format_frame_size = s->inbuf_ptr - s->inbuf; | |
691 | padding = (header1 >> 9) & 1; | |
692 | if (s->layer == 1) | |
693 | s->free_format_frame_size -= padding * 4; | |
694 | else | |
695 | s->free_format_frame_size -= padding; | |
696 | dprintf("free frame size=%d padding=%d\n", | |
697 | s->free_format_frame_size, padding); | |
698 | decode_header(s, header1); | |
699 | goto next_data; | |
700 | } | |
701 | p++; | |
702 | } | |
703 | /* not found: simply increase pointers */ | |
704 | buf_ptr += len; | |
705 | s->inbuf_ptr += len; | |
706 | buf_size -= len; | |
707 | } | |
708 | } else | |
709 | #endif | |
710 | if (len < s->frame_size) { | |
711 | if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) | |
712 | s->frame_size = MPA_MAX_CODED_FRAME_SIZE; | |
713 | len = s->frame_size - len; | |
714 | if (len > buf_size) | |
715 | len = buf_size; | |
716 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
717 | buf_ptr += len; | |
718 | s->inbuf_ptr += len; | |
719 | buf_size -= len; | |
720 | } | |
721 | // next_data: | |
722 | if (s->frame_size > 0 && | |
723 | (s->inbuf_ptr - s->inbuf) >= s->frame_size) { | |
4b9ac0b5 MN |
724 | if(s->header_count > 0){ |
725 | *poutbuf = s->inbuf; | |
726 | *poutbuf_size = s->inbuf_ptr - s->inbuf; | |
727 | } | |
8424cf50 FB |
728 | s->inbuf_ptr = s->inbuf; |
729 | s->frame_size = 0; | |
730 | break; | |
731 | } | |
732 | } | |
733 | return buf_ptr - buf; | |
734 | } | |
735 | ||
736 | #ifdef CONFIG_AC3 | |
737 | extern int a52_syncinfo (const uint8_t * buf, int * flags, | |
738 | int * sample_rate, int * bit_rate); | |
739 | ||
740 | typedef struct AC3ParseContext { | |
741 | uint8_t inbuf[4096]; /* input buffer */ | |
742 | uint8_t *inbuf_ptr; | |
743 | int frame_size; | |
744 | int flags; | |
745 | } AC3ParseContext; | |
746 | ||
747 | #define AC3_HEADER_SIZE 7 | |
748 | #define A52_LFE 16 | |
749 | ||
750 | static int ac3_parse_init(AVCodecParserContext *s1) | |
751 | { | |
752 | AC3ParseContext *s = s1->priv_data; | |
753 | s->inbuf_ptr = s->inbuf; | |
754 | return 0; | |
755 | } | |
756 | ||
757 | static int ac3_parse(AVCodecParserContext *s1, | |
758 | AVCodecContext *avctx, | |
759 | uint8_t **poutbuf, int *poutbuf_size, | |
760 | const uint8_t *buf, int buf_size) | |
761 | { | |
762 | AC3ParseContext *s = s1->priv_data; | |
763 | const uint8_t *buf_ptr; | |
764 | int len, sample_rate, bit_rate; | |
765 | static const int ac3_channels[8] = { | |
766 | 2, 1, 2, 3, 3, 4, 4, 5 | |
767 | }; | |
768 | ||
769 | *poutbuf = NULL; | |
770 | *poutbuf_size = 0; | |
771 | ||
772 | buf_ptr = buf; | |
773 | while (buf_size > 0) { | |
774 | len = s->inbuf_ptr - s->inbuf; | |
775 | if (s->frame_size == 0) { | |
776 | /* no header seen : find one. We need at least 7 bytes to parse it */ | |
777 | len = AC3_HEADER_SIZE - len; | |
778 | if (len > buf_size) | |
779 | len = buf_size; | |
780 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
781 | buf_ptr += len; | |
782 | s->inbuf_ptr += len; | |
783 | buf_size -= len; | |
784 | if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) { | |
785 | len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate); | |
786 | if (len == 0) { | |
787 | /* no sync found : move by one byte (inefficient, but simple!) */ | |
788 | memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1); | |
789 | s->inbuf_ptr--; | |
790 | } else { | |
791 | s->frame_size = len; | |
792 | /* update codec info */ | |
793 | avctx->sample_rate = sample_rate; | |
20da3179 MN |
794 | /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */ |
795 | if(avctx->channels!=1 && avctx->channels!=2){ | |
796 | avctx->channels = ac3_channels[s->flags & 7]; | |
797 | if (s->flags & A52_LFE) | |
798 | avctx->channels++; | |
799 | } | |
8424cf50 FB |
800 | avctx->bit_rate = bit_rate; |
801 | avctx->frame_size = 6 * 256; | |
802 | } | |
803 | } | |
804 | } else if (len < s->frame_size) { | |
805 | len = s->frame_size - len; | |
806 | if (len > buf_size) | |
807 | len = buf_size; | |
808 | ||
809 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
810 | buf_ptr += len; | |
811 | s->inbuf_ptr += len; | |
812 | buf_size -= len; | |
813 | } else { | |
814 | *poutbuf = s->inbuf; | |
815 | *poutbuf_size = s->frame_size; | |
816 | s->inbuf_ptr = s->inbuf; | |
817 | s->frame_size = 0; | |
818 | break; | |
819 | } | |
820 | } | |
821 | return buf_ptr - buf; | |
822 | } | |
823 | #endif | |
824 | ||
825 | AVCodecParser mpegvideo_parser = { | |
826 | { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO }, | |
827 | sizeof(ParseContext1), | |
828 | NULL, | |
829 | mpegvideo_parse, | |
e4cb187d | 830 | parse1_close, |
90ad92b3 | 831 | mpegvideo_split, |
8424cf50 FB |
832 | }; |
833 | ||
834 | AVCodecParser mpeg4video_parser = { | |
835 | { CODEC_ID_MPEG4 }, | |
836 | sizeof(ParseContext1), | |
837 | mpeg4video_parse_init, | |
838 | mpeg4video_parse, | |
e4cb187d | 839 | parse1_close, |
90ad92b3 | 840 | mpeg4video_split, |
8424cf50 FB |
841 | }; |
842 | ||
843 | AVCodecParser mpegaudio_parser = { | |
844 | { CODEC_ID_MP2, CODEC_ID_MP3 }, | |
845 | sizeof(MpegAudioParseContext), | |
846 | mpegaudio_parse_init, | |
847 | mpegaudio_parse, | |
848 | NULL, | |
849 | }; | |
850 | ||
851 | #ifdef CONFIG_AC3 | |
852 | AVCodecParser ac3_parser = { | |
853 | { CODEC_ID_AC3 }, | |
854 | sizeof(AC3ParseContext), | |
855 | ac3_parse_init, | |
856 | ac3_parse, | |
857 | NULL, | |
858 | }; | |
859 | #endif |