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.

1085 lines
34KB

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