Mercurial > libavcodec.hg
annotate parser.c @ 2348:d02fb928ca44 libavcodec
pnm parser
| author | michael |
|---|---|
| date | Fri, 12 Nov 2004 22:55:29 +0000 |
| parents | 21f450be6cb5 |
| children | 8d1983254e28 |
| rev | line source |
|---|---|
| 1613 | 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; | |
| 37 | |
| 38 for(parser = av_first_parser; parser != NULL; parser = parser->next) { | |
| 39 if (parser->codec_ids[0] == codec_id || | |
| 40 parser->codec_ids[1] == codec_id || | |
| 2348 | 41 parser->codec_ids[2] == codec_id || |
| 42 parser->codec_ids[3] == codec_id || | |
| 43 parser->codec_ids[4] == codec_id) | |
| 1613 | 44 goto found; |
| 45 } | |
| 46 return NULL; | |
| 47 found: | |
| 48 s = av_mallocz(sizeof(AVCodecParserContext)); | |
| 49 if (!s) | |
| 50 return NULL; | |
| 51 s->parser = parser; | |
| 52 s->priv_data = av_mallocz(parser->priv_data_size); | |
| 53 if (!s->priv_data) { | |
| 54 av_free(s); | |
| 55 return NULL; | |
| 56 } | |
| 57 if (parser->parser_init) { | |
| 58 ret = parser->parser_init(s); | |
| 59 if (ret != 0) { | |
| 60 av_free(s->priv_data); | |
| 61 av_free(s); | |
| 62 return NULL; | |
| 63 } | |
| 64 } | |
| 2030 | 65 s->fetch_timestamp=1; |
| 1613 | 66 return s; |
| 67 } | |
| 68 | |
|
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
69 /* NOTE: buf_size == 0 is used to signal EOF so that the last frame |
|
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
70 can be returned if necessary */ |
| 1613 | 71 int av_parser_parse(AVCodecParserContext *s, |
| 72 AVCodecContext *avctx, | |
| 73 uint8_t **poutbuf, int *poutbuf_size, | |
| 1696 | 74 const uint8_t *buf, int buf_size, |
| 75 int64_t pts, int64_t dts) | |
| 1613 | 76 { |
| 1696 | 77 int index, i, k; |
|
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
78 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; |
|
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
79 |
|
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
80 if (buf_size == 0) { |
|
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
81 /* padding is always necessary even if EOF, so we add it here */ |
|
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
82 memset(dummy_buf, 0, sizeof(dummy_buf)); |
|
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
83 buf = dummy_buf; |
| 1696 | 84 } else { |
| 85 /* add a new packet descriptor */ | |
| 86 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1); | |
| 87 s->cur_frame_start_index = k; | |
| 88 s->cur_frame_offset[k] = s->cur_offset; | |
| 89 s->cur_frame_pts[k] = pts; | |
| 90 s->cur_frame_dts[k] = dts; | |
| 91 | |
| 92 /* fill first PTS/DTS */ | |
| 2030 | 93 if (s->fetch_timestamp){ |
| 94 s->fetch_timestamp=0; | |
| 1696 | 95 s->last_pts = pts; |
| 96 s->last_dts = dts; | |
| 2107 | 97 s->cur_frame_pts[k] = |
| 98 s->cur_frame_dts[k] = AV_NOPTS_VALUE; | |
| 1696 | 99 } |
|
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
100 } |
|
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
101 |
| 1613 | 102 /* WARNING: the returned index can be negative */ |
| 103 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size); | |
| 2107 | 104 //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); |
| 1613 | 105 /* update the file pointer */ |
| 106 if (*poutbuf_size) { | |
| 1696 | 107 /* fill the data for the current frame */ |
| 1613 | 108 s->frame_offset = s->last_frame_offset; |
| 1696 | 109 s->pts = s->last_pts; |
| 110 s->dts = s->last_dts; | |
| 111 | |
| 112 /* offset of the next frame */ | |
| 1613 | 113 s->last_frame_offset = s->cur_offset + index; |
| 1696 | 114 /* find the packet in which the new frame starts. It |
| 115 is tricky because of MPEG video start codes | |
| 116 which can begin in one packet and finish in | |
| 117 another packet. In the worst case, an MPEG | |
| 118 video start code could be in 4 different | |
| 119 packets. */ | |
| 120 k = s->cur_frame_start_index; | |
| 121 for(i = 0; i < AV_PARSER_PTS_NB; i++) { | |
| 122 if (s->last_frame_offset >= s->cur_frame_offset[k]) | |
| 123 break; | |
| 124 k = (k - 1) & (AV_PARSER_PTS_NB - 1); | |
| 125 } | |
| 2030 | 126 |
| 1696 | 127 s->last_pts = s->cur_frame_pts[k]; |
| 128 s->last_dts = s->cur_frame_dts[k]; | |
| 2030 | 129 |
| 130 /* some parsers tell us the packet size even before seeing the first byte of the next packet, | |
| 131 so the next pts/dts is in the next chunk */ | |
| 132 if(index == buf_size){ | |
| 133 s->fetch_timestamp=1; | |
| 134 } | |
| 1613 | 135 } |
| 136 if (index < 0) | |
| 137 index = 0; | |
| 138 s->cur_offset += index; | |
| 139 return index; | |
| 140 } | |
| 141 | |
| 142 void av_parser_close(AVCodecParserContext *s) | |
| 143 { | |
| 144 if (s->parser->parser_close) | |
| 145 s->parser->parser_close(s); | |
| 146 av_free(s->priv_data); | |
| 147 av_free(s); | |
| 148 } | |
| 149 | |
| 150 /*****************************************************/ | |
| 151 | |
| 152 //#define END_NOT_FOUND (-100) | |
| 153 | |
| 154 #define PICTURE_START_CODE 0x00000100 | |
| 155 #define SEQ_START_CODE 0x000001b3 | |
| 156 #define EXT_START_CODE 0x000001b5 | |
| 157 #define SLICE_MIN_START_CODE 0x00000101 | |
| 158 #define SLICE_MAX_START_CODE 0x000001af | |
| 159 | |
| 160 typedef struct ParseContext1{ | |
| 1988 | 161 ParseContext pc; |
| 162 /* XXX/FIXME PC1 vs. PC */ | |
| 1613 | 163 /* MPEG2 specific */ |
| 164 int frame_rate; | |
| 165 int progressive_sequence; | |
| 166 int width, height; | |
| 1614 | 167 |
| 1613 | 168 /* XXX: suppress that, needed by MPEG4 */ |
| 169 MpegEncContext *enc; | |
| 1614 | 170 int first_picture; |
| 1613 | 171 } ParseContext1; |
| 172 | |
| 173 /** | |
| 174 * combines the (truncated) bitstream to a complete frame | |
| 175 * @returns -1 if no complete frame could be created | |
| 176 */ | |
| 1988 | 177 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size) |
| 1613 | 178 { |
| 179 #if 0 | |
| 180 if(pc->overread){ | |
| 181 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
| 182 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
| 183 } | |
| 184 #endif | |
| 185 | |
| 186 /* copy overreaded bytes from last frame into buffer */ | |
| 187 for(; pc->overread>0; pc->overread--){ | |
| 188 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++]; | |
| 189 } | |
| 190 | |
| 191 pc->last_index= pc->index; | |
| 192 | |
| 193 /* copy into buffer end return */ | |
| 194 if(next == END_NOT_FOUND){ | |
| 195 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
| 196 | |
| 197 memcpy(&pc->buffer[pc->index], *buf, *buf_size); | |
| 198 pc->index += *buf_size; | |
| 199 return -1; | |
| 200 } | |
| 201 | |
| 202 *buf_size= | |
| 203 pc->overread_index= pc->index + next; | |
| 204 | |
| 205 /* append to buffer */ | |
| 206 if(pc->index){ | |
| 207 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
| 208 | |
| 209 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE ); | |
| 210 pc->index = 0; | |
| 211 *buf= pc->buffer; | |
| 212 } | |
| 213 | |
| 214 /* store overread bytes */ | |
| 215 for(;next < 0; next++){ | |
| 216 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next]; | |
| 217 pc->overread++; | |
| 218 } | |
| 219 | |
| 220 #if 0 | |
| 221 if(pc->overread){ | |
| 222 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
| 223 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
| 224 } | |
| 225 #endif | |
| 226 | |
| 227 return 0; | |
| 228 } | |
| 229 | |
| 230 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end) | |
| 231 { | |
| 232 const uint8_t *buf_ptr; | |
| 233 unsigned int state=0xFFFFFFFF, v; | |
| 234 int val; | |
| 235 | |
| 236 buf_ptr = *pbuf_ptr; | |
| 237 while (buf_ptr < buf_end) { | |
| 238 v = *buf_ptr++; | |
| 239 if (state == 0x000001) { | |
| 240 state = ((state << 8) | v) & 0xffffff; | |
| 241 val = state; | |
| 242 goto found; | |
| 243 } | |
| 244 state = ((state << 8) | v) & 0xffffff; | |
| 245 } | |
| 246 val = -1; | |
| 247 found: | |
| 248 *pbuf_ptr = buf_ptr; | |
| 249 return val; | |
| 250 } | |
| 251 | |
| 252 /* XXX: merge with libavcodec ? */ | |
| 253 #define MPEG1_FRAME_RATE_BASE 1001 | |
| 254 | |
| 255 static const int frame_rate_tab[16] = { | |
| 256 0, | |
| 257 24000, | |
| 258 24024, | |
| 259 25025, | |
| 260 30000, | |
| 261 30030, | |
| 262 50050, | |
| 263 60000, | |
| 264 60060, | |
| 265 // Xing's 15fps: (9) | |
| 266 15015, | |
| 267 // libmpeg3's "Unofficial economy rates": (10-13) | |
| 268 5005, | |
| 269 10010, | |
| 270 12012, | |
| 271 15015, | |
| 272 // random, just to avoid segfault !never encode these | |
| 273 25025, | |
| 274 25025, | |
| 275 }; | |
| 276 | |
| 277 static void mpegvideo_extract_headers(AVCodecParserContext *s, | |
| 278 AVCodecContext *avctx, | |
| 279 const uint8_t *buf, int buf_size) | |
| 280 { | |
| 281 ParseContext1 *pc = s->priv_data; | |
| 282 const uint8_t *buf_end; | |
| 283 int32_t start_code; | |
| 284 int frame_rate_index, ext_type, bytes_left; | |
| 285 int frame_rate_ext_n, frame_rate_ext_d; | |
| 2119 | 286 int picture_structure, top_field_first, repeat_first_field, progressive_frame; |
| 1613 | 287 int horiz_size_ext, vert_size_ext; |
| 288 | |
| 289 s->repeat_pict = 0; | |
| 290 buf_end = buf + buf_size; | |
| 291 while (buf < buf_end) { | |
| 292 start_code = find_start_code(&buf, buf_end); | |
| 293 bytes_left = buf_end - buf; | |
| 294 switch(start_code) { | |
| 295 case PICTURE_START_CODE: | |
| 296 if (bytes_left >= 2) { | |
| 297 s->pict_type = (buf[1] >> 3) & 7; | |
| 298 } | |
| 299 break; | |
| 300 case SEQ_START_CODE: | |
| 301 if (bytes_left >= 4) { | |
| 2269 | 302 pc->width = (buf[0] << 4) | (buf[1] >> 4); |
| 303 pc->height = ((buf[1] & 0x0f) << 8) | buf[2]; | |
| 2270 | 304 avcodec_set_dimensions(avctx, pc->width, pc->height); |
| 1613 | 305 frame_rate_index = buf[3] & 0xf; |
| 306 pc->frame_rate = avctx->frame_rate = frame_rate_tab[frame_rate_index]; | |
| 307 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE; | |
| 1681 | 308 avctx->codec_id = CODEC_ID_MPEG1VIDEO; |
| 309 avctx->sub_id = 1; | |
| 1613 | 310 } |
| 311 break; | |
| 312 case EXT_START_CODE: | |
| 313 if (bytes_left >= 1) { | |
| 314 ext_type = (buf[0] >> 4); | |
| 315 switch(ext_type) { | |
| 316 case 0x1: /* sequence extension */ | |
| 317 if (bytes_left >= 6) { | |
| 318 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7); | |
| 319 vert_size_ext = (buf[2] >> 5) & 3; | |
| 320 frame_rate_ext_n = (buf[5] >> 5) & 3; | |
| 321 frame_rate_ext_d = (buf[5] & 0x1f); | |
| 322 pc->progressive_sequence = buf[1] & (1 << 3); | |
| 323 | |
| 2269 | 324 pc->width |=(horiz_size_ext << 12); |
| 325 pc->height |=( vert_size_ext << 12); | |
| 2270 | 326 avcodec_set_dimensions(avctx, pc->width, pc->height); |
| 1613 | 327 avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1); |
| 328 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1); | |
| 1681 | 329 avctx->codec_id = CODEC_ID_MPEG2VIDEO; |
| 1613 | 330 avctx->sub_id = 2; /* forces MPEG2 */ |
| 331 } | |
| 332 break; | |
| 333 case 0x8: /* picture coding extension */ | |
| 334 if (bytes_left >= 5) { | |
| 2120 | 335 picture_structure = buf[2]&3; |
| 1613 | 336 top_field_first = buf[3] & (1 << 7); |
| 337 repeat_first_field = buf[3] & (1 << 1); | |
| 338 progressive_frame = buf[4] & (1 << 7); | |
| 339 | |
| 340 /* check if we must repeat the frame */ | |
| 341 if (repeat_first_field) { | |
| 342 if (pc->progressive_sequence) { | |
| 343 if (top_field_first) | |
| 344 s->repeat_pict = 4; | |
| 345 else | |
| 346 s->repeat_pict = 2; | |
| 347 } else if (progressive_frame) { | |
| 348 s->repeat_pict = 1; | |
| 349 } | |
| 350 } | |
| 2119 | 351 |
| 352 /* the packet only represents half a frame | |
| 353 XXX,FIXME maybe find a different solution */ | |
| 354 if(picture_structure != 3) | |
| 355 s->repeat_pict = -1; | |
| 1613 | 356 } |
| 357 break; | |
| 358 } | |
| 359 } | |
| 360 break; | |
| 361 case -1: | |
| 362 goto the_end; | |
| 363 default: | |
| 364 /* we stop parsing when we encounter a slice. It ensures | |
| 365 that this function takes a negligible amount of time */ | |
| 366 if (start_code >= SLICE_MIN_START_CODE && | |
| 367 start_code <= SLICE_MAX_START_CODE) | |
| 368 goto the_end; | |
| 369 break; | |
| 370 } | |
| 371 } | |
| 372 the_end: ; | |
| 373 } | |
| 374 | |
| 375 static int mpegvideo_parse(AVCodecParserContext *s, | |
| 376 AVCodecContext *avctx, | |
| 377 uint8_t **poutbuf, int *poutbuf_size, | |
| 378 const uint8_t *buf, int buf_size) | |
| 379 { | |
| 1988 | 380 ParseContext1 *pc1 = s->priv_data; |
| 381 ParseContext *pc= &pc1->pc; | |
| 1613 | 382 int next; |
| 383 | |
| 1988 | 384 next= ff_mpeg1_find_frame_end(pc, buf, buf_size); |
| 1613 | 385 |
| 1988 | 386 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
| 1613 | 387 *poutbuf = NULL; |
| 388 *poutbuf_size = 0; | |
| 389 return buf_size; | |
| 390 } | |
| 391 /* we have a full frame : we just parse the first few MPEG headers | |
| 392 to have the full timing information. The time take by this | |
| 393 function should be negligible for uncorrupted streams */ | |
| 394 mpegvideo_extract_headers(s, avctx, buf, buf_size); | |
| 395 #if 0 | |
| 396 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", | |
| 397 s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict); | |
| 398 #endif | |
| 399 | |
| 400 *poutbuf = (uint8_t *)buf; | |
| 401 *poutbuf_size = buf_size; | |
| 402 return next; | |
| 403 } | |
| 404 | |
| 1988 | 405 void ff_parse_close(AVCodecParserContext *s) |
| 1613 | 406 { |
| 1988 | 407 ParseContext *pc = s->priv_data; |
| 1613 | 408 |
| 409 av_free(pc->buffer); | |
| 1988 | 410 } |
| 411 | |
| 412 static void parse1_close(AVCodecParserContext *s) | |
| 413 { | |
| 414 ParseContext1 *pc1 = s->priv_data; | |
| 415 | |
| 416 av_free(pc1->pc.buffer); | |
| 417 av_free(pc1->enc); | |
| 1613 | 418 } |
| 419 | |
| 420 /*************************/ | |
| 421 | |
| 422 /* used by parser */ | |
| 423 /* XXX: make it use less memory */ | |
| 424 static int av_mpeg4_decode_header(AVCodecParserContext *s1, | |
| 425 AVCodecContext *avctx, | |
| 426 const uint8_t *buf, int buf_size) | |
| 427 { | |
| 428 ParseContext1 *pc = s1->priv_data; | |
| 429 MpegEncContext *s = pc->enc; | |
| 430 GetBitContext gb1, *gb = &gb1; | |
| 431 int ret; | |
| 432 | |
| 433 s->avctx = avctx; | |
| 1614 | 434 s->current_picture_ptr = &s->current_picture; |
| 435 | |
| 436 if (avctx->extradata_size && pc->first_picture){ | |
| 437 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8); | |
| 438 ret = ff_mpeg4_decode_picture_header(s, gb); | |
| 439 } | |
| 440 | |
| 1613 | 441 init_get_bits(gb, buf, 8 * buf_size); |
| 442 ret = ff_mpeg4_decode_picture_header(s, gb); | |
| 443 if (s->width) { | |
| 2270 | 444 avcodec_set_dimensions(avctx, s->width, s->height); |
| 1613 | 445 } |
| 1614 | 446 pc->first_picture = 0; |
| 1613 | 447 return ret; |
| 448 } | |
| 449 | |
|
2024
f65d87bfdd5a
some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents:
1988
diff
changeset
|
450 static int mpeg4video_parse_init(AVCodecParserContext *s) |
| 1613 | 451 { |
| 452 ParseContext1 *pc = s->priv_data; | |
| 1614 | 453 |
| 1613 | 454 pc->enc = av_mallocz(sizeof(MpegEncContext)); |
| 455 if (!pc->enc) | |
| 456 return -1; | |
| 1614 | 457 pc->first_picture = 1; |
| 1613 | 458 return 0; |
| 459 } | |
| 460 | |
| 461 static int mpeg4video_parse(AVCodecParserContext *s, | |
| 462 AVCodecContext *avctx, | |
| 463 uint8_t **poutbuf, int *poutbuf_size, | |
| 464 const uint8_t *buf, int buf_size) | |
| 465 { | |
| 1988 | 466 ParseContext *pc = s->priv_data; |
| 1613 | 467 int next; |
| 468 | |
| 1988 | 469 next= ff_mpeg4_find_frame_end(pc, buf, buf_size); |
| 1613 | 470 |
| 1988 | 471 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
| 1613 | 472 *poutbuf = NULL; |
| 473 *poutbuf_size = 0; | |
| 474 return buf_size; | |
| 475 } | |
| 476 av_mpeg4_decode_header(s, avctx, buf, buf_size); | |
| 477 | |
| 478 *poutbuf = (uint8_t *)buf; | |
| 479 *poutbuf_size = buf_size; | |
| 480 return next; | |
| 481 } | |
| 482 | |
| 483 /*************************/ | |
| 484 | |
| 485 typedef struct MpegAudioParseContext { | |
| 486 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */ | |
| 487 uint8_t *inbuf_ptr; | |
| 488 int frame_size; | |
| 489 int free_format_frame_size; | |
| 490 int free_format_next_header; | |
| 491 } MpegAudioParseContext; | |
| 492 | |
| 493 #define MPA_HEADER_SIZE 4 | |
| 494 | |
| 495 /* header + layer + bitrate + freq + lsf/mpeg25 */ | |
| 496 #define SAME_HEADER_MASK \ | |
| 497 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19)) | |
| 498 | |
| 499 static int mpegaudio_parse_init(AVCodecParserContext *s1) | |
| 500 { | |
| 501 MpegAudioParseContext *s = s1->priv_data; | |
| 502 s->inbuf_ptr = s->inbuf; | |
| 503 return 0; | |
| 504 } | |
| 505 | |
| 506 static int mpegaudio_parse(AVCodecParserContext *s1, | |
| 507 AVCodecContext *avctx, | |
| 508 uint8_t **poutbuf, int *poutbuf_size, | |
| 509 const uint8_t *buf, int buf_size) | |
| 510 { | |
| 511 MpegAudioParseContext *s = s1->priv_data; | |
| 512 int len, ret; | |
| 513 uint32_t header; | |
| 514 const uint8_t *buf_ptr; | |
| 515 | |
| 516 *poutbuf = NULL; | |
| 517 *poutbuf_size = 0; | |
| 518 buf_ptr = buf; | |
| 519 while (buf_size > 0) { | |
| 520 len = s->inbuf_ptr - s->inbuf; | |
| 521 if (s->frame_size == 0) { | |
| 522 /* special case for next header for first frame in free | |
| 523 format case (XXX: find a simpler method) */ | |
| 524 if (s->free_format_next_header != 0) { | |
| 525 s->inbuf[0] = s->free_format_next_header >> 24; | |
| 526 s->inbuf[1] = s->free_format_next_header >> 16; | |
| 527 s->inbuf[2] = s->free_format_next_header >> 8; | |
| 528 s->inbuf[3] = s->free_format_next_header; | |
| 529 s->inbuf_ptr = s->inbuf + 4; | |
| 530 s->free_format_next_header = 0; | |
| 531 goto got_header; | |
| 532 } | |
| 533 /* no header seen : find one. We need at least MPA_HEADER_SIZE | |
| 534 bytes to parse it */ | |
| 535 len = MPA_HEADER_SIZE - len; | |
| 536 if (len > buf_size) | |
| 537 len = buf_size; | |
| 538 if (len > 0) { | |
| 539 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 540 buf_ptr += len; | |
| 541 buf_size -= len; | |
| 542 s->inbuf_ptr += len; | |
| 543 } | |
| 544 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) { | |
| 545 got_header: | |
| 546 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
| 547 (s->inbuf[2] << 8) | s->inbuf[3]; | |
| 548 | |
| 549 ret = mpa_decode_header(avctx, header); | |
| 550 if (ret < 0) { | |
| 551 /* no sync found : move by one byte (inefficient, but simple!) */ | |
| 552 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
| 553 s->inbuf_ptr--; | |
| 554 dprintf("skip %x\n", header); | |
| 555 /* reset free format frame size to give a chance | |
| 556 to get a new bitrate */ | |
| 557 s->free_format_frame_size = 0; | |
| 558 } else { | |
| 559 s->frame_size = ret; | |
| 560 #if 0 | |
| 561 /* free format: prepare to compute frame size */ | |
| 562 if (decode_header(s, header) == 1) { | |
| 563 s->frame_size = -1; | |
| 564 } | |
| 565 #endif | |
| 566 } | |
| 567 } | |
| 568 } else | |
| 569 #if 0 | |
| 570 if (s->frame_size == -1) { | |
| 571 /* free format : find next sync to compute frame size */ | |
| 572 len = MPA_MAX_CODED_FRAME_SIZE - len; | |
| 573 if (len > buf_size) | |
| 574 len = buf_size; | |
| 575 if (len == 0) { | |
| 576 /* frame too long: resync */ | |
| 577 s->frame_size = 0; | |
| 578 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
| 579 s->inbuf_ptr--; | |
| 580 } else { | |
| 581 uint8_t *p, *pend; | |
| 582 uint32_t header1; | |
| 583 int padding; | |
| 584 | |
| 585 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 586 /* check for header */ | |
| 587 p = s->inbuf_ptr - 3; | |
| 588 pend = s->inbuf_ptr + len - 4; | |
| 589 while (p <= pend) { | |
| 590 header = (p[0] << 24) | (p[1] << 16) | | |
| 591 (p[2] << 8) | p[3]; | |
| 592 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
| 593 (s->inbuf[2] << 8) | s->inbuf[3]; | |
| 594 /* check with high probability that we have a | |
| 595 valid header */ | |
| 596 if ((header & SAME_HEADER_MASK) == | |
| 597 (header1 & SAME_HEADER_MASK)) { | |
| 598 /* header found: update pointers */ | |
| 599 len = (p + 4) - s->inbuf_ptr; | |
| 600 buf_ptr += len; | |
| 601 buf_size -= len; | |
| 602 s->inbuf_ptr = p; | |
| 603 /* compute frame size */ | |
| 604 s->free_format_next_header = header; | |
| 605 s->free_format_frame_size = s->inbuf_ptr - s->inbuf; | |
| 606 padding = (header1 >> 9) & 1; | |
| 607 if (s->layer == 1) | |
| 608 s->free_format_frame_size -= padding * 4; | |
| 609 else | |
| 610 s->free_format_frame_size -= padding; | |
| 611 dprintf("free frame size=%d padding=%d\n", | |
| 612 s->free_format_frame_size, padding); | |
| 613 decode_header(s, header1); | |
| 614 goto next_data; | |
| 615 } | |
| 616 p++; | |
| 617 } | |
| 618 /* not found: simply increase pointers */ | |
| 619 buf_ptr += len; | |
| 620 s->inbuf_ptr += len; | |
| 621 buf_size -= len; | |
| 622 } | |
| 623 } else | |
| 624 #endif | |
| 625 if (len < s->frame_size) { | |
| 626 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) | |
| 627 s->frame_size = MPA_MAX_CODED_FRAME_SIZE; | |
| 628 len = s->frame_size - len; | |
| 629 if (len > buf_size) | |
| 630 len = buf_size; | |
| 631 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 632 buf_ptr += len; | |
| 633 s->inbuf_ptr += len; | |
| 634 buf_size -= len; | |
| 635 } | |
| 636 // next_data: | |
| 637 if (s->frame_size > 0 && | |
| 638 (s->inbuf_ptr - s->inbuf) >= s->frame_size) { | |
| 639 *poutbuf = s->inbuf; | |
| 640 *poutbuf_size = s->inbuf_ptr - s->inbuf; | |
| 641 s->inbuf_ptr = s->inbuf; | |
| 642 s->frame_size = 0; | |
| 643 break; | |
| 644 } | |
| 645 } | |
| 646 return buf_ptr - buf; | |
| 647 } | |
| 648 | |
| 649 #ifdef CONFIG_AC3 | |
| 650 extern int a52_syncinfo (const uint8_t * buf, int * flags, | |
| 651 int * sample_rate, int * bit_rate); | |
| 652 | |
| 653 typedef struct AC3ParseContext { | |
| 654 uint8_t inbuf[4096]; /* input buffer */ | |
| 655 uint8_t *inbuf_ptr; | |
| 656 int frame_size; | |
| 657 int flags; | |
| 658 } AC3ParseContext; | |
| 659 | |
| 660 #define AC3_HEADER_SIZE 7 | |
| 661 #define A52_LFE 16 | |
| 662 | |
| 663 static int ac3_parse_init(AVCodecParserContext *s1) | |
| 664 { | |
| 665 AC3ParseContext *s = s1->priv_data; | |
| 666 s->inbuf_ptr = s->inbuf; | |
| 667 return 0; | |
| 668 } | |
| 669 | |
| 670 static int ac3_parse(AVCodecParserContext *s1, | |
| 671 AVCodecContext *avctx, | |
| 672 uint8_t **poutbuf, int *poutbuf_size, | |
| 673 const uint8_t *buf, int buf_size) | |
| 674 { | |
| 675 AC3ParseContext *s = s1->priv_data; | |
| 676 const uint8_t *buf_ptr; | |
| 677 int len, sample_rate, bit_rate; | |
| 678 static const int ac3_channels[8] = { | |
| 679 2, 1, 2, 3, 3, 4, 4, 5 | |
| 680 }; | |
| 681 | |
| 682 *poutbuf = NULL; | |
| 683 *poutbuf_size = 0; | |
| 684 | |
| 685 buf_ptr = buf; | |
| 686 while (buf_size > 0) { | |
| 687 len = s->inbuf_ptr - s->inbuf; | |
| 688 if (s->frame_size == 0) { | |
| 689 /* no header seen : find one. We need at least 7 bytes to parse it */ | |
| 690 len = AC3_HEADER_SIZE - len; | |
| 691 if (len > buf_size) | |
| 692 len = buf_size; | |
| 693 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 694 buf_ptr += len; | |
| 695 s->inbuf_ptr += len; | |
| 696 buf_size -= len; | |
| 697 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) { | |
| 698 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate); | |
| 699 if (len == 0) { | |
| 700 /* no sync found : move by one byte (inefficient, but simple!) */ | |
| 701 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1); | |
| 702 s->inbuf_ptr--; | |
| 703 } else { | |
| 704 s->frame_size = len; | |
| 705 /* update codec info */ | |
| 706 avctx->sample_rate = sample_rate; | |
| 1987 | 707 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */ |
| 708 if(avctx->channels!=1 && avctx->channels!=2){ | |
| 709 avctx->channels = ac3_channels[s->flags & 7]; | |
| 710 if (s->flags & A52_LFE) | |
| 711 avctx->channels++; | |
| 712 } | |
| 1613 | 713 avctx->bit_rate = bit_rate; |
| 714 avctx->frame_size = 6 * 256; | |
| 715 } | |
| 716 } | |
| 717 } else if (len < s->frame_size) { | |
| 718 len = s->frame_size - len; | |
| 719 if (len > buf_size) | |
| 720 len = buf_size; | |
| 721 | |
| 722 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 723 buf_ptr += len; | |
| 724 s->inbuf_ptr += len; | |
| 725 buf_size -= len; | |
| 726 } else { | |
| 727 *poutbuf = s->inbuf; | |
| 728 *poutbuf_size = s->frame_size; | |
| 729 s->inbuf_ptr = s->inbuf; | |
| 730 s->frame_size = 0; | |
| 731 break; | |
| 732 } | |
| 733 } | |
| 734 return buf_ptr - buf; | |
| 735 } | |
| 736 #endif | |
| 737 | |
| 738 AVCodecParser mpegvideo_parser = { | |
| 739 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO }, | |
| 740 sizeof(ParseContext1), | |
| 741 NULL, | |
| 742 mpegvideo_parse, | |
| 1988 | 743 parse1_close, |
| 1613 | 744 }; |
| 745 | |
| 746 AVCodecParser mpeg4video_parser = { | |
| 747 { CODEC_ID_MPEG4 }, | |
| 748 sizeof(ParseContext1), | |
| 749 mpeg4video_parse_init, | |
| 750 mpeg4video_parse, | |
| 1988 | 751 parse1_close, |
| 1613 | 752 }; |
| 753 | |
| 754 AVCodecParser mpegaudio_parser = { | |
| 755 { CODEC_ID_MP2, CODEC_ID_MP3 }, | |
| 756 sizeof(MpegAudioParseContext), | |
| 757 mpegaudio_parse_init, | |
| 758 mpegaudio_parse, | |
| 759 NULL, | |
| 760 }; | |
| 761 | |
| 762 #ifdef CONFIG_AC3 | |
| 763 AVCodecParser ac3_parser = { | |
| 764 { CODEC_ID_AC3 }, | |
| 765 sizeof(AC3ParseContext), | |
| 766 ac3_parse_init, | |
| 767 ac3_parse, | |
| 768 NULL, | |
| 769 }; | |
| 770 #endif |
