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.

908 lines
27KB

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