|
808
|
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 #include "avcodec.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;
|
|
|
37
|
|
|
38 if(codec_id == CODEC_ID_NONE)
|
|
|
39 return NULL;
|
|
|
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 ||
|
|
|
44 parser->codec_ids[2] == codec_id ||
|
|
|
45 parser->codec_ids[3] == codec_id ||
|
|
|
46 parser->codec_ids[4] == codec_id)
|
|
|
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 }
|
|
|
68 s->fetch_timestamp=1;
|
|
|
69 return s;
|
|
|
70 }
|
|
|
71
|
|
|
72 /**
|
|
|
73 *
|
|
|
74 * @param buf input
|
|
|
75 * @param buf_size input length, to signal EOF, this should be 0 (so that the last frame can be output)
|
|
|
76 * @param pts input presentation timestamp
|
|
|
77 * @param dts input decoding timestamp
|
|
|
78 * @param poutbuf will contain a pointer to the first byte of the output frame
|
|
|
79 * @param poutbuf_size will contain the length of the output frame
|
|
|
80 * @return the number of bytes of the input bitstream used
|
|
|
81 *
|
|
|
82 * Example:
|
|
|
83 * @code
|
|
|
84 * while(in_len){
|
|
|
85 * len = av_parser_parse(myparser, AVCodecContext, &data, &size,
|
|
|
86 * in_data, in_len,
|
|
|
87 * pts, dts);
|
|
|
88 * in_data += len;
|
|
|
89 * in_len -= len;
|
|
|
90 *
|
|
|
91 * decode_frame(data, size);
|
|
|
92 * }
|
|
|
93 * @endcode
|
|
|
94 */
|
|
|
95 int av_parser_parse(AVCodecParserContext *s,
|
|
|
96 AVCodecContext *avctx,
|
|
|
97 uint8_t **poutbuf, int *poutbuf_size,
|
|
|
98 const uint8_t *buf, int buf_size,
|
|
|
99 int64_t pts, int64_t dts)
|
|
|
100 {
|
|
|
101 int index, i, k;
|
|
|
102 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
|
|
|
103
|
|
|
104 if (buf_size == 0) {
|
|
|
105 /* padding is always necessary even if EOF, so we add it here */
|
|
|
106 memset(dummy_buf, 0, sizeof(dummy_buf));
|
|
|
107 buf = dummy_buf;
|
|
|
108 } else {
|
|
|
109 /* add a new packet descriptor */
|
|
|
110 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
|
|
|
111 s->cur_frame_start_index = k;
|
|
|
112 s->cur_frame_offset[k] = s->cur_offset;
|
|
|
113 s->cur_frame_pts[k] = pts;
|
|
|
114 s->cur_frame_dts[k] = dts;
|
|
|
115
|
|
|
116 /* fill first PTS/DTS */
|
|
|
117 if (s->fetch_timestamp){
|
|
|
118 s->fetch_timestamp=0;
|
|
|
119 s->last_pts = pts;
|
|
|
120 s->last_dts = dts;
|
|
|
121 s->cur_frame_pts[k] =
|
|
|
122 s->cur_frame_dts[k] = AV_NOPTS_VALUE;
|
|
|
123 }
|
|
|
124 }
|
|
|
125
|
|
|
126 /* WARNING: the returned index can be negative */
|
|
|
127 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
|
|
|
128 //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);
|
|
|
129 /* update the file pointer */
|
|
|
130 if (*poutbuf_size) {
|
|
|
131 /* fill the data for the current frame */
|
|
|
132 s->frame_offset = s->last_frame_offset;
|
|
|
133 s->pts = s->last_pts;
|
|
|
134 s->dts = s->last_dts;
|
|
|
135
|
|
|
136 /* offset of the next frame */
|
|
|
137 s->last_frame_offset = s->cur_offset + index;
|
|
|
138 /* find the packet in which the new frame starts. It
|
|
|
139 is tricky because of MPEG video start codes
|
|
|
140 which can begin in one packet and finish in
|
|
|
141 another packet. In the worst case, an MPEG
|
|
|
142 video start code could be in 4 different
|
|
|
143 packets. */
|
|
|
144 k = s->cur_frame_start_index;
|
|
|
145 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
|
|
|
146 if (s->last_frame_offset >= s->cur_frame_offset[k])
|
|
|
147 break;
|
|
|
148 k = (k - 1) & (AV_PARSER_PTS_NB - 1);
|
|
|
149 }
|
|
|
150
|
|
|
151 s->last_pts = s->cur_frame_pts[k];
|
|
|
152 s->last_dts = s->cur_frame_dts[k];
|
|
|
153
|
|
|
154 /* some parsers tell us the packet size even before seeing the first byte of the next packet,
|
|
|
155 so the next pts/dts is in the next chunk */
|
|
|
156 if(index == buf_size){
|
|
|
157 s->fetch_timestamp=1;
|
|
|
158 }
|
|
|
159 }
|
|
|
160 if (index < 0)
|
|
|
161 index = 0;
|
|
|
162 s->cur_offset += index;
|
|
|
163 return index;
|
|
|
164 }
|
|
|
165
|
|
|
166 /**
|
|
|
167 *
|
|
|
168 * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed
|
|
|
169 * @deprecated use AVBitstreamFilter
|
|
|
170 */
|
|
|
171 int av_parser_change(AVCodecParserContext *s,
|
|
|
172 AVCodecContext *avctx,
|
|
|
173 uint8_t **poutbuf, int *poutbuf_size,
|
|
|
174 const uint8_t *buf, int buf_size, int keyframe){
|
|
|
175
|
|
|
176 if(s && s->parser->split){
|
|
|
177 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
|
|
|
178 int i= s->parser->split(avctx, buf, buf_size);
|
|
|
179 buf += i;
|
|
|
180 buf_size -= i;
|
|
|
181 }
|
|
|
182 }
|
|
|
183
|
|
|
184 /* cast to avoid warning about discarding qualifiers */
|
|
|
185 *poutbuf= (uint8_t *) buf;
|
|
|
186 *poutbuf_size= buf_size;
|
|
|
187 if(avctx->extradata){
|
|
|
188 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
|
|
|
189 /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
|
|
|
190 /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
|
|
|
191 int size= buf_size + avctx->extradata_size;
|
|
|
192 *poutbuf_size= size;
|
|
|
193 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
194
|
|
|
195 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
|
|
|
196 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
197 return 1;
|
|
|
198 }
|
|
|
199 }
|
|
|
200
|
|
|
201 return 0;
|
|
|
202 }
|
|
|
203
|
|
|
204 void av_parser_close(AVCodecParserContext *s)
|
|
|
205 {
|
|
|
206 if (s->parser->parser_close)
|
|
|
207 s->parser->parser_close(s);
|
|
|
208 av_free(s->priv_data);
|
|
|
209 av_free(s);
|
|
|
210 }
|
|
|
211
|
|
|
212 /*****************************************************/
|
|
|
213
|
|
|
214 //#define END_NOT_FOUND (-100)
|
|
|
215
|
|
|
216 #define PICTURE_START_CODE 0x00000100
|
|
|
217 #define SEQ_START_CODE 0x000001b3
|
|
|
218 #define EXT_START_CODE 0x000001b5
|
|
|
219 #define SLICE_MIN_START_CODE 0x00000101
|
|
|
220 #define SLICE_MAX_START_CODE 0x000001af
|
|
|
221
|
|
|
222 typedef struct ParseContext1{
|
|
|
223 ParseContext pc;
|
|
|
224 /* XXX/FIXME PC1 vs. PC */
|
|
|
225 /* MPEG2 specific */
|
|
|
226 int frame_rate;
|
|
|
227 int progressive_sequence;
|
|
|
228 int width, height;
|
|
|
229
|
|
|
230 /* XXX: suppress that, needed by MPEG4 */
|
|
|
231 MpegEncContext *enc;
|
|
|
232 int first_picture;
|
|
|
233 } ParseContext1;
|
|
|
234
|
|
|
235 /**
|
|
|
236 * combines the (truncated) bitstream to a complete frame
|
|
|
237 * @returns -1 if no complete frame could be created
|
|
|
238 */
|
|
|
239 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
|
|
|
240 {
|
|
|
241 #if 0
|
|
|
242 if(pc->overread){
|
|
|
243 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
|
|
|
244 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
|
|
|
245 }
|
|
|
246 #endif
|
|
|
247
|
|
|
248 /* copy overreaded bytes from last frame into buffer */
|
|
|
249 for(; pc->overread>0; pc->overread--){
|
|
|
250 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
|
|
|
251 }
|
|
|
252
|
|
|
253 /* flush remaining if EOF */
|
|
|
254 if(!*buf_size && next == END_NOT_FOUND){
|
|
|
255 next= 0;
|
|
|
256 }
|
|
|
257
|
|
|
258 pc->last_index= pc->index;
|
|
|
259
|
|
|
260 /* copy into buffer end return */
|
|
|
261 if(next == END_NOT_FOUND){
|
|
|
262 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
263
|
|
|
264 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
|
|
|
265 pc->index += *buf_size;
|
|
|
266 return -1;
|
|
|
267 }
|
|
|
268
|
|
|
269 *buf_size=
|
|
|
270 pc->overread_index= pc->index + next;
|
|
|
271
|
|
|
272 /* append to buffer */
|
|
|
273 if(pc->index){
|
|
|
274 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
275
|
|
|
276 memcpy(&pc->buffer[pc->index], *buf, 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->overread++;
|
|
|
285 }
|
|
|
286
|
|
|
287 #if 0
|
|
|
288 if(pc->overread){
|
|
|
289 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
|
|
|
290 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
|
|
|
291 }
|
|
|
292 #endif
|
|
|
293
|
|
|
294 return 0;
|
|
|
295 }
|
|
|
296
|
|
|
297 /* XXX: merge with libavcodec ? */
|
|
|
298 #define MPEG1_FRAME_RATE_BASE 1001
|
|
|
299
|
|
|
300 static const int frame_rate_tab[16] = {
|
|
|
301 0,
|
|
|
302 24000,
|
|
|
303 24024,
|
|
|
304 25025,
|
|
|
305 30000,
|
|
|
306 30030,
|
|
|
307 50050,
|
|
|
308 60000,
|
|
|
309 60060,
|
|
|
310 // Xing's 15fps: (9)
|
|
|
311 15015,
|
|
|
312 // libmpeg3's "Unofficial economy rates": (10-13)
|
|
|
313 5005,
|
|
|
314 10010,
|
|
|
315 12012,
|
|
|
316 15015,
|
|
|
317 // random, just to avoid segfault !never encode these
|
|
|
318 25025,
|
|
|
319 25025,
|
|
|
320 };
|
|
|
321
|
|
|
322 #ifdef CONFIG_MPEGVIDEO_PARSER
|
|
|
323 //FIXME move into mpeg12.c
|
|
|
324 static void mpegvideo_extract_headers(AVCodecParserContext *s,
|
|
|
325 AVCodecContext *avctx,
|
|
|
326 const uint8_t *buf, int buf_size)
|
|
|
327 {
|
|
|
328 ParseContext1 *pc = s->priv_data;
|
|
|
329 const uint8_t *buf_end;
|
|
|
330 uint32_t start_code;
|
|
|
331 int frame_rate_index, ext_type, bytes_left;
|
|
|
332 int frame_rate_ext_n, frame_rate_ext_d;
|
|
|
333 int picture_structure, top_field_first, repeat_first_field, progressive_frame;
|
|
|
334 int horiz_size_ext, vert_size_ext, bit_rate_ext;
|
|
|
335 //FIXME replace the crap with get_bits()
|
|
|
336 s->repeat_pict = 0;
|
|
|
337 buf_end = buf + buf_size;
|
|
|
338 while (buf < buf_end) {
|
|
|
339 start_code= -1;
|
|
|
340 buf= ff_find_start_code(buf, buf_end, &start_code);
|
|
|
341 bytes_left = buf_end - buf;
|
|
|
342 switch(start_code) {
|
|
|
343 case PICTURE_START_CODE:
|
|
|
344 if (bytes_left >= 2) {
|
|
|
345 s->pict_type = (buf[1] >> 3) & 7;
|
|
|
346 }
|
|
|
347 break;
|
|
|
348 case SEQ_START_CODE:
|
|
|
349 if (bytes_left >= 7) {
|
|
|
350 pc->width = (buf[0] << 4) | (buf[1] >> 4);
|
|
|
351 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
|
|
|
352 avcodec_set_dimensions(avctx, pc->width, pc->height);
|
|
|
353 frame_rate_index = buf[3] & 0xf;
|
|
|
354 pc->frame_rate = avctx->time_base.den = frame_rate_tab[frame_rate_index];
|
|
|
355 avctx->time_base.num = MPEG1_FRAME_RATE_BASE;
|
|
|
356 avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
|
|
|
357 avctx->codec_id = CODEC_ID_MPEG1VIDEO;
|
|
|
358 avctx->sub_id = 1;
|
|
|
359 }
|
|
|
360 break;
|
|
|
361 case EXT_START_CODE:
|
|
|
362 if (bytes_left >= 1) {
|
|
|
363 ext_type = (buf[0] >> 4);
|
|
|
364 switch(ext_type) {
|
|
|
365 case 0x1: /* sequence extension */
|
|
|
366 if (bytes_left >= 6) {
|
|
|
367 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
|
|
|
368 vert_size_ext = (buf[2] >> 5) & 3;
|
|
|
369 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
|
|
|
370 frame_rate_ext_n = (buf[5] >> 5) & 3;
|
|
|
371 frame_rate_ext_d = (buf[5] & 0x1f);
|
|
|
372 pc->progressive_sequence = buf[1] & (1 << 3);
|
|
|
373 avctx->has_b_frames= !(buf[5] >> 7);
|
|
|
374
|
|
|
375 pc->width |=(horiz_size_ext << 12);
|
|
|
376 pc->height |=( vert_size_ext << 12);
|
|
|
377 avctx->bit_rate += (bit_rate_ext << 18) * 400;
|
|
|
378 avcodec_set_dimensions(avctx, pc->width, pc->height);
|
|
|
379 avctx->time_base.den = pc->frame_rate * (frame_rate_ext_n + 1);
|
|
|
380 avctx->time_base.num = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
|
|
|
381 avctx->codec_id = CODEC_ID_MPEG2VIDEO;
|
|
|
382 avctx->sub_id = 2; /* forces MPEG2 */
|
|
|
383 }
|
|
|
384 break;
|
|
|
385 case 0x8: /* picture coding extension */
|
|
|
386 if (bytes_left >= 5) {
|
|
|
387 picture_structure = buf[2]&3;
|
|
|
388 top_field_first = buf[3] & (1 << 7);
|
|
|
389 repeat_first_field = buf[3] & (1 << 1);
|
|
|
390 progressive_frame = buf[4] & (1 << 7);
|
|
|
391
|
|
|
392 /* check if we must repeat the frame */
|
|
|
393 if (repeat_first_field) {
|
|
|
394 if (pc->progressive_sequence) {
|
|
|
395 if (top_field_first)
|
|
|
396 s->repeat_pict = 4;
|
|
|
397 else
|
|
|
398 s->repeat_pict = 2;
|
|
|
399 } else if (progressive_frame) {
|
|
|
400 s->repeat_pict = 1;
|
|
|
401 }
|
|
|
402 }
|
|
|
403
|
|
|
404 /* the packet only represents half a frame
|
|
|
405 XXX,FIXME maybe find a different solution */
|
|
|
406 if(picture_structure != 3)
|
|
|
407 s->repeat_pict = -1;
|
|
|
408 }
|
|
|
409 break;
|
|
|
410 }
|
|
|
411 }
|
|
|
412 break;
|
|
|
413 case -1:
|
|
|
414 goto the_end;
|
|
|
415 default:
|
|
|
416 /* we stop parsing when we encounter a slice. It ensures
|
|
|
417 that this function takes a negligible amount of time */
|
|
|
418 if (start_code >= SLICE_MIN_START_CODE &&
|
|
|
419 start_code <= SLICE_MAX_START_CODE)
|
|
|
420 goto the_end;
|
|
|
421 break;
|
|
|
422 }
|
|
|
423 }
|
|
|
424 the_end: ;
|
|
|
425 }
|
|
|
426
|
|
|
427 static int mpegvideo_parse(AVCodecParserContext *s,
|
|
|
428 AVCodecContext *avctx,
|
|
|
429 uint8_t **poutbuf, int *poutbuf_size,
|
|
|
430 const uint8_t *buf, int buf_size)
|
|
|
431 {
|
|
|
432 ParseContext1 *pc1 = s->priv_data;
|
|
|
433 ParseContext *pc= &pc1->pc;
|
|
|
434 int next;
|
|
|
435
|
|
|
436 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
|
|
|
437 next= buf_size;
|
|
|
438 }else{
|
|
|
439 next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
|
|
|
440
|
|
|
441 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
|
|
|
442 *poutbuf = NULL;
|
|
|
443 *poutbuf_size = 0;
|
|
|
444 return buf_size;
|
|
|
445 }
|
|
|
446
|
|
|
447 }
|
|
|
448 /* we have a full frame : we just parse the first few MPEG headers
|
|
|
449 to have the full timing information. The time take by this
|
|
|
450 function should be negligible for uncorrupted streams */
|
|
|
451 mpegvideo_extract_headers(s, avctx, buf, buf_size);
|
|
|
452 #if 0
|
|
|
453 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
|
|
|
454 s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
|
|
|
455 #endif
|
|
|
456
|
|
|
457 *poutbuf = (uint8_t *)buf;
|
|
|
458 *poutbuf_size = buf_size;
|
|
|
459 return next;
|
|
|
460 }
|
|
|
461
|
|
|
462 static int mpegvideo_split(AVCodecContext *avctx,
|
|
|
463 const uint8_t *buf, int buf_size)
|
|
|
464 {
|
|
|
465 int i;
|
|
|
466 uint32_t state= -1;
|
|
|
467
|
|
|
468 for(i=0; i<buf_size; i++){
|
|
|
469 state= (state<<8) | buf[i];
|
|
|
470 if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
|
|
|
471 return i-3;
|
|
|
472 }
|
|
|
473 return 0;
|
|
|
474 }
|
|
|
475 #endif /* CONFIG_MPEGVIDEO_PARSER */
|
|
|
476
|
|
|
477 void ff_parse_close(AVCodecParserContext *s)
|
|
|
478 {
|
|
|
479 ParseContext *pc = s->priv_data;
|
|
|
480
|
|
|
481 av_free(pc->buffer);
|
|
|
482 }
|
|
|
483
|
|
|
484 static void parse1_close(AVCodecParserContext *s)
|
|
|
485 {
|
|
|
486 ParseContext1 *pc1 = s->priv_data;
|
|
|
487
|
|
|
488 av_free(pc1->pc.buffer);
|
|
|
489 av_free(pc1->enc);
|
|
|
490 }
|
|
|
491
|
|
|
492 /*************************/
|
|
|
493
|
|
|
494 #ifdef CONFIG_MPEG4VIDEO_PARSER
|
|
|
495 /* used by parser */
|
|
|
496 /* XXX: make it use less memory */
|
|
|
497 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
|
|
|
498 AVCodecContext *avctx,
|
|
|
499 const uint8_t *buf, int buf_size)
|
|
|
500 {
|
|
|
501 ParseContext1 *pc = s1->priv_data;
|
|
|
502 MpegEncContext *s = pc->enc;
|
|
|
503 GetBitContext gb1, *gb = &gb1;
|
|
|
504 int ret;
|
|
|
505
|
|
|
506 s->avctx = avctx;
|
|
|
507 s->current_picture_ptr = &s->current_picture;
|
|
|
508
|
|
|
509 if (avctx->extradata_size && pc->first_picture){
|
|
|
510 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
|
|
|
511 ret = ff_mpeg4_decode_picture_header(s, gb);
|
|
|
512 }
|
|
|
513
|
|
|
514 init_get_bits(gb, buf, 8 * buf_size);
|
|
|
515 ret = ff_mpeg4_decode_picture_header(s, gb);
|
|
|
516 if (s->width) {
|
|
|
517 avcodec_set_dimensions(avctx, s->width, s->height);
|
|
|
518 }
|
|
|
519 s1->pict_type= s->pict_type;
|
|
|
520 pc->first_picture = 0;
|
|
|
521 return ret;
|
|
|
522 }
|
|
|
523
|
|
|
524 static int mpeg4video_parse_init(AVCodecParserContext *s)
|
|
|
525 {
|
|
|
526 ParseContext1 *pc = s->priv_data;
|
|
|
527
|
|
|
528 pc->enc = av_mallocz(sizeof(MpegEncContext));
|
|
|
529 if (!pc->enc)
|
|
|
530 return -1;
|
|
|
531 pc->first_picture = 1;
|
|
|
532 return 0;
|
|
|
533 }
|
|
|
534
|
|
|
535 static int mpeg4video_parse(AVCodecParserContext *s,
|
|
|
536 AVCodecContext *avctx,
|
|
|
537 uint8_t **poutbuf, int *poutbuf_size,
|
|
|
538 const uint8_t *buf, int buf_size)
|
|
|
539 {
|
|
|
540 ParseContext *pc = s->priv_data;
|
|
|
541 int next;
|
|
|
542
|
|
|
543 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
|
|
|
544 next= buf_size;
|
|
|
545 }else{
|
|
|
546 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
|
|
|
547
|
|
|
548 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
|
|
|
549 *poutbuf = NULL;
|
|
|
550 *poutbuf_size = 0;
|
|
|
551 return buf_size;
|
|
|
552 }
|
|
|
553 }
|
|
|
554 av_mpeg4_decode_header(s, avctx, buf, buf_size);
|
|
|
555
|
|
|
556 *poutbuf = (uint8_t *)buf;
|
|
|
557 *poutbuf_size = buf_size;
|
|
|
558 return next;
|
|
|
559 }
|
|
|
560 #endif
|
|
|
561
|
|
|
562 #ifdef CONFIG_CAVSVIDEO_PARSER
|
|
|
563 static int cavsvideo_parse(AVCodecParserContext *s,
|
|
|
564 AVCodecContext *avctx,
|
|
|
565 uint8_t **poutbuf, int *poutbuf_size,
|
|
|
566 const uint8_t *buf, int buf_size)
|
|
|
567 {
|
|
|
568 ParseContext *pc = s->priv_data;
|
|
|
569 int next;
|
|
|
570
|
|
|
571 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
|
|
|
572 next= buf_size;
|
|
|
573 }else{
|
|
|
574 next= ff_cavs_find_frame_end(pc, buf, buf_size);
|
|
|
575
|
|
|
576 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
|
|
|
577 *poutbuf = NULL;
|
|
|
578 *poutbuf_size = 0;
|
|
|
579 return buf_size;
|
|
|
580 }
|
|
|
581 }
|
|
|
582 *poutbuf = (uint8_t *)buf;
|
|
|
583 *poutbuf_size = buf_size;
|
|
|
584 return next;
|
|
|
585 }
|
|
|
586 #endif /* CONFIG_CAVSVIDEO_PARSER */
|
|
|
587
|
|
|
588 static int mpeg4video_split(AVCodecContext *avctx,
|
|
|
589 const uint8_t *buf, int buf_size)
|
|
|
590 {
|
|
|
591 int i;
|
|
|
592 uint32_t state= -1;
|
|
|
593
|
|
|
594 for(i=0; i<buf_size; i++){
|
|
|
595 state= (state<<8) | buf[i];
|
|
|
596 if(state == 0x1B3 || state == 0x1B6)
|
|
|
597 return i-3;
|
|
|
598 }
|
|
|
599 return 0;
|
|
|
600 }
|
|
|
601
|
|
|
602 /*************************/
|
|
|
603
|
|
|
604 #ifdef CONFIG_MPEGAUDIO_PARSER
|
|
|
605 typedef struct MpegAudioParseContext {
|
|
|
606 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
|
|
|
607 uint8_t *inbuf_ptr;
|
|
|
608 int frame_size;
|
|
|
609 int free_format_frame_size;
|
|
|
610 int free_format_next_header;
|
|
|
611 uint32_t header;
|
|
|
612 int header_count;
|
|
|
613 } MpegAudioParseContext;
|
|
|
614
|
|
|
615 #define MPA_HEADER_SIZE 4
|
|
|
616
|
|
|
617 /* header + layer + bitrate + freq + lsf/mpeg25 */
|
|
|
618 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
|
|
|
619 #define SAME_HEADER_MASK \
|
|
|
620 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
|
|
|
621
|
|
|
622 static int mpegaudio_parse_init(AVCodecParserContext *s1)
|
|
|
623 {
|
|
|
624 MpegAudioParseContext *s = s1->priv_data;
|
|
|
625 s->inbuf_ptr = s->inbuf;
|
|
|
626 return 0;
|
|
|
627 }
|
|
|
628
|
|
|
629 static int mpegaudio_parse(AVCodecParserContext *s1,
|
|
|
630 AVCodecContext *avctx,
|
|
|
631 uint8_t **poutbuf, int *poutbuf_size,
|
|
|
632 const uint8_t *buf, int buf_size)
|
|
|
633 {
|
|
|
634 MpegAudioParseContext *s = s1->priv_data;
|
|
|
635 int len, ret, sr;
|
|
|
636 uint32_t header;
|
|
|
637 const uint8_t *buf_ptr;
|
|
|
638
|
|
|
639 *poutbuf = NULL;
|
|
|
640 *poutbuf_size = 0;
|
|
|
641 buf_ptr = buf;
|
|
|
642 while (buf_size > 0) {
|
|
|
643 len = s->inbuf_ptr - s->inbuf;
|
|
|
644 if (s->frame_size == 0) {
|
|
|
645 /* special case for next header for first frame in free
|
|
|
646 format case (XXX: find a simpler method) */
|
|
|
647 if (s->free_format_next_header != 0) {
|
|
|
648 s->inbuf[0] = s->free_format_next_header >> 24;
|
|
|
649 s->inbuf[1] = s->free_format_next_header >> 16;
|
|
|
650 s->inbuf[2] = s->free_format_next_header >> 8;
|
|
|
651 s->inbuf[3] = s->free_format_next_header;
|
|
|
652 s->inbuf_ptr = s->inbuf + 4;
|
|
|
653 s->free_format_next_header = 0;
|
|
|
654 goto got_header;
|
|
|
655 }
|
|
|
656 /* no header seen : find one. We need at least MPA_HEADER_SIZE
|
|
|
657 bytes to parse it */
|
|
|
658 len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
|
|
|
659 if (len > 0) {
|
|
|
660 memcpy(s->inbuf_ptr, buf_ptr, len);
|
|
|
661 buf_ptr += len;
|
|
|
662 buf_size -= len;
|
|
|
663 s->inbuf_ptr += len;
|
|
|
664 }
|
|
|
665 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
|
|
|
666 got_header:
|
|
|
667 sr= avctx->sample_rate;
|
|
|
668 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
|
|
|
669 (s->inbuf[2] << 8) | s->inbuf[3];
|
|
|
670
|
|
|
671 ret = mpa_decode_header(avctx, header);
|
|
|
672 if (ret < 0) {
|
|
|
673 s->header_count= -2;
|
|
|
674 /* no sync found : move by one byte (inefficient, but simple!) */
|
|
|
675 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
|
|
|
676 s->inbuf_ptr--;
|
|
|
677 dprintf("skip %x\n", header);
|
|
|
678 /* reset free format frame size to give a chance
|
|
|
679 to get a new bitrate */
|
|
|
680 s->free_format_frame_size = 0;
|
|
|
681 } else {
|
|
|
682 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
|
|
|
683 s->header_count= -3;
|
|
|
684 s->header= header;
|
|
|
685 s->header_count++;
|
|
|
686 s->frame_size = ret;
|
|
|
687
|
|
|
688 #if 0
|
|
|
689 /* free format: prepare to compute frame size */
|
|
|
690 if (decode_header(s, header) == 1) {
|
|
|
691 s->frame_size = -1;
|
|
|
692 }
|
|
|
693 #endif
|
|
|
694 }
|
|
|
695 if(s->header_count <= 0)
|
|
|
696 avctx->sample_rate= sr; //FIXME ugly
|
|
|
697 }
|
|
|
698 } else
|
|
|
699 #if 0
|
|
|
700 if (s->frame_size == -1) {
|
|
|
701 /* free format : find next sync to compute frame size */
|
|
|
702 len = MPA_MAX_CODED_FRAME_SIZE - len;
|
|
|
703 if (len > buf_size)
|
|
|
704 len = buf_size;
|
|
|
705 if (len == 0) {
|
|
|
706 /* frame too long: resync */
|
|
|
707 s->frame_size = 0;
|
|
|
708 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
|
|
|
709 s->inbuf_ptr--;
|
|
|
710 } else {
|
|
|
711 uint8_t *p, *pend;
|
|
|
712 uint32_t header1;
|
|
|
713 int padding;
|
|
|
714
|
|
|
715 memcpy(s->inbuf_ptr, buf_ptr, len);
|
|
|
716 /* check for header */
|
|
|
717 p = s->inbuf_ptr - 3;
|
|
|
718 pend = s->inbuf_ptr + len - 4;
|
|
|
719 while (p <= pend) {
|
|
|
720 header = (p[0] << 24) | (p[1] << 16) |
|
|
|
721 (p[2] << 8) | p[3];
|
|
|
722 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
|
|
|
723 (s->inbuf[2] << 8) | s->inbuf[3];
|
|
|
724 /* check with high probability that we have a
|
|
|
725 valid header */
|
|
|
726 if ((header & SAME_HEADER_MASK) ==
|
|
|
727 (header1 & SAME_HEADER_MASK)) {
|
|
|
728 /* header found: update pointers */
|
|
|
729 len = (p + 4) - s->inbuf_ptr;
|
|
|
730 buf_ptr += len;
|
|
|
731 buf_size -= len;
|
|
|
732 s->inbuf_ptr = p;
|
|
|
733 /* compute frame size */
|
|
|
734 s->free_format_next_header = header;
|
|
|
735 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
|
|
|
736 padding = (header1 >> 9) & 1;
|
|
|
737 if (s->layer == 1)
|
|
|
738 s->free_format_frame_size -= padding * 4;
|
|
|
739 else
|
|
|
740 s->free_format_frame_size -= padding;
|
|
|
741 dprintf("free frame size=%d padding=%d\n",
|
|
|
742 s->free_format_frame_size, padding);
|
|
|
743 decode_header(s, header1);
|
|
|
744 goto next_data;
|
|
|
745 }
|
|
|
746 p++;
|
|
|
747 }
|
|
|
748 /* not found: simply increase pointers */
|
|
|
749 buf_ptr += len;
|
|
|
750 s->inbuf_ptr += len;
|
|
|
751 buf_size -= len;
|
|
|
752 }
|
|
|
753 } else
|
|
|
754 #endif
|
|
|
755 if (len < s->frame_size) {
|
|
|
756 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
|
|
|
757 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
|
|
|
758 len = FFMIN(s->frame_size - len, buf_size);
|
|
|
759 memcpy(s->inbuf_ptr, buf_ptr, len);
|
|
|
760 buf_ptr += len;
|
|
|
761 s->inbuf_ptr += len;
|
|
|
762 buf_size -= len;
|
|
|
763 }
|
|
|
764
|
|
|
765 if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
|
|
|
766 && buf_size + buf_ptr - buf >= s->frame_size){
|
|
|
767 if(s->header_count > 0){
|
|
|
768 *poutbuf = buf;
|
|
|
769 *poutbuf_size = s->frame_size;
|
|
|
770 }
|
|
|
771 buf_ptr = buf + s->frame_size;
|
|
|
772 s->inbuf_ptr = s->inbuf;
|
|
|
773 s->frame_size = 0;
|
|
|
774 break;
|
|
|
775 }
|
|
|
776
|
|
|
777 // next_data:
|
|
|
778 if (s->frame_size > 0 &&
|
|
|
779 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
|
|
|
780 if(s->header_count > 0){
|
|
|
781 *poutbuf = s->inbuf;
|
|
|
782 *poutbuf_size = s->inbuf_ptr - s->inbuf;
|
|
|
783 }
|
|
|
784 s->inbuf_ptr = s->inbuf;
|
|
|
785 s->frame_size = 0;
|
|
|
786 break;
|
|
|
787 }
|
|
|
788 }
|
|
|
789 return buf_ptr - buf;
|
|
|
790 }
|
|
|
791 #endif /* CONFIG_MPEGAUDIO_PARSER */
|
|
|
792
|
|
|
793 #if defined(CONFIG_AC3_PARSER) || defined(CONFIG_AAC_PARSER)
|
|
|
794 /* also used for ADTS AAC */
|
|
|
795 typedef struct AC3ParseContext {
|
|
|
796 uint8_t *inbuf_ptr;
|
|
|
797 int frame_size;
|
|
|
798 int header_size;
|
|
|
799 int (*sync)(const uint8_t *buf, int *channels, int *sample_rate,
|
|
|
800 int *bit_rate, int *samples);
|
|
|
801 uint8_t inbuf[8192]; /* input buffer */
|
|
|
802 } AC3ParseContext;
|
|
|
803
|
|
|
804 #define AC3_HEADER_SIZE 7
|
|
|
805 #define AAC_HEADER_SIZE 7
|
|
|
806
|
|
|
807 #ifdef CONFIG_AC3_PARSER
|
|
|
808 static const int ac3_sample_rates[4] = {
|
|
|
809 48000, 44100, 32000, 0
|
|
|
810 };
|
|
|
811
|
|
|
812 static const int ac3_frame_sizes[64][3] = {
|
|
|
813 { 64, 69, 96 },
|
|
|
814 { 64, 70, 96 },
|
|
|
815 { 80, 87, 120 },
|
|
|
816 { 80, 88, 120 },
|
|
|
817 { 96, 104, 144 },
|
|
|
818 { 96, 105, 144 },
|
|
|
819 { 112, 121, 168 },
|
|
|
820 { 112, 122, 168 },
|
|
|
821 { 128, 139, 192 },
|
|
|
822 { 128, 140, 192 },
|
|
|
823 { 160, 174, 240 },
|
|
|
824 { 160, 175, 240 },
|
|
|
825 { 192, 208, 288 },
|
|
|
826 { 192, 209, 288 },
|
|
|
827 { 224, 243, 336 },
|
|
|
828 { 224, 244, 336 },
|
|
|
829 { 256, 278, 384 },
|
|
|
830 { 256, 279, 384 },
|
|
|
831 { 320, 348, 480 },
|
|
|
832 { 320, 349, 480 },
|
|
|
833 { 384, 417, 576 },
|
|
|
834 { 384, 418, 576 },
|
|
|
835 { 448, 487, 672 },
|
|
|
836 { 448, 488, 672 },
|
|
|
837 { 512, 557, 768 },
|
|
|
838 { 512, 558, 768 },
|
|
|
839 { 640, 696, 960 },
|
|
|
840 { 640, 697, 960 },
|
|
|
841 { 768, 835, 1152 },
|
|
|
842 { 768, 836, 1152 },
|
|
|
843 { 896, 975, 1344 },
|
|
|
844 { 896, 976, 1344 },
|
|
|
845 { 1024, 1114, 1536 },
|
|
|
846 { 1024, 1115, 1536 },
|
|
|
847 { 1152, 1253, 1728 },
|
|
|
848 { 1152, 1254, 1728 },
|
|
|
849 { 1280, 1393, 1920 },
|
|
|
850 { 1280, 1394, 1920 },
|
|
|
851 };
|
|
|
852
|
|
|
853 static const int ac3_bitrates[64] = {
|
|
|
854 32, 32, 40, 40, 48, 48, 56, 56, 64, 64, 80, 80, 96, 96, 112, 112,
|
|
|
855 128, 128, 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384,
|
|
|
856 384, 448, 448, 512, 512, 576, 576, 640, 640,
|
|
|
857 };
|
|
|
858
|
|
|
859 static const int ac3_channels[8] = {
|
|
|
860 2, 1, 2, 3, 3, 4, 4, 5
|
|
|
861 };
|
|
|
862 #endif /* CONFIG_AC3_PARSER */
|
|
|
863
|
|
|
864 #ifdef CONFIG_AAC_PARSER
|
|
|
865 static const int aac_sample_rates[16] = {
|
|
|
866 96000, 88200, 64000, 48000, 44100, 32000,
|
|
|
867 24000, 22050, 16000, 12000, 11025, 8000, 7350
|
|
|
868 };
|
|
|
869
|
|
|
870 static const int aac_channels[8] = {
|
|
|
871 0, 1, 2, 3, 4, 5, 6, 8
|
|
|
872 };
|
|
|
873 #endif
|
|
|
874
|
|
|
875 #ifdef CONFIG_AC3_PARSER
|
|
|
876 static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
|
|
|
877 int *bit_rate, int *samples)
|
|
|
878 {
|
|
|
879 unsigned int fscod, frmsizecod, acmod, bsid, lfeon;
|
|
|
880 GetBitContext bits;
|
|
|
881
|
|
|
882 init_get_bits(&bits, buf, AC3_HEADER_SIZE * 8);
|
|
|
883
|
|
|
884 if(get_bits(&bits, 16) != 0x0b77)
|
|
|
885 return 0;
|
|
|
886
|
|
|
887 skip_bits(&bits, 16); /* crc */
|
|
|
888 fscod = get_bits(&bits, 2);
|
|
|
889 frmsizecod = get_bits(&bits, 6);
|
|
|
890
|
|
|
891 if(!ac3_sample_rates[fscod])
|
|
|
892 return 0;
|
|
|
893
|
|
|
894 bsid = get_bits(&bits, 5);
|
|
|
895 if(bsid > 8)
|
|
|
896 return 0;
|
|
|
897 skip_bits(&bits, 3); /* bsmod */
|
|
|
898 acmod = get_bits(&bits, 3);
|
|
|
899 if(acmod & 1 && acmod != 1)
|
|
|
900 skip_bits(&bits, 2); /* cmixlev */
|
|
|
901 if(acmod & 4)
|
|
|
902 skip_bits(&bits, 2); /* surmixlev */
|
|
|
903 if(acmod & 2)
|
|
|
904 skip_bits(&bits, 2); /* dsurmod */
|
|
|
905 lfeon = get_bits1(&bits);
|
|
|
906
|
|
|
907 *sample_rate = ac3_sample_rates[fscod];
|
|
|
908 *bit_rate = ac3_bitrates[frmsizecod] * 1000;
|
|
|
909 *channels = ac3_channels[acmod] + lfeon;
|
|
|
910 *samples = 6 * 256;
|
|
|
911
|
|
|
912 return ac3_frame_sizes[frmsizecod][fscod] * 2;
|
|
|
913 }
|
|
|
914 #endif /* CONFIG_AC3_PARSER */
|
|
|
915
|
|
|
916 #ifdef CONFIG_AAC_PARSER
|
|
|
917 static int aac_sync(const uint8_t *buf, int *channels, int *sample_rate,
|
|
|
918 int *bit_rate, int *samples)
|
|
|
919 {
|
|
|
920 GetBitContext bits;
|
|
|
921 int size, rdb, ch, sr;
|
|
|
922
|
|
|
923 init_get_bits(&bits, buf, AAC_HEADER_SIZE * 8);
|
|
|
924
|
|
|
925 if(get_bits(&bits, 12) != 0xfff)
|
|
|
926 return 0;
|
|
|
927
|
|
|
928 skip_bits1(&bits); /* id */
|
|
|
929 skip_bits(&bits, 2); /* layer */
|
|
|
930 skip_bits1(&bits); /* protection_absent */
|
|
|
931 skip_bits(&bits, 2); /* profile_objecttype */
|
|
|
932 sr = get_bits(&bits, 4); /* sample_frequency_index */
|
|
|
933 if(!aac_sample_rates[sr])
|
|
|
934 return 0;
|
|
|
935 skip_bits1(&bits); /* private_bit */
|
|
|
936 ch = get_bits(&bits, 3); /* channel_configuration */
|
|
|
937 if(!aac_channels[ch])
|
|
|
938 return 0;
|
|
|
939 skip_bits1(&bits); /* original/copy */
|
|
|
940 skip_bits1(&bits); /* home */
|
|
|
941
|
|
|
942 /* adts_variable_header */
|
|
|
943 skip_bits1(&bits); /* copyright_identification_bit */
|
|
|
944 skip_bits1(&bits); /* copyright_identification_start */
|
|
|
945 size = get_bits(&bits, 13); /* aac_frame_length */
|
|
|
946 skip_bits(&bits, 11); /* adts_buffer_fullness */
|
|
|
947 rdb = get_bits(&bits, 2); /* number_of_raw_data_blocks_in_frame */
|
|
|
948
|
|
|
949 *channels = aac_channels[ch];
|
|
|
950 *sample_rate = aac_sample_rates[sr];
|
|
|
951 *samples = (rdb + 1) * 1024;
|
|
|
952 *bit_rate = size * 8 * *sample_rate / *samples;
|
|
|
953
|
|
|
954 return size;
|
|
|
955 }
|
|
|
956 #endif /* CONFIG_AAC_PARSER */
|
|
|
957
|
|
|
958 #ifdef CONFIG_AC3_PARSER
|
|
|
959 static int ac3_parse_init(AVCodecParserContext *s1)
|
|
|
960 {
|
|
|
961 AC3ParseContext *s = s1->priv_data;
|
|
|
962 s->inbuf_ptr = s->inbuf;
|
|
|
963 s->header_size = AC3_HEADER_SIZE;
|
|
|
964 s->sync = ac3_sync;
|
|
|
965 return 0;
|
|
|
966 }
|
|
|
967 #endif
|
|
|
968
|
|
|
969 #ifdef CONFIG_AAC_PARSER
|
|
|
970 static int aac_parse_init(AVCodecParserContext *s1)
|
|
|
971 {
|
|
|
972 AC3ParseContext *s = s1->priv_data;
|
|
|
973 s->inbuf_ptr = s->inbuf;
|
|
|
974 s->header_size = AAC_HEADER_SIZE;
|
|
|
975 s->sync = aac_sync;
|
|
|
976 return 0;
|
|
|
977 }
|
|
|
978 #endif
|
|
|
979
|
|
|
980 /* also used for ADTS AAC */
|
|
|
981 static int ac3_parse(AVCodecParserContext *s1,
|
|
|
982 AVCodecContext *avctx,
|
|
|
983 uint8_t **poutbuf, int *poutbuf_size,
|
|
|
984 const uint8_t *buf, int buf_size)
|
|
|
985 {
|
|
|
986 AC3ParseContext *s = s1->priv_data;
|
|
|
987 const uint8_t *buf_ptr;
|
|
|
988 int len, sample_rate, bit_rate, channels, samples;
|
|
|
989
|
|
|
990 *poutbuf = NULL;
|
|
|
991 *poutbuf_size = 0;
|
|
|
992
|
|
|
993 buf_ptr = buf;
|
|
|
994 while (buf_size > 0) {
|
|
|
995 len = s->inbuf_ptr - s->inbuf;
|
|
|
996 if (s->frame_size == 0) {
|
|
|
997 /* no header seen : find one. We need at least s->header_size
|
|
|
998 bytes to parse it */
|
|
|
999 len = FFMIN(s->header_size - len, buf_size);
|
|
|
1000
|
|
|
1001 memcpy(s->inbuf_ptr, buf_ptr, len);
|
|
|
1002 buf_ptr += len;
|
|
|
1003 s->inbuf_ptr += len;
|
|
|
1004 buf_size -= len;
|
|
|
1005 if ((s->inbuf_ptr - s->inbuf) == s->header_size) {
|
|
|
1006 len = s->sync(s->inbuf, &channels, &sample_rate, &bit_rate,
|
|
|
1007 &samples);
|
|
|
1008 if (len == 0) {
|
|
|
1009 /* no sync found : move by one byte (inefficient, but simple!) */
|
|
|
1010 memmove(s->inbuf, s->inbuf + 1, s->header_size - 1);
|
|
|
1011 s->inbuf_ptr--;
|
|
|
1012 } else {
|
|
|
1013 s->frame_size = len;
|
|
|
1014 /* update codec info */
|
|
|
1015 avctx->sample_rate = sample_rate;
|
|
|
1016 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
|
|
|
1017 if(avctx->codec_id == CODEC_ID_AC3){
|
|
|
1018 if(avctx->channels!=1 && avctx->channels!=2){
|
|
|
1019 avctx->channels = channels;
|
|
|
1020 }
|
|
|
1021 } else {
|
|
|
1022 avctx->channels = channels;
|
|
|
1023 }
|
|
|
1024 avctx->bit_rate = bit_rate;
|
|
|
1025 avctx->frame_size = samples;
|
|
|
1026 }
|
|
|
1027 }
|
|
|
1028 } else {
|
|
|
1029 len = FFMIN(s->frame_size - len, buf_size);
|
|
|
1030
|
|
|
1031 memcpy(s->inbuf_ptr, buf_ptr, len);
|
|
|
1032 buf_ptr += len;
|
|
|
1033 s->inbuf_ptr += len;
|
|
|
1034 buf_size -= len;
|
|
|
1035
|
|
|
1036 if(s->inbuf_ptr - s->inbuf == s->frame_size){
|
|
|
1037 *poutbuf = s->inbuf;
|
|
|
1038 *poutbuf_size = s->frame_size;
|
|
|
1039 s->inbuf_ptr = s->inbuf;
|
|
|
1040 s->frame_size = 0;
|
|
|
1041 break;
|
|
|
1042 }
|
|
|
1043 }
|
|
|
1044 }
|
|
|
1045 return buf_ptr - buf;
|
|
|
1046 }
|
|
|
1047 #endif /* CONFIG_AC3_PARSER || CONFIG_AAC_PARSER */
|
|
|
1048
|
|
|
1049 #ifdef CONFIG_MPEGVIDEO_PARSER
|
|
|
1050 AVCodecParser mpegvideo_parser = {
|
|
|
1051 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
|
|
|
1052 sizeof(ParseContext1),
|
|
|
1053 NULL,
|
|
|
1054 mpegvideo_parse,
|
|
|
1055 parse1_close,
|
|
|
1056 mpegvideo_split,
|
|
|
1057 };
|
|
|
1058 #endif
|
|
|
1059 #ifdef CONFIG_MPEG4VIDEO_PARSER
|
|
|
1060 AVCodecParser mpeg4video_parser = {
|
|
|
1061 { CODEC_ID_MPEG4 },
|
|
|
1062 sizeof(ParseContext1),
|
|
|
1063 mpeg4video_parse_init,
|
|
|
1064 mpeg4video_parse,
|
|
|
1065 parse1_close,
|
|
|
1066 mpeg4video_split,
|
|
|
1067 };
|
|
|
1068 #endif
|
|
|
1069 #ifdef CONFIG_CAVSVIDEO_PARSER
|
|
|
1070 AVCodecParser cavsvideo_parser = {
|
|
|
1071 { CODEC_ID_CAVS },
|
|
|
1072 sizeof(ParseContext1),
|
|
|
1073 NULL,
|
|
|
1074 cavsvideo_parse,
|
|
|
1075 parse1_close,
|
|
|
1076 mpeg4video_split,
|
|
|
1077 };
|
|
|
1078 #endif
|
|
|
1079 #ifdef CONFIG_MPEGAUDIO_PARSER
|
|
|
1080 AVCodecParser mpegaudio_parser = {
|
|
|
1081 { CODEC_ID_MP2, CODEC_ID_MP3 },
|
|
|
1082 sizeof(MpegAudioParseContext),
|
|
|
1083 mpegaudio_parse_init,
|
|
|
1084 mpegaudio_parse,
|
|
|
1085 NULL,
|
|
|
1086 };
|
|
|
1087 #endif
|
|
|
1088 #ifdef CONFIG_AC3_PARSER
|
|
|
1089 AVCodecParser ac3_parser = {
|
|
|
1090 { CODEC_ID_AC3 },
|
|
|
1091 sizeof(AC3ParseContext),
|
|
|
1092 ac3_parse_init,
|
|
|
1093 ac3_parse,
|
|
|
1094 NULL,
|
|
|
1095 };
|
|
|
1096 #endif
|
|
|
1097 #ifdef CONFIG_AAC_PARSER
|
|
|
1098 AVCodecParser aac_parser = {
|
|
|
1099 { CODEC_ID_AAC },
|
|
|
1100 sizeof(AC3ParseContext),
|
|
|
1101 aac_parse_init,
|
|
|
1102 ac3_parse,
|
|
|
1103 NULL,
|
|
|
1104 };
|
|
|
1105 #endif
|