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.

893 lines
26KB

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