You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

884 lines
28KB

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