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.

760 lines
24KB

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