Mercurial > libavcodec.hg
annotate parser.c @ 3061:259fed5adf5d libavcodec
sc_factor support for encoding.
Here's the description I'll add to the mplayer man page:
---
Causes frames with higher quantizers to be more likely to trigger a
scene change detection and make libavcodec use an I-frame (default: 1).
1-16 is a sane range.
Values between 2 and 6 may yield increasing PSNR (up to approximately
0.04 dB) and better placement of I-frames in high-motion scenes.
Higher values than 6 may give very slightly better PSNR (approximately
0.01 dB more than sc_factor=6), but noticably worse visual quality.
---
Original idea from Michael; patch by me.
| author | corey |
|---|---|
| date | Sun, 22 Jan 2006 20:54:52 +0000 |
| parents | 61b4cc042988 |
| children | f02d0b59279c |
| 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 | |
|
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 1613 | 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; | |
| 2967 | 37 |
|
2486
f2a9559db6ac
10l (array gets padded with 0 which is CODEC_ID_NONE -> parsers claim to support CODEC_ID_NONE)
michael
parents:
2480
diff
changeset
|
38 if(codec_id == CODEC_ID_NONE) |
|
f2a9559db6ac
10l (array gets padded with 0 which is CODEC_ID_NONE -> parsers claim to support CODEC_ID_NONE)
michael
parents:
2480
diff
changeset
|
39 return NULL; |
| 1613 | 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 || | |
| 2348 | 44 parser->codec_ids[2] == codec_id || |
| 45 parser->codec_ids[3] == codec_id || | |
| 46 parser->codec_ids[4] == codec_id) | |
| 1613 | 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 } | |
| 2030 | 68 s->fetch_timestamp=1; |
| 1613 | 69 return s; |
| 70 } | |
| 71 | |
|
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
72 /* 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
|
73 can be returned if necessary */ |
| 2967 | 74 int av_parser_parse(AVCodecParserContext *s, |
| 1613 | 75 AVCodecContext *avctx, |
| 2967 | 76 uint8_t **poutbuf, int *poutbuf_size, |
| 1696 | 77 const uint8_t *buf, int buf_size, |
| 78 int64_t pts, int64_t dts) | |
| 1613 | 79 { |
| 1696 | 80 int index, i, k; |
|
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
81 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; |
| 2967 | 82 |
|
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
83 if (buf_size == 0) { |
|
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
84 /* 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
|
85 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
|
86 buf = dummy_buf; |
| 1696 | 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 */ | |
| 2030 | 96 if (s->fetch_timestamp){ |
| 97 s->fetch_timestamp=0; | |
| 1696 | 98 s->last_pts = pts; |
| 99 s->last_dts = dts; | |
| 2107 | 100 s->cur_frame_pts[k] = |
| 101 s->cur_frame_dts[k] = AV_NOPTS_VALUE; | |
| 1696 | 102 } |
|
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
103 } |
|
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
104 |
| 1613 | 105 /* WARNING: the returned index can be negative */ |
| 106 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size); | |
| 2107 | 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); |
| 1613 | 108 /* update the file pointer */ |
| 109 if (*poutbuf_size) { | |
| 1696 | 110 /* fill the data for the current frame */ |
| 1613 | 111 s->frame_offset = s->last_frame_offset; |
| 1696 | 112 s->pts = s->last_pts; |
| 113 s->dts = s->last_dts; | |
| 2967 | 114 |
| 1696 | 115 /* offset of the next frame */ |
| 1613 | 116 s->last_frame_offset = s->cur_offset + index; |
| 1696 | 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 } | |
| 2030 | 129 |
| 1696 | 130 s->last_pts = s->cur_frame_pts[k]; |
| 131 s->last_dts = s->cur_frame_dts[k]; | |
| 2967 | 132 |
| 2030 | 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 } | |
| 1613 | 138 } |
| 139 if (index < 0) | |
| 140 index = 0; | |
| 141 s->cur_offset += index; | |
| 142 return index; | |
| 143 } | |
| 144 | |
| 2777 | 145 /** |
| 146 * | |
| 147 * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed | |
| 148 */ | |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
149 int av_parser_change(AVCodecParserContext *s, |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
150 AVCodecContext *avctx, |
| 2967 | 151 uint8_t **poutbuf, int *poutbuf_size, |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
152 const uint8_t *buf, int buf_size, int keyframe){ |
| 2967 | 153 |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
154 if(s && s->parser->split){ |
| 2777 | 155 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){ |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
156 int i= s->parser->split(avctx, buf, buf_size); |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
157 buf += i; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
158 buf_size -= i; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
159 } |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
160 } |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
161 |
|
2864
95bac7109ff0
Kill some compiler warnings. Compiled code verified identical after changes.
mru
parents:
2846
diff
changeset
|
162 /* cast to avoid warning about discarding qualifiers */ |
|
95bac7109ff0
Kill some compiler warnings. Compiled code verified identical after changes.
mru
parents:
2846
diff
changeset
|
163 *poutbuf= (uint8_t *) buf; |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
164 *poutbuf_size= buf_size; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
165 if(avctx->extradata){ |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
166 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
167 /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/ |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
168 /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){ |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
169 int size= buf_size + avctx->extradata_size; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
170 *poutbuf_size= size; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
171 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); |
| 2967 | 172 |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
173 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size); |
| 2777 | 174 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
175 return 1; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
176 } |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
177 } |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
178 |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
179 return 0; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
180 } |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
181 |
| 1613 | 182 void av_parser_close(AVCodecParserContext *s) |
| 183 { | |
| 184 if (s->parser->parser_close) | |
| 185 s->parser->parser_close(s); | |
| 186 av_free(s->priv_data); | |
| 187 av_free(s); | |
| 188 } | |
| 189 | |
| 190 /*****************************************************/ | |
| 191 | |
| 192 //#define END_NOT_FOUND (-100) | |
| 193 | |
| 2979 | 194 #define PICTURE_START_CODE 0x00000100 |
| 195 #define SEQ_START_CODE 0x000001b3 | |
| 196 #define EXT_START_CODE 0x000001b5 | |
| 197 #define SLICE_MIN_START_CODE 0x00000101 | |
| 198 #define SLICE_MAX_START_CODE 0x000001af | |
| 1613 | 199 |
| 200 typedef struct ParseContext1{ | |
| 1988 | 201 ParseContext pc; |
| 202 /* XXX/FIXME PC1 vs. PC */ | |
| 1613 | 203 /* MPEG2 specific */ |
| 204 int frame_rate; | |
| 205 int progressive_sequence; | |
| 206 int width, height; | |
| 1614 | 207 |
| 1613 | 208 /* XXX: suppress that, needed by MPEG4 */ |
| 209 MpegEncContext *enc; | |
| 1614 | 210 int first_picture; |
| 1613 | 211 } ParseContext1; |
| 212 | |
| 213 /** | |
| 214 * combines the (truncated) bitstream to a complete frame | |
| 215 * @returns -1 if no complete frame could be created | |
| 216 */ | |
| 1988 | 217 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size) |
| 1613 | 218 { |
| 219 #if 0 | |
| 220 if(pc->overread){ | |
| 221 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
| 222 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
| 223 } | |
| 224 #endif | |
| 225 | |
| 226 /* copy overreaded bytes from last frame into buffer */ | |
| 227 for(; pc->overread>0; pc->overread--){ | |
| 228 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++]; | |
| 229 } | |
| 2386 | 230 |
| 231 /* flush remaining if EOF */ | |
| 232 if(!*buf_size && next == END_NOT_FOUND){ | |
| 233 next= 0; | |
| 234 } | |
| 235 | |
| 1613 | 236 pc->last_index= pc->index; |
| 237 | |
| 238 /* copy into buffer end return */ | |
| 239 if(next == END_NOT_FOUND){ | |
| 240 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
| 241 | |
| 242 memcpy(&pc->buffer[pc->index], *buf, *buf_size); | |
| 243 pc->index += *buf_size; | |
| 244 return -1; | |
| 245 } | |
| 246 | |
| 247 *buf_size= | |
| 248 pc->overread_index= pc->index + next; | |
| 2967 | 249 |
| 1613 | 250 /* append to buffer */ |
| 251 if(pc->index){ | |
| 252 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
| 253 | |
| 254 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE ); | |
| 255 pc->index = 0; | |
| 256 *buf= pc->buffer; | |
| 257 } | |
| 258 | |
| 259 /* store overread bytes */ | |
| 260 for(;next < 0; next++){ | |
| 261 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next]; | |
| 262 pc->overread++; | |
| 263 } | |
| 264 | |
| 265 #if 0 | |
| 266 if(pc->overread){ | |
| 267 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
| 268 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
| 269 } | |
| 270 #endif | |
| 271 | |
| 272 return 0; | |
| 273 } | |
| 274 | |
| 275 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end) | |
| 276 { | |
| 277 const uint8_t *buf_ptr; | |
| 278 unsigned int state=0xFFFFFFFF, v; | |
| 279 int val; | |
| 280 | |
| 281 buf_ptr = *pbuf_ptr; | |
| 282 while (buf_ptr < buf_end) { | |
| 283 v = *buf_ptr++; | |
| 284 if (state == 0x000001) { | |
| 285 state = ((state << 8) | v) & 0xffffff; | |
| 286 val = state; | |
| 287 goto found; | |
| 288 } | |
| 289 state = ((state << 8) | v) & 0xffffff; | |
| 290 } | |
| 291 val = -1; | |
| 292 found: | |
| 293 *pbuf_ptr = buf_ptr; | |
| 294 return val; | |
| 295 } | |
| 296 | |
| 297 /* XXX: merge with libavcodec ? */ | |
| 298 #define MPEG1_FRAME_RATE_BASE 1001 | |
| 299 | |
| 300 static const int frame_rate_tab[16] = { | |
| 2967 | 301 0, |
| 1613 | 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 | |
| 2637 | 322 //FIXME move into mpeg12.c |
| 2967 | 323 static void mpegvideo_extract_headers(AVCodecParserContext *s, |
| 1613 | 324 AVCodecContext *avctx, |
| 325 const uint8_t *buf, int buf_size) | |
| 326 { | |
| 327 ParseContext1 *pc = s->priv_data; | |
| 328 const uint8_t *buf_end; | |
| 329 int32_t start_code; | |
| 330 int frame_rate_index, ext_type, bytes_left; | |
| 331 int frame_rate_ext_n, frame_rate_ext_d; | |
| 2119 | 332 int picture_structure, top_field_first, repeat_first_field, progressive_frame; |
| 2539 | 333 int horiz_size_ext, vert_size_ext, bit_rate_ext; |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
334 //FIXME replace the crap with get_bits() |
| 1613 | 335 s->repeat_pict = 0; |
| 336 buf_end = buf + buf_size; | |
| 337 while (buf < buf_end) { | |
| 338 start_code = find_start_code(&buf, buf_end); | |
| 339 bytes_left = buf_end - buf; | |
| 340 switch(start_code) { | |
| 341 case PICTURE_START_CODE: | |
| 342 if (bytes_left >= 2) { | |
| 343 s->pict_type = (buf[1] >> 3) & 7; | |
| 344 } | |
| 345 break; | |
| 346 case SEQ_START_CODE: | |
| 2539 | 347 if (bytes_left >= 7) { |
| 2269 | 348 pc->width = (buf[0] << 4) | (buf[1] >> 4); |
| 349 pc->height = ((buf[1] & 0x0f) << 8) | buf[2]; | |
| 2270 | 350 avcodec_set_dimensions(avctx, pc->width, pc->height); |
| 1613 | 351 frame_rate_index = buf[3] & 0xf; |
| 2637 | 352 pc->frame_rate = avctx->time_base.den = frame_rate_tab[frame_rate_index]; |
| 353 avctx->time_base.num = MPEG1_FRAME_RATE_BASE; | |
| 2539 | 354 avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400; |
| 1681 | 355 avctx->codec_id = CODEC_ID_MPEG1VIDEO; |
| 356 avctx->sub_id = 1; | |
| 1613 | 357 } |
| 358 break; | |
| 359 case EXT_START_CODE: | |
| 360 if (bytes_left >= 1) { | |
| 361 ext_type = (buf[0] >> 4); | |
| 362 switch(ext_type) { | |
| 363 case 0x1: /* sequence extension */ | |
| 364 if (bytes_left >= 6) { | |
| 365 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7); | |
| 366 vert_size_ext = (buf[2] >> 5) & 3; | |
| 2539 | 367 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1); |
| 1613 | 368 frame_rate_ext_n = (buf[5] >> 5) & 3; |
| 369 frame_rate_ext_d = (buf[5] & 0x1f); | |
| 370 pc->progressive_sequence = buf[1] & (1 << 3); | |
| 2565 | 371 avctx->has_b_frames= !(buf[5] >> 7); |
| 1613 | 372 |
| 2269 | 373 pc->width |=(horiz_size_ext << 12); |
| 374 pc->height |=( vert_size_ext << 12); | |
| 2539 | 375 avctx->bit_rate += (bit_rate_ext << 18) * 400; |
| 2270 | 376 avcodec_set_dimensions(avctx, pc->width, pc->height); |
| 2637 | 377 avctx->time_base.den = pc->frame_rate * (frame_rate_ext_n + 1); |
| 378 avctx->time_base.num = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1); | |
| 1681 | 379 avctx->codec_id = CODEC_ID_MPEG2VIDEO; |
| 1613 | 380 avctx->sub_id = 2; /* forces MPEG2 */ |
| 381 } | |
| 382 break; | |
| 383 case 0x8: /* picture coding extension */ | |
| 384 if (bytes_left >= 5) { | |
| 2120 | 385 picture_structure = buf[2]&3; |
| 1613 | 386 top_field_first = buf[3] & (1 << 7); |
| 387 repeat_first_field = buf[3] & (1 << 1); | |
| 388 progressive_frame = buf[4] & (1 << 7); | |
| 2967 | 389 |
| 1613 | 390 /* check if we must repeat the frame */ |
| 391 if (repeat_first_field) { | |
| 392 if (pc->progressive_sequence) { | |
| 393 if (top_field_first) | |
| 394 s->repeat_pict = 4; | |
| 395 else | |
| 396 s->repeat_pict = 2; | |
| 397 } else if (progressive_frame) { | |
| 398 s->repeat_pict = 1; | |
| 399 } | |
| 400 } | |
| 2967 | 401 |
| 402 /* the packet only represents half a frame | |
| 2119 | 403 XXX,FIXME maybe find a different solution */ |
| 404 if(picture_structure != 3) | |
| 405 s->repeat_pict = -1; | |
| 1613 | 406 } |
| 407 break; | |
| 408 } | |
| 409 } | |
| 410 break; | |
| 411 case -1: | |
| 412 goto the_end; | |
| 413 default: | |
| 414 /* we stop parsing when we encounter a slice. It ensures | |
| 415 that this function takes a negligible amount of time */ | |
| 2967 | 416 if (start_code >= SLICE_MIN_START_CODE && |
| 1613 | 417 start_code <= SLICE_MAX_START_CODE) |
| 418 goto the_end; | |
| 419 break; | |
| 420 } | |
| 421 } | |
| 422 the_end: ; | |
| 423 } | |
| 424 | |
| 425 static int mpegvideo_parse(AVCodecParserContext *s, | |
| 426 AVCodecContext *avctx, | |
| 2967 | 427 uint8_t **poutbuf, int *poutbuf_size, |
| 1613 | 428 const uint8_t *buf, int buf_size) |
| 429 { | |
| 1988 | 430 ParseContext1 *pc1 = s->priv_data; |
| 431 ParseContext *pc= &pc1->pc; | |
| 1613 | 432 int next; |
| 2967 | 433 |
| 2837 | 434 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ |
| 435 next= buf_size; | |
| 436 }else{ | |
| 437 next= ff_mpeg1_find_frame_end(pc, buf, buf_size); | |
| 2967 | 438 |
| 2837 | 439 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
| 440 *poutbuf = NULL; | |
| 441 *poutbuf_size = 0; | |
| 442 return buf_size; | |
| 443 } | |
| 2967 | 444 |
| 1613 | 445 } |
| 446 /* we have a full frame : we just parse the first few MPEG headers | |
| 447 to have the full timing information. The time take by this | |
| 448 function should be negligible for uncorrupted streams */ | |
| 449 mpegvideo_extract_headers(s, avctx, buf, buf_size); | |
| 450 #if 0 | |
| 2967 | 451 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", |
| 2637 | 452 s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict); |
| 1613 | 453 #endif |
| 454 | |
| 455 *poutbuf = (uint8_t *)buf; | |
| 456 *poutbuf_size = buf_size; | |
| 457 return next; | |
| 458 } | |
| 459 | |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
460 static int mpegvideo_split(AVCodecContext *avctx, |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
461 const uint8_t *buf, int buf_size) |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
462 { |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
463 int i; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
464 uint32_t state= -1; |
| 2967 | 465 |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
466 for(i=0; i<buf_size; i++){ |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
467 state= (state<<8) | buf[i]; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
468 if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) |
| 2777 | 469 return i-3; |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
470 } |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
471 return 0; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
472 } |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
473 |
| 1988 | 474 void ff_parse_close(AVCodecParserContext *s) |
| 1613 | 475 { |
| 1988 | 476 ParseContext *pc = s->priv_data; |
| 1613 | 477 |
| 478 av_free(pc->buffer); | |
| 1988 | 479 } |
| 480 | |
| 481 static void parse1_close(AVCodecParserContext *s) | |
| 482 { | |
| 483 ParseContext1 *pc1 = s->priv_data; | |
| 484 | |
| 485 av_free(pc1->pc.buffer); | |
| 486 av_free(pc1->enc); | |
| 1613 | 487 } |
| 488 | |
| 489 /*************************/ | |
| 490 | |
| 491 /* used by parser */ | |
| 492 /* XXX: make it use less memory */ | |
| 2967 | 493 static int av_mpeg4_decode_header(AVCodecParserContext *s1, |
| 1613 | 494 AVCodecContext *avctx, |
| 495 const uint8_t *buf, int buf_size) | |
| 496 { | |
| 497 ParseContext1 *pc = s1->priv_data; | |
| 498 MpegEncContext *s = pc->enc; | |
| 499 GetBitContext gb1, *gb = &gb1; | |
| 500 int ret; | |
| 501 | |
| 502 s->avctx = avctx; | |
| 1614 | 503 s->current_picture_ptr = &s->current_picture; |
| 504 | |
| 505 if (avctx->extradata_size && pc->first_picture){ | |
| 506 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8); | |
| 507 ret = ff_mpeg4_decode_picture_header(s, gb); | |
| 508 } | |
| 509 | |
| 1613 | 510 init_get_bits(gb, buf, 8 * buf_size); |
| 511 ret = ff_mpeg4_decode_picture_header(s, gb); | |
| 512 if (s->width) { | |
| 2270 | 513 avcodec_set_dimensions(avctx, s->width, s->height); |
| 1613 | 514 } |
| 2837 | 515 s1->pict_type= s->pict_type; |
| 1614 | 516 pc->first_picture = 0; |
| 1613 | 517 return ret; |
| 518 } | |
| 519 | |
|
2024
f65d87bfdd5a
some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents:
1988
diff
changeset
|
520 static int mpeg4video_parse_init(AVCodecParserContext *s) |
| 1613 | 521 { |
| 522 ParseContext1 *pc = s->priv_data; | |
| 1614 | 523 |
| 1613 | 524 pc->enc = av_mallocz(sizeof(MpegEncContext)); |
| 525 if (!pc->enc) | |
| 526 return -1; | |
| 1614 | 527 pc->first_picture = 1; |
| 1613 | 528 return 0; |
| 529 } | |
| 530 | |
| 531 static int mpeg4video_parse(AVCodecParserContext *s, | |
| 532 AVCodecContext *avctx, | |
| 2967 | 533 uint8_t **poutbuf, int *poutbuf_size, |
| 1613 | 534 const uint8_t *buf, int buf_size) |
| 535 { | |
| 1988 | 536 ParseContext *pc = s->priv_data; |
| 1613 | 537 int next; |
| 2967 | 538 |
| 2837 | 539 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ |
| 540 next= buf_size; | |
| 541 }else{ | |
| 542 next= ff_mpeg4_find_frame_end(pc, buf, buf_size); | |
| 2967 | 543 |
| 2837 | 544 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
| 545 *poutbuf = NULL; | |
| 546 *poutbuf_size = 0; | |
| 547 return buf_size; | |
| 548 } | |
| 1613 | 549 } |
| 550 av_mpeg4_decode_header(s, avctx, buf, buf_size); | |
| 551 | |
| 552 *poutbuf = (uint8_t *)buf; | |
| 553 *poutbuf_size = buf_size; | |
| 554 return next; | |
| 555 } | |
| 556 | |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
557 static int mpeg4video_split(AVCodecContext *avctx, |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
558 const uint8_t *buf, int buf_size) |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
559 { |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
560 int i; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
561 uint32_t state= -1; |
| 2967 | 562 |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
563 for(i=0; i<buf_size; i++){ |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
564 state= (state<<8) | buf[i]; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
565 if(state == 0x1B3 || state == 0x1B6) |
| 2777 | 566 return i-3; |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
567 } |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
568 return 0; |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
569 } |
|
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
570 |
| 1613 | 571 /*************************/ |
| 572 | |
| 573 typedef struct MpegAudioParseContext { | |
| 2979 | 574 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */ |
| 1613 | 575 uint8_t *inbuf_ptr; |
| 576 int frame_size; | |
| 577 int free_format_frame_size; | |
| 578 int free_format_next_header; | |
|
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
579 uint32_t header; |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
580 int header_count; |
| 1613 | 581 } MpegAudioParseContext; |
| 582 | |
| 583 #define MPA_HEADER_SIZE 4 | |
| 584 | |
| 585 /* header + layer + bitrate + freq + lsf/mpeg25 */ | |
|
2522
e25782262d7d
kill warnings patch by (M?ns Rullg?rd <mru inprovide com>)
michael
parents:
2486
diff
changeset
|
586 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */ |
| 1613 | 587 #define SAME_HEADER_MASK \ |
| 2480 | 588 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)) |
| 1613 | 589 |
| 590 static int mpegaudio_parse_init(AVCodecParserContext *s1) | |
| 591 { | |
| 592 MpegAudioParseContext *s = s1->priv_data; | |
| 593 s->inbuf_ptr = s->inbuf; | |
| 594 return 0; | |
| 595 } | |
| 596 | |
| 597 static int mpegaudio_parse(AVCodecParserContext *s1, | |
| 598 AVCodecContext *avctx, | |
| 2967 | 599 uint8_t **poutbuf, int *poutbuf_size, |
| 1613 | 600 const uint8_t *buf, int buf_size) |
| 601 { | |
| 602 MpegAudioParseContext *s = s1->priv_data; | |
|
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
603 int len, ret, sr; |
| 1613 | 604 uint32_t header; |
| 605 const uint8_t *buf_ptr; | |
| 606 | |
| 607 *poutbuf = NULL; | |
| 608 *poutbuf_size = 0; | |
| 609 buf_ptr = buf; | |
| 610 while (buf_size > 0) { | |
| 2979 | 611 len = s->inbuf_ptr - s->inbuf; |
| 612 if (s->frame_size == 0) { | |
| 1613 | 613 /* special case for next header for first frame in free |
| 614 format case (XXX: find a simpler method) */ | |
| 615 if (s->free_format_next_header != 0) { | |
| 616 s->inbuf[0] = s->free_format_next_header >> 24; | |
| 617 s->inbuf[1] = s->free_format_next_header >> 16; | |
| 618 s->inbuf[2] = s->free_format_next_header >> 8; | |
| 619 s->inbuf[3] = s->free_format_next_header; | |
| 620 s->inbuf_ptr = s->inbuf + 4; | |
| 621 s->free_format_next_header = 0; | |
| 622 goto got_header; | |
| 623 } | |
| 2979 | 624 /* no header seen : find one. We need at least MPA_HEADER_SIZE |
| 1613 | 625 bytes to parse it */ |
| 2979 | 626 len = MPA_HEADER_SIZE - len; |
| 627 if (len > buf_size) | |
| 628 len = buf_size; | |
| 629 if (len > 0) { | |
| 630 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 631 buf_ptr += len; | |
| 632 buf_size -= len; | |
| 633 s->inbuf_ptr += len; | |
| 634 } | |
| 635 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) { | |
| 1613 | 636 got_header: |
|
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
637 sr= avctx->sample_rate; |
| 2979 | 638 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | |
| 639 (s->inbuf[2] << 8) | s->inbuf[3]; | |
| 1613 | 640 |
| 641 ret = mpa_decode_header(avctx, header); | |
| 642 if (ret < 0) { | |
|
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
643 s->header_count= -2; |
| 2979 | 644 /* no sync found : move by one byte (inefficient, but simple!) */ |
| 645 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
| 646 s->inbuf_ptr--; | |
| 1613 | 647 dprintf("skip %x\n", header); |
| 648 /* reset free format frame size to give a chance | |
| 649 to get a new bitrate */ | |
| 650 s->free_format_frame_size = 0; | |
| 2979 | 651 } else { |
|
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
652 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header) |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
653 s->header_count= -3; |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
654 s->header= header; |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
655 s->header_count++; |
| 1613 | 656 s->frame_size = ret; |
| 2967 | 657 |
| 1613 | 658 #if 0 |
| 659 /* free format: prepare to compute frame size */ | |
| 2979 | 660 if (decode_header(s, header) == 1) { |
| 661 s->frame_size = -1; | |
| 1613 | 662 } |
| 663 #endif | |
| 2979 | 664 } |
|
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
665 if(s->header_count <= 0) |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
666 avctx->sample_rate= sr; //FIXME ugly |
| 2979 | 667 } |
| 2967 | 668 } else |
| 1613 | 669 #if 0 |
| 670 if (s->frame_size == -1) { | |
| 671 /* free format : find next sync to compute frame size */ | |
| 2979 | 672 len = MPA_MAX_CODED_FRAME_SIZE - len; |
| 673 if (len > buf_size) | |
| 674 len = buf_size; | |
| 1613 | 675 if (len == 0) { |
| 2979 | 676 /* frame too long: resync */ |
| 1613 | 677 s->frame_size = 0; |
| 2979 | 678 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); |
| 679 s->inbuf_ptr--; | |
| 1613 | 680 } else { |
| 681 uint8_t *p, *pend; | |
| 682 uint32_t header1; | |
| 683 int padding; | |
| 684 | |
| 685 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 686 /* check for header */ | |
| 687 p = s->inbuf_ptr - 3; | |
| 688 pend = s->inbuf_ptr + len - 4; | |
| 689 while (p <= pend) { | |
| 690 header = (p[0] << 24) | (p[1] << 16) | | |
| 691 (p[2] << 8) | p[3]; | |
| 692 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
| 693 (s->inbuf[2] << 8) | s->inbuf[3]; | |
| 694 /* check with high probability that we have a | |
| 695 valid header */ | |
| 696 if ((header & SAME_HEADER_MASK) == | |
| 697 (header1 & SAME_HEADER_MASK)) { | |
| 698 /* header found: update pointers */ | |
| 699 len = (p + 4) - s->inbuf_ptr; | |
| 700 buf_ptr += len; | |
| 701 buf_size -= len; | |
| 702 s->inbuf_ptr = p; | |
| 703 /* compute frame size */ | |
| 704 s->free_format_next_header = header; | |
| 705 s->free_format_frame_size = s->inbuf_ptr - s->inbuf; | |
| 706 padding = (header1 >> 9) & 1; | |
| 707 if (s->layer == 1) | |
| 708 s->free_format_frame_size -= padding * 4; | |
| 709 else | |
| 710 s->free_format_frame_size -= padding; | |
| 2967 | 711 dprintf("free frame size=%d padding=%d\n", |
| 1613 | 712 s->free_format_frame_size, padding); |
| 713 decode_header(s, header1); | |
| 714 goto next_data; | |
| 715 } | |
| 716 p++; | |
| 717 } | |
| 718 /* not found: simply increase pointers */ | |
| 719 buf_ptr += len; | |
| 720 s->inbuf_ptr += len; | |
| 721 buf_size -= len; | |
| 722 } | |
| 2979 | 723 } else |
| 1613 | 724 #endif |
| 725 if (len < s->frame_size) { | |
| 726 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) | |
| 727 s->frame_size = MPA_MAX_CODED_FRAME_SIZE; | |
| 2979 | 728 len = s->frame_size - len; |
| 729 if (len > buf_size) | |
| 730 len = buf_size; | |
| 731 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 732 buf_ptr += len; | |
| 733 s->inbuf_ptr += len; | |
| 734 buf_size -= len; | |
| 735 } | |
| 1613 | 736 // next_data: |
| 2967 | 737 if (s->frame_size > 0 && |
| 1613 | 738 (s->inbuf_ptr - s->inbuf) >= s->frame_size) { |
|
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
739 if(s->header_count > 0){ |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
740 *poutbuf = s->inbuf; |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
741 *poutbuf_size = s->inbuf_ptr - s->inbuf; |
|
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
742 } |
| 2979 | 743 s->inbuf_ptr = s->inbuf; |
| 744 s->frame_size = 0; | |
| 745 break; | |
| 746 } | |
| 1613 | 747 } |
| 748 return buf_ptr - buf; | |
| 749 } | |
| 750 | |
| 751 typedef struct AC3ParseContext { | |
| 752 uint8_t inbuf[4096]; /* input buffer */ | |
| 753 uint8_t *inbuf_ptr; | |
| 754 int frame_size; | |
| 755 } AC3ParseContext; | |
| 756 | |
| 757 #define AC3_HEADER_SIZE 7 | |
| 3059 | 758 |
| 759 static const int ac3_sample_rates[4] = { | |
| 760 48000, 44100, 32000, 0 | |
| 761 }; | |
| 762 | |
| 763 static const int ac3_frame_sizes[64][3] = { | |
| 764 { 64, 69, 96 }, | |
| 765 { 64, 70, 96 }, | |
| 766 { 80, 87, 120 }, | |
| 767 { 80, 88, 120 }, | |
| 768 { 96, 104, 144 }, | |
| 769 { 96, 105, 144 }, | |
| 770 { 112, 121, 168 }, | |
| 771 { 112, 122, 168 }, | |
| 772 { 128, 139, 192 }, | |
| 773 { 128, 140, 192 }, | |
| 774 { 160, 174, 240 }, | |
| 775 { 160, 175, 240 }, | |
| 776 { 192, 208, 288 }, | |
| 777 { 192, 209, 288 }, | |
| 778 { 224, 243, 336 }, | |
| 779 { 224, 244, 336 }, | |
| 780 { 256, 278, 384 }, | |
| 781 { 256, 279, 384 }, | |
| 782 { 320, 348, 480 }, | |
| 783 { 320, 349, 480 }, | |
| 784 { 384, 417, 576 }, | |
| 785 { 384, 418, 576 }, | |
| 786 { 448, 487, 672 }, | |
| 787 { 448, 488, 672 }, | |
| 788 { 512, 557, 768 }, | |
| 789 { 512, 558, 768 }, | |
| 790 { 640, 696, 960 }, | |
| 791 { 640, 697, 960 }, | |
| 792 { 768, 835, 1152 }, | |
| 793 { 768, 836, 1152 }, | |
| 794 { 896, 975, 1344 }, | |
| 795 { 896, 976, 1344 }, | |
| 796 { 1024, 1114, 1536 }, | |
| 797 { 1024, 1115, 1536 }, | |
| 798 { 1152, 1253, 1728 }, | |
| 799 { 1152, 1254, 1728 }, | |
| 800 { 1280, 1393, 1920 }, | |
| 801 { 1280, 1394, 1920 }, | |
| 802 }; | |
| 803 | |
| 804 static const int ac3_bitrates[64] = { | |
| 805 32, 32, 40, 40, 48, 48, 56, 56, 64, 64, 80, 80, 96, 96, 112, 112, | |
| 806 128, 128, 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384, | |
| 807 384, 448, 448, 512, 512, 576, 576, 640, 640, | |
| 808 }; | |
| 809 | |
| 810 static const int ac3_channels[8] = { | |
| 811 2, 1, 2, 3, 3, 4, 4, 5 | |
| 812 }; | |
| 813 | |
| 814 static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate, | |
| 815 int *bit_rate) | |
| 816 { | |
| 817 unsigned int fscod, frmsizecod, acmod, bsid, lfeon; | |
| 818 GetBitContext bits; | |
| 819 | |
| 820 init_get_bits(&bits, buf, AC3_HEADER_SIZE * 8); | |
| 821 | |
| 822 if(get_bits(&bits, 16) != 0x0b77) | |
| 823 return 0; | |
| 824 | |
| 825 get_bits(&bits, 16); /* crc */ | |
| 826 fscod = get_bits(&bits, 2); | |
| 827 frmsizecod = get_bits(&bits, 6); | |
| 828 | |
| 829 if(!ac3_sample_rates[fscod]) | |
| 830 return 0; | |
| 831 | |
| 832 bsid = get_bits(&bits, 5); | |
| 833 if(bsid > 8) | |
| 834 return 0; | |
| 835 get_bits(&bits, 3); /* bsmod */ | |
| 836 acmod = get_bits(&bits, 3); | |
| 837 if(acmod & 1 && acmod != 1) | |
| 838 get_bits(&bits, 2); /* cmixlev */ | |
| 839 if(acmod & 4) | |
| 840 get_bits(&bits, 2); /* surmixlev */ | |
| 841 if(acmod & 2) | |
| 842 get_bits(&bits, 2); /* dsurmod */ | |
| 843 lfeon = get_bits(&bits, 1); | |
| 844 | |
| 845 *sample_rate = ac3_sample_rates[fscod]; | |
| 846 *bit_rate = ac3_bitrates[frmsizecod] * 1000; | |
| 847 *channels = ac3_channels[acmod] + lfeon; | |
| 848 | |
| 849 return ac3_frame_sizes[frmsizecod][fscod] * 2; | |
| 850 } | |
| 1613 | 851 |
| 852 static int ac3_parse_init(AVCodecParserContext *s1) | |
| 853 { | |
| 854 AC3ParseContext *s = s1->priv_data; | |
| 855 s->inbuf_ptr = s->inbuf; | |
| 856 return 0; | |
| 857 } | |
| 858 | |
| 859 static int ac3_parse(AVCodecParserContext *s1, | |
| 860 AVCodecContext *avctx, | |
| 2967 | 861 uint8_t **poutbuf, int *poutbuf_size, |
| 1613 | 862 const uint8_t *buf, int buf_size) |
| 863 { | |
| 864 AC3ParseContext *s = s1->priv_data; | |
| 865 const uint8_t *buf_ptr; | |
| 3059 | 866 int len, sample_rate, bit_rate, channels; |
| 1613 | 867 |
| 868 *poutbuf = NULL; | |
| 869 *poutbuf_size = 0; | |
| 870 | |
| 871 buf_ptr = buf; | |
| 872 while (buf_size > 0) { | |
| 873 len = s->inbuf_ptr - s->inbuf; | |
| 874 if (s->frame_size == 0) { | |
| 875 /* no header seen : find one. We need at least 7 bytes to parse it */ | |
| 876 len = AC3_HEADER_SIZE - len; | |
| 877 if (len > buf_size) | |
| 878 len = buf_size; | |
| 879 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 880 buf_ptr += len; | |
| 881 s->inbuf_ptr += len; | |
| 882 buf_size -= len; | |
| 883 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) { | |
| 3059 | 884 len = ac3_sync(s->inbuf, &channels, &sample_rate, &bit_rate); |
| 1613 | 885 if (len == 0) { |
| 886 /* no sync found : move by one byte (inefficient, but simple!) */ | |
| 887 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1); | |
| 888 s->inbuf_ptr--; | |
| 889 } else { | |
| 2979 | 890 s->frame_size = len; |
| 1613 | 891 /* update codec info */ |
| 892 avctx->sample_rate = sample_rate; | |
| 1987 | 893 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */ |
| 894 if(avctx->channels!=1 && avctx->channels!=2){ | |
| 3059 | 895 avctx->channels = channels; |
| 1987 | 896 } |
| 2979 | 897 avctx->bit_rate = bit_rate; |
| 1613 | 898 avctx->frame_size = 6 * 256; |
| 899 } | |
| 900 } | |
| 901 } else if (len < s->frame_size) { | |
| 902 len = s->frame_size - len; | |
| 903 if (len > buf_size) | |
| 904 len = buf_size; | |
| 905 | |
| 906 memcpy(s->inbuf_ptr, buf_ptr, len); | |
| 907 buf_ptr += len; | |
| 908 s->inbuf_ptr += len; | |
| 909 buf_size -= len; | |
| 910 } else { | |
| 911 *poutbuf = s->inbuf; | |
| 912 *poutbuf_size = s->frame_size; | |
| 913 s->inbuf_ptr = s->inbuf; | |
| 914 s->frame_size = 0; | |
| 915 break; | |
| 916 } | |
| 917 } | |
| 918 return buf_ptr - buf; | |
| 919 } | |
| 920 | |
| 921 AVCodecParser mpegvideo_parser = { | |
| 922 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO }, | |
| 923 sizeof(ParseContext1), | |
| 924 NULL, | |
| 925 mpegvideo_parse, | |
| 1988 | 926 parse1_close, |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
927 mpegvideo_split, |
| 1613 | 928 }; |
| 929 | |
| 930 AVCodecParser mpeg4video_parser = { | |
| 931 { CODEC_ID_MPEG4 }, | |
| 932 sizeof(ParseContext1), | |
| 933 mpeg4video_parse_init, | |
| 934 mpeg4video_parse, | |
| 1988 | 935 parse1_close, |
|
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
936 mpeg4video_split, |
| 1613 | 937 }; |
| 938 | |
| 939 AVCodecParser mpegaudio_parser = { | |
| 940 { CODEC_ID_MP2, CODEC_ID_MP3 }, | |
| 941 sizeof(MpegAudioParseContext), | |
| 942 mpegaudio_parse_init, | |
| 943 mpegaudio_parse, | |
| 944 NULL, | |
| 945 }; | |
| 946 | |
| 947 AVCodecParser ac3_parser = { | |
| 948 { CODEC_ID_AC3 }, | |
| 949 sizeof(AC3ParseContext), | |
| 950 ac3_parse_init, | |
| 951 ac3_parse, | |
| 952 NULL, | |
| 953 }; |
