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.

905 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. 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. }
  264. break;
  265. case EXT_START_CODE:
  266. if (bytes_left >= 1) {
  267. ext_type = (buf[0] >> 4);
  268. switch(ext_type) {
  269. case 0x1: /* sequence extension */
  270. if (bytes_left >= 6) {
  271. horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
  272. vert_size_ext = (buf[2] >> 5) & 3;
  273. frame_rate_ext_n = (buf[5] >> 5) & 3;
  274. frame_rate_ext_d = (buf[5] & 0x1f);
  275. pc->progressive_sequence = buf[1] & (1 << 3);
  276. avctx->width = pc->width | (horiz_size_ext << 12);
  277. avctx->height = pc->height | (vert_size_ext << 12);
  278. avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1);
  279. avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
  280. avctx->sub_id = 2; /* forces MPEG2 */
  281. }
  282. break;
  283. case 0x8: /* picture coding extension */
  284. if (bytes_left >= 5) {
  285. top_field_first = buf[3] & (1 << 7);
  286. repeat_first_field = buf[3] & (1 << 1);
  287. progressive_frame = buf[4] & (1 << 7);
  288. /* check if we must repeat the frame */
  289. if (repeat_first_field) {
  290. if (pc->progressive_sequence) {
  291. if (top_field_first)
  292. s->repeat_pict = 4;
  293. else
  294. s->repeat_pict = 2;
  295. } else if (progressive_frame) {
  296. s->repeat_pict = 1;
  297. }
  298. }
  299. }
  300. break;
  301. }
  302. }
  303. break;
  304. case -1:
  305. goto the_end;
  306. default:
  307. /* we stop parsing when we encounter a slice. It ensures
  308. that this function takes a negligible amount of time */
  309. if (start_code >= SLICE_MIN_START_CODE &&
  310. start_code <= SLICE_MAX_START_CODE)
  311. goto the_end;
  312. break;
  313. }
  314. }
  315. the_end: ;
  316. }
  317. static int mpegvideo_parse(AVCodecParserContext *s,
  318. AVCodecContext *avctx,
  319. uint8_t **poutbuf, int *poutbuf_size,
  320. const uint8_t *buf, int buf_size)
  321. {
  322. ParseContext1 *pc = s->priv_data;
  323. int next;
  324. next= mpeg1_find_frame_end(pc, buf, buf_size);
  325. if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  326. *poutbuf = NULL;
  327. *poutbuf_size = 0;
  328. return buf_size;
  329. }
  330. /* we have a full frame : we just parse the first few MPEG headers
  331. to have the full timing information. The time take by this
  332. function should be negligible for uncorrupted streams */
  333. mpegvideo_extract_headers(s, avctx, buf, buf_size);
  334. #if 0
  335. printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
  336. s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict);
  337. #endif
  338. *poutbuf = (uint8_t *)buf;
  339. *poutbuf_size = buf_size;
  340. return next;
  341. }
  342. static void mpegvideo_parse_close(AVCodecParserContext *s)
  343. {
  344. ParseContext1 *pc = s->priv_data;
  345. av_free(pc->buffer);
  346. av_free(pc->enc);
  347. }
  348. /*************************/
  349. /**
  350. * finds the end of the current frame in the bitstream.
  351. * @return the position of the first byte of the next frame, or -1
  352. */
  353. static int mpeg4_find_frame_end(ParseContext1 *pc,
  354. const uint8_t *buf, int buf_size)
  355. {
  356. int vop_found, i;
  357. uint32_t state;
  358. vop_found= pc->frame_start_found;
  359. state= pc->state;
  360. i=0;
  361. if(!vop_found){
  362. for(i=0; i<buf_size; i++){
  363. state= (state<<8) | buf[i];
  364. if(state == 0x1B6){
  365. i++;
  366. vop_found=1;
  367. break;
  368. }
  369. }
  370. }
  371. if(vop_found){
  372. for(; i<buf_size; i++){
  373. state= (state<<8) | buf[i];
  374. if((state&0xFFFFFF00) == 0x100){
  375. pc->frame_start_found=0;
  376. pc->state=-1;
  377. return i-3;
  378. }
  379. }
  380. }
  381. pc->frame_start_found= vop_found;
  382. pc->state= state;
  383. return END_NOT_FOUND;
  384. }
  385. /* used by parser */
  386. /* XXX: make it use less memory */
  387. static int av_mpeg4_decode_header(AVCodecParserContext *s1,
  388. AVCodecContext *avctx,
  389. const uint8_t *buf, int buf_size)
  390. {
  391. ParseContext1 *pc = s1->priv_data;
  392. MpegEncContext *s = pc->enc;
  393. GetBitContext gb1, *gb = &gb1;
  394. int ret;
  395. s->avctx = avctx;
  396. s->current_picture_ptr = &s->current_picture;
  397. if (avctx->extradata_size && pc->first_picture){
  398. init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
  399. ret = ff_mpeg4_decode_picture_header(s, gb);
  400. }
  401. init_get_bits(gb, buf, 8 * buf_size);
  402. ret = ff_mpeg4_decode_picture_header(s, gb);
  403. if (s->width) {
  404. avctx->width = s->width;
  405. avctx->height = s->height;
  406. }
  407. pc->first_picture = 0;
  408. return ret;
  409. }
  410. int mpeg4video_parse_init(AVCodecParserContext *s)
  411. {
  412. ParseContext1 *pc = s->priv_data;
  413. pc->enc = av_mallocz(sizeof(MpegEncContext));
  414. if (!pc->enc)
  415. return -1;
  416. pc->first_picture = 1;
  417. return 0;
  418. }
  419. static int mpeg4video_parse(AVCodecParserContext *s,
  420. AVCodecContext *avctx,
  421. uint8_t **poutbuf, int *poutbuf_size,
  422. const uint8_t *buf, int buf_size)
  423. {
  424. ParseContext1 *pc = s->priv_data;
  425. int next;
  426. next= mpeg4_find_frame_end(pc, buf, buf_size);
  427. if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  428. *poutbuf = NULL;
  429. *poutbuf_size = 0;
  430. return buf_size;
  431. }
  432. av_mpeg4_decode_header(s, avctx, buf, buf_size);
  433. *poutbuf = (uint8_t *)buf;
  434. *poutbuf_size = buf_size;
  435. return next;
  436. }
  437. /*************************/
  438. static int h263_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
  439. {
  440. int vop_found, i;
  441. uint32_t state;
  442. vop_found= pc->frame_start_found;
  443. state= pc->state;
  444. i=0;
  445. if(!vop_found){
  446. for(i=0; i<buf_size; i++){
  447. state= (state<<8) | buf[i];
  448. if(state>>(32-22) == 0x20){
  449. i++;
  450. vop_found=1;
  451. break;
  452. }
  453. }
  454. }
  455. if(vop_found){
  456. for(; i<buf_size; i++){
  457. state= (state<<8) | buf[i];
  458. if(state>>(32-22) == 0x20){
  459. pc->frame_start_found=0;
  460. pc->state=-1;
  461. return i-3;
  462. }
  463. }
  464. }
  465. pc->frame_start_found= vop_found;
  466. pc->state= state;
  467. return END_NOT_FOUND;
  468. }
  469. static int h263_parse(AVCodecParserContext *s,
  470. AVCodecContext *avctx,
  471. uint8_t **poutbuf, int *poutbuf_size,
  472. const uint8_t *buf, int buf_size)
  473. {
  474. ParseContext1 *pc = s->priv_data;
  475. int next;
  476. next= h263_find_frame_end(pc, buf, buf_size);
  477. if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  478. *poutbuf = NULL;
  479. *poutbuf_size = 0;
  480. return buf_size;
  481. }
  482. *poutbuf = (uint8_t *)buf;
  483. *poutbuf_size = buf_size;
  484. return next;
  485. }
  486. /*************************/
  487. /**
  488. * finds the end of the current frame in the bitstream.
  489. * @return the position of the first byte of the next frame, or -1
  490. */
  491. static int h264_find_frame_end(ParseContext1 *pc, const uint8_t *buf, int buf_size)
  492. {
  493. int i;
  494. uint32_t state;
  495. //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
  496. // mb_addr= pc->mb_addr - 1;
  497. state= pc->state;
  498. //FIXME this will fail with slices
  499. for(i=0; i<buf_size; i++){
  500. state= (state<<8) | buf[i];
  501. if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
  502. if(pc->frame_start_found){
  503. pc->state=-1;
  504. pc->frame_start_found= 0;
  505. return i-3;
  506. }
  507. pc->frame_start_found= 1;
  508. }
  509. }
  510. pc->state= state;
  511. return END_NOT_FOUND;
  512. }
  513. static int h264_parse(AVCodecParserContext *s,
  514. AVCodecContext *avctx,
  515. uint8_t **poutbuf, int *poutbuf_size,
  516. const uint8_t *buf, int buf_size)
  517. {
  518. ParseContext1 *pc = s->priv_data;
  519. int next;
  520. next= h264_find_frame_end(pc, buf, buf_size);
  521. if (ff_combine_frame1(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  522. *poutbuf = NULL;
  523. *poutbuf_size = 0;
  524. return buf_size;
  525. }
  526. *poutbuf = (uint8_t *)buf;
  527. *poutbuf_size = buf_size;
  528. return next;
  529. }
  530. /*************************/
  531. typedef struct MpegAudioParseContext {
  532. uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
  533. uint8_t *inbuf_ptr;
  534. int frame_size;
  535. int free_format_frame_size;
  536. int free_format_next_header;
  537. } MpegAudioParseContext;
  538. #define MPA_HEADER_SIZE 4
  539. /* header + layer + bitrate + freq + lsf/mpeg25 */
  540. #define SAME_HEADER_MASK \
  541. (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
  542. static int mpegaudio_parse_init(AVCodecParserContext *s1)
  543. {
  544. MpegAudioParseContext *s = s1->priv_data;
  545. s->inbuf_ptr = s->inbuf;
  546. return 0;
  547. }
  548. static int mpegaudio_parse(AVCodecParserContext *s1,
  549. AVCodecContext *avctx,
  550. uint8_t **poutbuf, int *poutbuf_size,
  551. const uint8_t *buf, int buf_size)
  552. {
  553. MpegAudioParseContext *s = s1->priv_data;
  554. int len, ret;
  555. uint32_t header;
  556. const uint8_t *buf_ptr;
  557. *poutbuf = NULL;
  558. *poutbuf_size = 0;
  559. buf_ptr = buf;
  560. while (buf_size > 0) {
  561. len = s->inbuf_ptr - s->inbuf;
  562. if (s->frame_size == 0) {
  563. /* special case for next header for first frame in free
  564. format case (XXX: find a simpler method) */
  565. if (s->free_format_next_header != 0) {
  566. s->inbuf[0] = s->free_format_next_header >> 24;
  567. s->inbuf[1] = s->free_format_next_header >> 16;
  568. s->inbuf[2] = s->free_format_next_header >> 8;
  569. s->inbuf[3] = s->free_format_next_header;
  570. s->inbuf_ptr = s->inbuf + 4;
  571. s->free_format_next_header = 0;
  572. goto got_header;
  573. }
  574. /* no header seen : find one. We need at least MPA_HEADER_SIZE
  575. bytes to parse it */
  576. len = MPA_HEADER_SIZE - len;
  577. if (len > buf_size)
  578. len = buf_size;
  579. if (len > 0) {
  580. memcpy(s->inbuf_ptr, buf_ptr, len);
  581. buf_ptr += len;
  582. buf_size -= len;
  583. s->inbuf_ptr += len;
  584. }
  585. if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
  586. got_header:
  587. header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  588. (s->inbuf[2] << 8) | s->inbuf[3];
  589. ret = mpa_decode_header(avctx, header);
  590. if (ret < 0) {
  591. /* no sync found : move by one byte (inefficient, but simple!) */
  592. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  593. s->inbuf_ptr--;
  594. dprintf("skip %x\n", header);
  595. /* reset free format frame size to give a chance
  596. to get a new bitrate */
  597. s->free_format_frame_size = 0;
  598. } else {
  599. s->frame_size = ret;
  600. #if 0
  601. /* free format: prepare to compute frame size */
  602. if (decode_header(s, header) == 1) {
  603. s->frame_size = -1;
  604. }
  605. #endif
  606. }
  607. }
  608. } else
  609. #if 0
  610. if (s->frame_size == -1) {
  611. /* free format : find next sync to compute frame size */
  612. len = MPA_MAX_CODED_FRAME_SIZE - len;
  613. if (len > buf_size)
  614. len = buf_size;
  615. if (len == 0) {
  616. /* frame too long: resync */
  617. s->frame_size = 0;
  618. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  619. s->inbuf_ptr--;
  620. } else {
  621. uint8_t *p, *pend;
  622. uint32_t header1;
  623. int padding;
  624. memcpy(s->inbuf_ptr, buf_ptr, len);
  625. /* check for header */
  626. p = s->inbuf_ptr - 3;
  627. pend = s->inbuf_ptr + len - 4;
  628. while (p <= pend) {
  629. header = (p[0] << 24) | (p[1] << 16) |
  630. (p[2] << 8) | p[3];
  631. header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  632. (s->inbuf[2] << 8) | s->inbuf[3];
  633. /* check with high probability that we have a
  634. valid header */
  635. if ((header & SAME_HEADER_MASK) ==
  636. (header1 & SAME_HEADER_MASK)) {
  637. /* header found: update pointers */
  638. len = (p + 4) - s->inbuf_ptr;
  639. buf_ptr += len;
  640. buf_size -= len;
  641. s->inbuf_ptr = p;
  642. /* compute frame size */
  643. s->free_format_next_header = header;
  644. s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
  645. padding = (header1 >> 9) & 1;
  646. if (s->layer == 1)
  647. s->free_format_frame_size -= padding * 4;
  648. else
  649. s->free_format_frame_size -= padding;
  650. dprintf("free frame size=%d padding=%d\n",
  651. s->free_format_frame_size, padding);
  652. decode_header(s, header1);
  653. goto next_data;
  654. }
  655. p++;
  656. }
  657. /* not found: simply increase pointers */
  658. buf_ptr += len;
  659. s->inbuf_ptr += len;
  660. buf_size -= len;
  661. }
  662. } else
  663. #endif
  664. if (len < s->frame_size) {
  665. if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
  666. s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
  667. len = s->frame_size - len;
  668. if (len > buf_size)
  669. len = buf_size;
  670. memcpy(s->inbuf_ptr, buf_ptr, len);
  671. buf_ptr += len;
  672. s->inbuf_ptr += len;
  673. buf_size -= len;
  674. }
  675. // next_data:
  676. if (s->frame_size > 0 &&
  677. (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
  678. *poutbuf = s->inbuf;
  679. *poutbuf_size = s->inbuf_ptr - s->inbuf;
  680. s->inbuf_ptr = s->inbuf;
  681. s->frame_size = 0;
  682. break;
  683. }
  684. }
  685. return buf_ptr - buf;
  686. }
  687. #ifdef CONFIG_AC3
  688. extern int a52_syncinfo (const uint8_t * buf, int * flags,
  689. int * sample_rate, int * bit_rate);
  690. typedef struct AC3ParseContext {
  691. uint8_t inbuf[4096]; /* input buffer */
  692. uint8_t *inbuf_ptr;
  693. int frame_size;
  694. int flags;
  695. } AC3ParseContext;
  696. #define AC3_HEADER_SIZE 7
  697. #define A52_LFE 16
  698. static int ac3_parse_init(AVCodecParserContext *s1)
  699. {
  700. AC3ParseContext *s = s1->priv_data;
  701. s->inbuf_ptr = s->inbuf;
  702. return 0;
  703. }
  704. static int ac3_parse(AVCodecParserContext *s1,
  705. AVCodecContext *avctx,
  706. uint8_t **poutbuf, int *poutbuf_size,
  707. const uint8_t *buf, int buf_size)
  708. {
  709. AC3ParseContext *s = s1->priv_data;
  710. const uint8_t *buf_ptr;
  711. int len, sample_rate, bit_rate;
  712. static const int ac3_channels[8] = {
  713. 2, 1, 2, 3, 3, 4, 4, 5
  714. };
  715. *poutbuf = NULL;
  716. *poutbuf_size = 0;
  717. buf_ptr = buf;
  718. while (buf_size > 0) {
  719. len = s->inbuf_ptr - s->inbuf;
  720. if (s->frame_size == 0) {
  721. /* no header seen : find one. We need at least 7 bytes to parse it */
  722. len = AC3_HEADER_SIZE - len;
  723. if (len > buf_size)
  724. len = buf_size;
  725. memcpy(s->inbuf_ptr, buf_ptr, len);
  726. buf_ptr += len;
  727. s->inbuf_ptr += len;
  728. buf_size -= len;
  729. if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
  730. len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
  731. if (len == 0) {
  732. /* no sync found : move by one byte (inefficient, but simple!) */
  733. memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
  734. s->inbuf_ptr--;
  735. } else {
  736. s->frame_size = len;
  737. /* update codec info */
  738. avctx->sample_rate = sample_rate;
  739. avctx->channels = ac3_channels[s->flags & 7];
  740. if (s->flags & A52_LFE)
  741. avctx->channels++;
  742. avctx->bit_rate = bit_rate;
  743. avctx->frame_size = 6 * 256;
  744. }
  745. }
  746. } else if (len < s->frame_size) {
  747. len = s->frame_size - len;
  748. if (len > buf_size)
  749. len = buf_size;
  750. memcpy(s->inbuf_ptr, buf_ptr, len);
  751. buf_ptr += len;
  752. s->inbuf_ptr += len;
  753. buf_size -= len;
  754. } else {
  755. *poutbuf = s->inbuf;
  756. *poutbuf_size = s->frame_size;
  757. s->inbuf_ptr = s->inbuf;
  758. s->frame_size = 0;
  759. break;
  760. }
  761. }
  762. return buf_ptr - buf;
  763. }
  764. #endif
  765. AVCodecParser mpegvideo_parser = {
  766. { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
  767. sizeof(ParseContext1),
  768. NULL,
  769. mpegvideo_parse,
  770. mpegvideo_parse_close,
  771. };
  772. AVCodecParser mpeg4video_parser = {
  773. { CODEC_ID_MPEG4 },
  774. sizeof(ParseContext1),
  775. mpeg4video_parse_init,
  776. mpeg4video_parse,
  777. mpegvideo_parse_close,
  778. };
  779. AVCodecParser h263_parser = {
  780. { CODEC_ID_H263 },
  781. sizeof(ParseContext1),
  782. NULL,
  783. h263_parse,
  784. mpegvideo_parse_close,
  785. };
  786. AVCodecParser h264_parser = {
  787. { CODEC_ID_H264 },
  788. sizeof(ParseContext1),
  789. NULL,
  790. h264_parse,
  791. mpegvideo_parse_close,
  792. };
  793. AVCodecParser mpegaudio_parser = {
  794. { CODEC_ID_MP2, CODEC_ID_MP3 },
  795. sizeof(MpegAudioParseContext),
  796. mpegaudio_parse_init,
  797. mpegaudio_parse,
  798. NULL,
  799. };
  800. #ifdef CONFIG_AC3
  801. AVCodecParser ac3_parser = {
  802. { CODEC_ID_AC3 },
  803. sizeof(AC3ParseContext),
  804. ac3_parse_init,
  805. ac3_parse,
  806. NULL,
  807. };
  808. #endif