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.

1087 lines
34KB

  1. /*
  2. * Audio and Video frame extraction
  3. * Copyright (c) 2003 Fabrice Bellard.
  4. * Copyright (c) 2003 Michael Niedermayer.
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "mpegvideo.h"
  24. #include "mpegaudio.h"
  25. AVCodecParser *av_first_parser = NULL;
  26. void av_register_codec_parser(AVCodecParser *parser)
  27. {
  28. parser->next = av_first_parser;
  29. av_first_parser = parser;
  30. }
  31. AVCodecParserContext *av_parser_init(int codec_id)
  32. {
  33. AVCodecParserContext *s;
  34. AVCodecParser *parser;
  35. int ret;
  36. if(codec_id == CODEC_ID_NONE)
  37. return NULL;
  38. for(parser = av_first_parser; parser != NULL; parser = parser->next) {
  39. if (parser->codec_ids[0] == codec_id ||
  40. parser->codec_ids[1] == codec_id ||
  41. parser->codec_ids[2] == codec_id ||
  42. parser->codec_ids[3] == codec_id ||
  43. parser->codec_ids[4] == codec_id)
  44. goto found;
  45. }
  46. return NULL;
  47. found:
  48. s = av_mallocz(sizeof(AVCodecParserContext));
  49. if (!s)
  50. return NULL;
  51. s->parser = parser;
  52. s->priv_data = av_mallocz(parser->priv_data_size);
  53. if (!s->priv_data) {
  54. av_free(s);
  55. return NULL;
  56. }
  57. if (parser->parser_init) {
  58. ret = parser->parser_init(s);
  59. if (ret != 0) {
  60. av_free(s->priv_data);
  61. av_free(s);
  62. return NULL;
  63. }
  64. }
  65. s->fetch_timestamp=1;
  66. return s;
  67. }
  68. /* NOTE: buf_size == 0 is used to signal EOF so that the last frame
  69. can be returned if necessary */
  70. int av_parser_parse(AVCodecParserContext *s,
  71. AVCodecContext *avctx,
  72. uint8_t **poutbuf, int *poutbuf_size,
  73. const uint8_t *buf, int buf_size,
  74. int64_t pts, int64_t dts)
  75. {
  76. int index, i, k;
  77. uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
  78. if (buf_size == 0) {
  79. /* padding is always necessary even if EOF, so we add it here */
  80. memset(dummy_buf, 0, sizeof(dummy_buf));
  81. buf = dummy_buf;
  82. } else {
  83. /* add a new packet descriptor */
  84. k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
  85. s->cur_frame_start_index = k;
  86. s->cur_frame_offset[k] = s->cur_offset;
  87. s->cur_frame_pts[k] = pts;
  88. s->cur_frame_dts[k] = dts;
  89. /* fill first PTS/DTS */
  90. if (s->fetch_timestamp){
  91. s->fetch_timestamp=0;
  92. s->last_pts = pts;
  93. s->last_dts = dts;
  94. s->cur_frame_pts[k] =
  95. s->cur_frame_dts[k] = AV_NOPTS_VALUE;
  96. }
  97. }
  98. /* WARNING: the returned index can be negative */
  99. index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
  100. //av_log(NULL, AV_LOG_DEBUG, "parser: in:%lld, %lld, out:%lld, %lld, in:%d out:%d id:%d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id);
  101. /* update the file pointer */
  102. if (*poutbuf_size) {
  103. /* fill the data for the current frame */
  104. s->frame_offset = s->last_frame_offset;
  105. s->pts = s->last_pts;
  106. s->dts = s->last_dts;
  107. /* offset of the next frame */
  108. s->last_frame_offset = s->cur_offset + index;
  109. /* find the packet in which the new frame starts. It
  110. is tricky because of MPEG video start codes
  111. which can begin in one packet and finish in
  112. another packet. In the worst case, an MPEG
  113. video start code could be in 4 different
  114. packets. */
  115. k = s->cur_frame_start_index;
  116. for(i = 0; i < AV_PARSER_PTS_NB; i++) {
  117. if (s->last_frame_offset >= s->cur_frame_offset[k])
  118. break;
  119. k = (k - 1) & (AV_PARSER_PTS_NB - 1);
  120. }
  121. s->last_pts = s->cur_frame_pts[k];
  122. s->last_dts = s->cur_frame_dts[k];
  123. /* some parsers tell us the packet size even before seeing the first byte of the next packet,
  124. so the next pts/dts is in the next chunk */
  125. if(index == buf_size){
  126. s->fetch_timestamp=1;
  127. }
  128. }
  129. if (index < 0)
  130. index = 0;
  131. s->cur_offset += index;
  132. return index;
  133. }
  134. /**
  135. *
  136. * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed
  137. * @deprecated use AVBitstreamFilter
  138. */
  139. int av_parser_change(AVCodecParserContext *s,
  140. AVCodecContext *avctx,
  141. uint8_t **poutbuf, int *poutbuf_size,
  142. const uint8_t *buf, int buf_size, int keyframe){
  143. if(s && s->parser->split){
  144. if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
  145. int i= s->parser->split(avctx, buf, buf_size);
  146. buf += i;
  147. buf_size -= i;
  148. }
  149. }
  150. /* cast to avoid warning about discarding qualifiers */
  151. *poutbuf= (uint8_t *) buf;
  152. *poutbuf_size= buf_size;
  153. if(avctx->extradata){
  154. if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
  155. /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
  156. /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
  157. int size= buf_size + avctx->extradata_size;
  158. *poutbuf_size= size;
  159. *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  160. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  161. memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  162. return 1;
  163. }
  164. }
  165. return 0;
  166. }
  167. void av_parser_close(AVCodecParserContext *s)
  168. {
  169. if (s->parser->parser_close)
  170. s->parser->parser_close(s);
  171. av_free(s->priv_data);
  172. av_free(s);
  173. }
  174. /*****************************************************/
  175. //#define END_NOT_FOUND (-100)
  176. #define PICTURE_START_CODE 0x00000100
  177. #define SEQ_START_CODE 0x000001b3
  178. #define EXT_START_CODE 0x000001b5
  179. #define SLICE_MIN_START_CODE 0x00000101
  180. #define SLICE_MAX_START_CODE 0x000001af
  181. typedef struct ParseContext1{
  182. ParseContext pc;
  183. /* XXX/FIXME PC1 vs. PC */
  184. /* MPEG2 specific */
  185. int frame_rate;
  186. int progressive_sequence;
  187. int width, height;
  188. /* XXX: suppress that, needed by MPEG4 */
  189. MpegEncContext *enc;
  190. int first_picture;
  191. } ParseContext1;
  192. /**
  193. * combines the (truncated) bitstream to a complete frame
  194. * @returns -1 if no complete frame could be created
  195. */
  196. int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
  197. {
  198. #if 0
  199. if(pc->overread){
  200. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  201. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  202. }
  203. #endif
  204. /* copy overreaded bytes from last frame into buffer */
  205. for(; pc->overread>0; pc->overread--){
  206. pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
  207. }
  208. /* flush remaining if EOF */
  209. if(!*buf_size && next == END_NOT_FOUND){
  210. next= 0;
  211. }
  212. pc->last_index= pc->index;
  213. /* copy into buffer end return */
  214. if(next == END_NOT_FOUND){
  215. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  216. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  217. pc->index += *buf_size;
  218. return -1;
  219. }
  220. *buf_size=
  221. pc->overread_index= pc->index + next;
  222. /* append to buffer */
  223. if(pc->index){
  224. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  225. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  226. pc->index = 0;
  227. *buf= pc->buffer;
  228. }
  229. /* store overread bytes */
  230. for(;next < 0; next++){
  231. pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
  232. pc->overread++;
  233. }
  234. #if 0
  235. if(pc->overread){
  236. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  237. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  238. }
  239. #endif
  240. return 0;
  241. }
  242. /* XXX: merge with libavcodec ? */
  243. #define MPEG1_FRAME_RATE_BASE 1001
  244. static const int frame_rate_tab[16] = {
  245. 0,
  246. 24000,
  247. 24024,
  248. 25025,
  249. 30000,
  250. 30030,
  251. 50050,
  252. 60000,
  253. 60060,
  254. // Xing's 15fps: (9)
  255. 15015,
  256. // libmpeg3's "Unofficial economy rates": (10-13)
  257. 5005,
  258. 10010,
  259. 12012,
  260. 15015,
  261. // random, just to avoid segfault !never encode these
  262. 25025,
  263. 25025,
  264. };
  265. #ifdef CONFIG_MPEGVIDEO_PARSER
  266. //FIXME move into mpeg12.c
  267. static void mpegvideo_extract_headers(AVCodecParserContext *s,
  268. AVCodecContext *avctx,
  269. const uint8_t *buf, int buf_size)
  270. {
  271. ParseContext1 *pc = s->priv_data;
  272. const uint8_t *buf_end;
  273. uint32_t start_code;
  274. int frame_rate_index, ext_type, bytes_left;
  275. int frame_rate_ext_n, frame_rate_ext_d;
  276. int picture_structure, top_field_first, repeat_first_field, progressive_frame;
  277. int horiz_size_ext, vert_size_ext, bit_rate_ext;
  278. //FIXME replace the crap with get_bits()
  279. s->repeat_pict = 0;
  280. buf_end = buf + buf_size;
  281. while (buf < buf_end) {
  282. start_code= -1;
  283. buf= ff_find_start_code(buf, buf_end, &start_code);
  284. bytes_left = buf_end - buf;
  285. switch(start_code) {
  286. case PICTURE_START_CODE:
  287. if (bytes_left >= 2) {
  288. s->pict_type = (buf[1] >> 3) & 7;
  289. }
  290. break;
  291. case SEQ_START_CODE:
  292. if (bytes_left >= 7) {
  293. pc->width = (buf[0] << 4) | (buf[1] >> 4);
  294. pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
  295. avcodec_set_dimensions(avctx, pc->width, pc->height);
  296. frame_rate_index = buf[3] & 0xf;
  297. pc->frame_rate = avctx->time_base.den = frame_rate_tab[frame_rate_index];
  298. avctx->time_base.num = MPEG1_FRAME_RATE_BASE;
  299. avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
  300. avctx->codec_id = CODEC_ID_MPEG1VIDEO;
  301. avctx->sub_id = 1;
  302. }
  303. break;
  304. case EXT_START_CODE:
  305. if (bytes_left >= 1) {
  306. ext_type = (buf[0] >> 4);
  307. switch(ext_type) {
  308. case 0x1: /* sequence extension */
  309. if (bytes_left >= 6) {
  310. horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
  311. vert_size_ext = (buf[2] >> 5) & 3;
  312. bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
  313. frame_rate_ext_n = (buf[5] >> 5) & 3;
  314. frame_rate_ext_d = (buf[5] & 0x1f);
  315. pc->progressive_sequence = buf[1] & (1 << 3);
  316. avctx->has_b_frames= !(buf[5] >> 7);
  317. pc->width |=(horiz_size_ext << 12);
  318. pc->height |=( vert_size_ext << 12);
  319. avctx->bit_rate += (bit_rate_ext << 18) * 400;
  320. avcodec_set_dimensions(avctx, pc->width, pc->height);
  321. avctx->time_base.den = pc->frame_rate * (frame_rate_ext_n + 1);
  322. avctx->time_base.num = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
  323. avctx->codec_id = CODEC_ID_MPEG2VIDEO;
  324. avctx->sub_id = 2; /* forces MPEG2 */
  325. }
  326. break;
  327. case 0x8: /* picture coding extension */
  328. if (bytes_left >= 5) {
  329. picture_structure = buf[2]&3;
  330. top_field_first = buf[3] & (1 << 7);
  331. repeat_first_field = buf[3] & (1 << 1);
  332. progressive_frame = buf[4] & (1 << 7);
  333. /* check if we must repeat the frame */
  334. if (repeat_first_field) {
  335. if (pc->progressive_sequence) {
  336. if (top_field_first)
  337. s->repeat_pict = 4;
  338. else
  339. s->repeat_pict = 2;
  340. } else if (progressive_frame) {
  341. s->repeat_pict = 1;
  342. }
  343. }
  344. /* the packet only represents half a frame
  345. XXX,FIXME maybe find a different solution */
  346. if(picture_structure != 3)
  347. s->repeat_pict = -1;
  348. }
  349. break;
  350. }
  351. }
  352. break;
  353. case -1:
  354. goto the_end;
  355. default:
  356. /* we stop parsing when we encounter a slice. It ensures
  357. that this function takes a negligible amount of time */
  358. if (start_code >= SLICE_MIN_START_CODE &&
  359. start_code <= SLICE_MAX_START_CODE)
  360. goto the_end;
  361. break;
  362. }
  363. }
  364. the_end: ;
  365. }
  366. static int mpegvideo_parse(AVCodecParserContext *s,
  367. AVCodecContext *avctx,
  368. uint8_t **poutbuf, int *poutbuf_size,
  369. const uint8_t *buf, int buf_size)
  370. {
  371. ParseContext1 *pc1 = s->priv_data;
  372. ParseContext *pc= &pc1->pc;
  373. int next;
  374. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  375. next= buf_size;
  376. }else{
  377. next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
  378. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  379. *poutbuf = NULL;
  380. *poutbuf_size = 0;
  381. return buf_size;
  382. }
  383. }
  384. /* we have a full frame : we just parse the first few MPEG headers
  385. to have the full timing information. The time take by this
  386. function should be negligible for uncorrupted streams */
  387. mpegvideo_extract_headers(s, avctx, buf, buf_size);
  388. #if 0
  389. printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
  390. s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
  391. #endif
  392. *poutbuf = (uint8_t *)buf;
  393. *poutbuf_size = buf_size;
  394. return next;
  395. }
  396. static int mpegvideo_split(AVCodecContext *avctx,
  397. const uint8_t *buf, int buf_size)
  398. {
  399. int i;
  400. uint32_t state= -1;
  401. for(i=0; i<buf_size; i++){
  402. state= (state<<8) | buf[i];
  403. if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
  404. return i-3;
  405. }
  406. return 0;
  407. }
  408. #endif /* CONFIG_MPEGVIDEO_PARSER */
  409. void ff_parse_close(AVCodecParserContext *s)
  410. {
  411. ParseContext *pc = s->priv_data;
  412. av_free(pc->buffer);
  413. }
  414. static void parse1_close(AVCodecParserContext *s)
  415. {
  416. ParseContext1 *pc1 = s->priv_data;
  417. av_free(pc1->pc.buffer);
  418. av_free(pc1->enc);
  419. }
  420. /*************************/
  421. #ifdef CONFIG_MPEG4VIDEO_PARSER
  422. /* used by parser */
  423. /* XXX: make it use less memory */
  424. static int av_mpeg4_decode_header(AVCodecParserContext *s1,
  425. AVCodecContext *avctx,
  426. const uint8_t *buf, int buf_size)
  427. {
  428. ParseContext1 *pc = s1->priv_data;
  429. MpegEncContext *s = pc->enc;
  430. GetBitContext gb1, *gb = &gb1;
  431. int ret;
  432. s->avctx = avctx;
  433. s->current_picture_ptr = &s->current_picture;
  434. if (avctx->extradata_size && pc->first_picture){
  435. init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
  436. ret = ff_mpeg4_decode_picture_header(s, gb);
  437. }
  438. init_get_bits(gb, buf, 8 * buf_size);
  439. ret = ff_mpeg4_decode_picture_header(s, gb);
  440. if (s->width) {
  441. avcodec_set_dimensions(avctx, s->width, s->height);
  442. }
  443. s1->pict_type= s->pict_type;
  444. pc->first_picture = 0;
  445. return ret;
  446. }
  447. static int mpeg4video_parse_init(AVCodecParserContext *s)
  448. {
  449. ParseContext1 *pc = s->priv_data;
  450. pc->enc = av_mallocz(sizeof(MpegEncContext));
  451. if (!pc->enc)
  452. return -1;
  453. pc->first_picture = 1;
  454. return 0;
  455. }
  456. static int mpeg4video_parse(AVCodecParserContext *s,
  457. AVCodecContext *avctx,
  458. uint8_t **poutbuf, int *poutbuf_size,
  459. const uint8_t *buf, int buf_size)
  460. {
  461. ParseContext *pc = s->priv_data;
  462. int next;
  463. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  464. next= buf_size;
  465. }else{
  466. next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
  467. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  468. *poutbuf = NULL;
  469. *poutbuf_size = 0;
  470. return buf_size;
  471. }
  472. }
  473. av_mpeg4_decode_header(s, avctx, buf, buf_size);
  474. *poutbuf = (uint8_t *)buf;
  475. *poutbuf_size = buf_size;
  476. return next;
  477. }
  478. #endif
  479. #ifdef CONFIG_CAVSVIDEO_PARSER
  480. static int cavsvideo_parse(AVCodecParserContext *s,
  481. AVCodecContext *avctx,
  482. uint8_t **poutbuf, int *poutbuf_size,
  483. const uint8_t *buf, int buf_size)
  484. {
  485. ParseContext *pc = s->priv_data;
  486. int next;
  487. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  488. next= buf_size;
  489. }else{
  490. next= ff_cavs_find_frame_end(pc, buf, buf_size);
  491. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  492. *poutbuf = NULL;
  493. *poutbuf_size = 0;
  494. return buf_size;
  495. }
  496. }
  497. *poutbuf = (uint8_t *)buf;
  498. *poutbuf_size = buf_size;
  499. return next;
  500. }
  501. #endif /* CONFIG_CAVSVIDEO_PARSER */
  502. static int mpeg4video_split(AVCodecContext *avctx,
  503. const uint8_t *buf, int buf_size)
  504. {
  505. int i;
  506. uint32_t state= -1;
  507. for(i=0; i<buf_size; i++){
  508. state= (state<<8) | buf[i];
  509. if(state == 0x1B3 || state == 0x1B6)
  510. return i-3;
  511. }
  512. return 0;
  513. }
  514. /*************************/
  515. #ifdef CONFIG_MPEGAUDIO_PARSER
  516. typedef struct MpegAudioParseContext {
  517. uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
  518. uint8_t *inbuf_ptr;
  519. int frame_size;
  520. int free_format_frame_size;
  521. int free_format_next_header;
  522. uint32_t header;
  523. int header_count;
  524. } MpegAudioParseContext;
  525. #define MPA_HEADER_SIZE 4
  526. /* header + layer + bitrate + freq + lsf/mpeg25 */
  527. #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
  528. #define SAME_HEADER_MASK \
  529. (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
  530. static int mpegaudio_parse_init(AVCodecParserContext *s1)
  531. {
  532. MpegAudioParseContext *s = s1->priv_data;
  533. s->inbuf_ptr = s->inbuf;
  534. return 0;
  535. }
  536. static int mpegaudio_parse(AVCodecParserContext *s1,
  537. AVCodecContext *avctx,
  538. uint8_t **poutbuf, int *poutbuf_size,
  539. const uint8_t *buf, int buf_size)
  540. {
  541. MpegAudioParseContext *s = s1->priv_data;
  542. int len, ret, sr;
  543. uint32_t header;
  544. const uint8_t *buf_ptr;
  545. *poutbuf = NULL;
  546. *poutbuf_size = 0;
  547. buf_ptr = buf;
  548. while (buf_size > 0) {
  549. len = s->inbuf_ptr - s->inbuf;
  550. if (s->frame_size == 0) {
  551. /* special case for next header for first frame in free
  552. format case (XXX: find a simpler method) */
  553. if (s->free_format_next_header != 0) {
  554. s->inbuf[0] = s->free_format_next_header >> 24;
  555. s->inbuf[1] = s->free_format_next_header >> 16;
  556. s->inbuf[2] = s->free_format_next_header >> 8;
  557. s->inbuf[3] = s->free_format_next_header;
  558. s->inbuf_ptr = s->inbuf + 4;
  559. s->free_format_next_header = 0;
  560. goto got_header;
  561. }
  562. /* no header seen : find one. We need at least MPA_HEADER_SIZE
  563. bytes to parse it */
  564. len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
  565. if (len > 0) {
  566. memcpy(s->inbuf_ptr, buf_ptr, len);
  567. buf_ptr += len;
  568. buf_size -= len;
  569. s->inbuf_ptr += len;
  570. }
  571. if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
  572. got_header:
  573. sr= avctx->sample_rate;
  574. header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  575. (s->inbuf[2] << 8) | s->inbuf[3];
  576. ret = mpa_decode_header(avctx, header);
  577. if (ret < 0) {
  578. s->header_count= -2;
  579. /* no sync found : move by one byte (inefficient, but simple!) */
  580. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  581. s->inbuf_ptr--;
  582. dprintf("skip %x\n", header);
  583. /* reset free format frame size to give a chance
  584. to get a new bitrate */
  585. s->free_format_frame_size = 0;
  586. } else {
  587. if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  588. s->header_count= -3;
  589. s->header= header;
  590. s->header_count++;
  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. if(s->header_count <= 0)
  600. avctx->sample_rate= sr; //FIXME ugly
  601. }
  602. } else
  603. #if 0
  604. if (s->frame_size == -1) {
  605. /* free format : find next sync to compute frame size */
  606. len = MPA_MAX_CODED_FRAME_SIZE - len;
  607. if (len > buf_size)
  608. len = buf_size;
  609. if (len == 0) {
  610. /* frame too long: resync */
  611. s->frame_size = 0;
  612. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  613. s->inbuf_ptr--;
  614. } else {
  615. uint8_t *p, *pend;
  616. uint32_t header1;
  617. int padding;
  618. memcpy(s->inbuf_ptr, buf_ptr, len);
  619. /* check for header */
  620. p = s->inbuf_ptr - 3;
  621. pend = s->inbuf_ptr + len - 4;
  622. while (p <= pend) {
  623. header = (p[0] << 24) | (p[1] << 16) |
  624. (p[2] << 8) | p[3];
  625. header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  626. (s->inbuf[2] << 8) | s->inbuf[3];
  627. /* check with high probability that we have a
  628. valid header */
  629. if ((header & SAME_HEADER_MASK) ==
  630. (header1 & SAME_HEADER_MASK)) {
  631. /* header found: update pointers */
  632. len = (p + 4) - s->inbuf_ptr;
  633. buf_ptr += len;
  634. buf_size -= len;
  635. s->inbuf_ptr = p;
  636. /* compute frame size */
  637. s->free_format_next_header = header;
  638. s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
  639. padding = (header1 >> 9) & 1;
  640. if (s->layer == 1)
  641. s->free_format_frame_size -= padding * 4;
  642. else
  643. s->free_format_frame_size -= padding;
  644. dprintf("free frame size=%d padding=%d\n",
  645. s->free_format_frame_size, padding);
  646. decode_header(s, header1);
  647. goto next_data;
  648. }
  649. p++;
  650. }
  651. /* not found: simply increase pointers */
  652. buf_ptr += len;
  653. s->inbuf_ptr += len;
  654. buf_size -= len;
  655. }
  656. } else
  657. #endif
  658. if (len < s->frame_size) {
  659. if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
  660. s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
  661. len = FFMIN(s->frame_size - 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. if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
  668. && buf_size + buf_ptr - buf >= s->frame_size){
  669. if(s->header_count > 0){
  670. *poutbuf = buf;
  671. *poutbuf_size = s->frame_size;
  672. }
  673. buf_ptr = buf + s->frame_size;
  674. s->inbuf_ptr = s->inbuf;
  675. s->frame_size = 0;
  676. break;
  677. }
  678. // next_data:
  679. if (s->frame_size > 0 &&
  680. (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
  681. if(s->header_count > 0){
  682. *poutbuf = s->inbuf;
  683. *poutbuf_size = s->inbuf_ptr - s->inbuf;
  684. }
  685. s->inbuf_ptr = s->inbuf;
  686. s->frame_size = 0;
  687. break;
  688. }
  689. }
  690. return buf_ptr - buf;
  691. }
  692. #endif /* CONFIG_MPEGAUDIO_PARSER */
  693. #if defined(CONFIG_AC3_PARSER) || defined(CONFIG_AAC_PARSER)
  694. /* also used for ADTS AAC */
  695. typedef struct AC3ParseContext {
  696. uint8_t *inbuf_ptr;
  697. int frame_size;
  698. int header_size;
  699. int (*sync)(const uint8_t *buf, int *channels, int *sample_rate,
  700. int *bit_rate, int *samples);
  701. uint8_t inbuf[8192]; /* input buffer */
  702. } AC3ParseContext;
  703. #define AC3_HEADER_SIZE 7
  704. #define AAC_HEADER_SIZE 7
  705. #ifdef CONFIG_AC3_PARSER
  706. static const int ac3_sample_rates[4] = {
  707. 48000, 44100, 32000, 0
  708. };
  709. static const int ac3_frame_sizes[64][3] = {
  710. { 64, 69, 96 },
  711. { 64, 70, 96 },
  712. { 80, 87, 120 },
  713. { 80, 88, 120 },
  714. { 96, 104, 144 },
  715. { 96, 105, 144 },
  716. { 112, 121, 168 },
  717. { 112, 122, 168 },
  718. { 128, 139, 192 },
  719. { 128, 140, 192 },
  720. { 160, 174, 240 },
  721. { 160, 175, 240 },
  722. { 192, 208, 288 },
  723. { 192, 209, 288 },
  724. { 224, 243, 336 },
  725. { 224, 244, 336 },
  726. { 256, 278, 384 },
  727. { 256, 279, 384 },
  728. { 320, 348, 480 },
  729. { 320, 349, 480 },
  730. { 384, 417, 576 },
  731. { 384, 418, 576 },
  732. { 448, 487, 672 },
  733. { 448, 488, 672 },
  734. { 512, 557, 768 },
  735. { 512, 558, 768 },
  736. { 640, 696, 960 },
  737. { 640, 697, 960 },
  738. { 768, 835, 1152 },
  739. { 768, 836, 1152 },
  740. { 896, 975, 1344 },
  741. { 896, 976, 1344 },
  742. { 1024, 1114, 1536 },
  743. { 1024, 1115, 1536 },
  744. { 1152, 1253, 1728 },
  745. { 1152, 1254, 1728 },
  746. { 1280, 1393, 1920 },
  747. { 1280, 1394, 1920 },
  748. };
  749. static const int ac3_bitrates[64] = {
  750. 32, 32, 40, 40, 48, 48, 56, 56, 64, 64, 80, 80, 96, 96, 112, 112,
  751. 128, 128, 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384,
  752. 384, 448, 448, 512, 512, 576, 576, 640, 640,
  753. };
  754. static const int ac3_channels[8] = {
  755. 2, 1, 2, 3, 3, 4, 4, 5
  756. };
  757. #endif /* CONFIG_AC3_PARSER */
  758. #ifdef CONFIG_AAC_PARSER
  759. static const int aac_sample_rates[16] = {
  760. 96000, 88200, 64000, 48000, 44100, 32000,
  761. 24000, 22050, 16000, 12000, 11025, 8000, 7350
  762. };
  763. static const int aac_channels[8] = {
  764. 0, 1, 2, 3, 4, 5, 6, 8
  765. };
  766. #endif
  767. #ifdef CONFIG_AC3_PARSER
  768. static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
  769. int *bit_rate, int *samples)
  770. {
  771. unsigned int fscod, frmsizecod, acmod, bsid, lfeon;
  772. GetBitContext bits;
  773. init_get_bits(&bits, buf, AC3_HEADER_SIZE * 8);
  774. if(get_bits(&bits, 16) != 0x0b77)
  775. return 0;
  776. skip_bits(&bits, 16); /* crc */
  777. fscod = get_bits(&bits, 2);
  778. frmsizecod = get_bits(&bits, 6);
  779. if(!ac3_sample_rates[fscod])
  780. return 0;
  781. bsid = get_bits(&bits, 5);
  782. if(bsid > 8)
  783. return 0;
  784. skip_bits(&bits, 3); /* bsmod */
  785. acmod = get_bits(&bits, 3);
  786. if(acmod & 1 && acmod != 1)
  787. skip_bits(&bits, 2); /* cmixlev */
  788. if(acmod & 4)
  789. skip_bits(&bits, 2); /* surmixlev */
  790. if(acmod & 2)
  791. skip_bits(&bits, 2); /* dsurmod */
  792. lfeon = get_bits1(&bits);
  793. *sample_rate = ac3_sample_rates[fscod];
  794. *bit_rate = ac3_bitrates[frmsizecod] * 1000;
  795. *channels = ac3_channels[acmod] + lfeon;
  796. *samples = 6 * 256;
  797. return ac3_frame_sizes[frmsizecod][fscod] * 2;
  798. }
  799. #endif /* CONFIG_AC3_PARSER */
  800. #ifdef CONFIG_AAC_PARSER
  801. static int aac_sync(const uint8_t *buf, int *channels, int *sample_rate,
  802. int *bit_rate, int *samples)
  803. {
  804. GetBitContext bits;
  805. int size, rdb, ch, sr;
  806. init_get_bits(&bits, buf, AAC_HEADER_SIZE * 8);
  807. if(get_bits(&bits, 12) != 0xfff)
  808. return 0;
  809. skip_bits1(&bits); /* id */
  810. skip_bits(&bits, 2); /* layer */
  811. skip_bits1(&bits); /* protection_absent */
  812. skip_bits(&bits, 2); /* profile_objecttype */
  813. sr = get_bits(&bits, 4); /* sample_frequency_index */
  814. if(!aac_sample_rates[sr])
  815. return 0;
  816. skip_bits1(&bits); /* private_bit */
  817. ch = get_bits(&bits, 3); /* channel_configuration */
  818. if(!aac_channels[ch])
  819. return 0;
  820. skip_bits1(&bits); /* original/copy */
  821. skip_bits1(&bits); /* home */
  822. /* adts_variable_header */
  823. skip_bits1(&bits); /* copyright_identification_bit */
  824. skip_bits1(&bits); /* copyright_identification_start */
  825. size = get_bits(&bits, 13); /* aac_frame_length */
  826. skip_bits(&bits, 11); /* adts_buffer_fullness */
  827. rdb = get_bits(&bits, 2); /* number_of_raw_data_blocks_in_frame */
  828. *channels = aac_channels[ch];
  829. *sample_rate = aac_sample_rates[sr];
  830. *samples = (rdb + 1) * 1024;
  831. *bit_rate = size * 8 * *sample_rate / *samples;
  832. return size;
  833. }
  834. #endif /* CONFIG_AAC_PARSER */
  835. #ifdef CONFIG_AC3_PARSER
  836. static int ac3_parse_init(AVCodecParserContext *s1)
  837. {
  838. AC3ParseContext *s = s1->priv_data;
  839. s->inbuf_ptr = s->inbuf;
  840. s->header_size = AC3_HEADER_SIZE;
  841. s->sync = ac3_sync;
  842. return 0;
  843. }
  844. #endif
  845. #ifdef CONFIG_AAC_PARSER
  846. static int aac_parse_init(AVCodecParserContext *s1)
  847. {
  848. AC3ParseContext *s = s1->priv_data;
  849. s->inbuf_ptr = s->inbuf;
  850. s->header_size = AAC_HEADER_SIZE;
  851. s->sync = aac_sync;
  852. return 0;
  853. }
  854. #endif
  855. /* also used for ADTS AAC */
  856. static int ac3_parse(AVCodecParserContext *s1,
  857. AVCodecContext *avctx,
  858. uint8_t **poutbuf, int *poutbuf_size,
  859. const uint8_t *buf, int buf_size)
  860. {
  861. AC3ParseContext *s = s1->priv_data;
  862. const uint8_t *buf_ptr;
  863. int len, sample_rate, bit_rate, channels, samples;
  864. *poutbuf = NULL;
  865. *poutbuf_size = 0;
  866. buf_ptr = buf;
  867. while (buf_size > 0) {
  868. len = s->inbuf_ptr - s->inbuf;
  869. if (s->frame_size == 0) {
  870. /* no header seen : find one. We need at least s->header_size
  871. bytes to parse it */
  872. len = FFMIN(s->header_size - len, buf_size);
  873. memcpy(s->inbuf_ptr, buf_ptr, len);
  874. buf_ptr += len;
  875. s->inbuf_ptr += len;
  876. buf_size -= len;
  877. if ((s->inbuf_ptr - s->inbuf) == s->header_size) {
  878. len = s->sync(s->inbuf, &channels, &sample_rate, &bit_rate,
  879. &samples);
  880. if (len == 0) {
  881. /* no sync found : move by one byte (inefficient, but simple!) */
  882. memmove(s->inbuf, s->inbuf + 1, s->header_size - 1);
  883. s->inbuf_ptr--;
  884. } else {
  885. s->frame_size = len;
  886. /* update codec info */
  887. avctx->sample_rate = sample_rate;
  888. /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
  889. if(avctx->codec_id == CODEC_ID_AC3){
  890. if(avctx->channels!=1 && avctx->channels!=2){
  891. avctx->channels = channels;
  892. }
  893. } else {
  894. avctx->channels = channels;
  895. }
  896. avctx->bit_rate = bit_rate;
  897. avctx->frame_size = samples;
  898. }
  899. }
  900. } else {
  901. len = FFMIN(s->frame_size - len, buf_size);
  902. memcpy(s->inbuf_ptr, buf_ptr, len);
  903. buf_ptr += len;
  904. s->inbuf_ptr += len;
  905. buf_size -= len;
  906. if(s->inbuf_ptr - s->inbuf == s->frame_size){
  907. *poutbuf = s->inbuf;
  908. *poutbuf_size = s->frame_size;
  909. s->inbuf_ptr = s->inbuf;
  910. s->frame_size = 0;
  911. break;
  912. }
  913. }
  914. }
  915. return buf_ptr - buf;
  916. }
  917. #endif /* CONFIG_AC3_PARSER || CONFIG_AAC_PARSER */
  918. #ifdef CONFIG_MPEGVIDEO_PARSER
  919. AVCodecParser mpegvideo_parser = {
  920. { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
  921. sizeof(ParseContext1),
  922. NULL,
  923. mpegvideo_parse,
  924. parse1_close,
  925. mpegvideo_split,
  926. };
  927. #endif
  928. #ifdef CONFIG_MPEG4VIDEO_PARSER
  929. AVCodecParser mpeg4video_parser = {
  930. { CODEC_ID_MPEG4 },
  931. sizeof(ParseContext1),
  932. mpeg4video_parse_init,
  933. mpeg4video_parse,
  934. parse1_close,
  935. mpeg4video_split,
  936. };
  937. #endif
  938. #ifdef CONFIG_CAVSVIDEO_PARSER
  939. AVCodecParser cavsvideo_parser = {
  940. { CODEC_ID_CAVS },
  941. sizeof(ParseContext1),
  942. NULL,
  943. cavsvideo_parse,
  944. parse1_close,
  945. mpeg4video_split,
  946. };
  947. #endif
  948. #ifdef CONFIG_MPEGAUDIO_PARSER
  949. AVCodecParser mpegaudio_parser = {
  950. { CODEC_ID_MP2, CODEC_ID_MP3 },
  951. sizeof(MpegAudioParseContext),
  952. mpegaudio_parse_init,
  953. mpegaudio_parse,
  954. NULL,
  955. };
  956. #endif
  957. #ifdef CONFIG_AC3_PARSER
  958. AVCodecParser ac3_parser = {
  959. { CODEC_ID_AC3 },
  960. sizeof(AC3ParseContext),
  961. ac3_parse_init,
  962. ac3_parse,
  963. NULL,
  964. };
  965. #endif
  966. #ifdef CONFIG_AAC_PARSER
  967. AVCodecParser aac_parser = {
  968. { CODEC_ID_AAC },
  969. sizeof(AC3ParseContext),
  970. aac_parse_init,
  971. ac3_parse,
  972. NULL,
  973. };
  974. #endif