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.

1013 lines
32KB

  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. */
  136. int av_parser_change(AVCodecParserContext *s,
  137. AVCodecContext *avctx,
  138. uint8_t **poutbuf, int *poutbuf_size,
  139. const uint8_t *buf, int buf_size, int keyframe){
  140. if(s && s->parser->split){
  141. if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
  142. int i= s->parser->split(avctx, buf, buf_size);
  143. buf += i;
  144. buf_size -= i;
  145. }
  146. }
  147. /* cast to avoid warning about discarding qualifiers */
  148. *poutbuf= (uint8_t *) buf;
  149. *poutbuf_size= buf_size;
  150. if(avctx->extradata){
  151. if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
  152. /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
  153. /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
  154. int size= buf_size + avctx->extradata_size;
  155. *poutbuf_size= size;
  156. *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  157. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  158. memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  159. return 1;
  160. }
  161. }
  162. return 0;
  163. }
  164. void av_parser_close(AVCodecParserContext *s)
  165. {
  166. if (s->parser->parser_close)
  167. s->parser->parser_close(s);
  168. av_free(s->priv_data);
  169. av_free(s);
  170. }
  171. /*****************************************************/
  172. //#define END_NOT_FOUND (-100)
  173. #define PICTURE_START_CODE 0x00000100
  174. #define SEQ_START_CODE 0x000001b3
  175. #define EXT_START_CODE 0x000001b5
  176. #define SLICE_MIN_START_CODE 0x00000101
  177. #define SLICE_MAX_START_CODE 0x000001af
  178. typedef struct ParseContext1{
  179. ParseContext pc;
  180. /* XXX/FIXME PC1 vs. PC */
  181. /* MPEG2 specific */
  182. int frame_rate;
  183. int progressive_sequence;
  184. int width, height;
  185. /* XXX: suppress that, needed by MPEG4 */
  186. MpegEncContext *enc;
  187. int first_picture;
  188. } ParseContext1;
  189. /**
  190. * combines the (truncated) bitstream to a complete frame
  191. * @returns -1 if no complete frame could be created
  192. */
  193. int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
  194. {
  195. #if 0
  196. if(pc->overread){
  197. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  198. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  199. }
  200. #endif
  201. /* copy overreaded bytes from last frame into buffer */
  202. for(; pc->overread>0; pc->overread--){
  203. pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
  204. }
  205. /* flush remaining if EOF */
  206. if(!*buf_size && next == END_NOT_FOUND){
  207. next= 0;
  208. }
  209. pc->last_index= pc->index;
  210. /* copy into buffer end return */
  211. if(next == END_NOT_FOUND){
  212. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  213. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  214. pc->index += *buf_size;
  215. return -1;
  216. }
  217. *buf_size=
  218. pc->overread_index= pc->index + next;
  219. /* append to buffer */
  220. if(pc->index){
  221. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  222. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  223. pc->index = 0;
  224. *buf= pc->buffer;
  225. }
  226. /* store overread bytes */
  227. for(;next < 0; next++){
  228. pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
  229. pc->overread++;
  230. }
  231. #if 0
  232. if(pc->overread){
  233. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  234. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  235. }
  236. #endif
  237. return 0;
  238. }
  239. /* XXX: merge with libavcodec ? */
  240. #define MPEG1_FRAME_RATE_BASE 1001
  241. static const int frame_rate_tab[16] = {
  242. 0,
  243. 24000,
  244. 24024,
  245. 25025,
  246. 30000,
  247. 30030,
  248. 50050,
  249. 60000,
  250. 60060,
  251. // Xing's 15fps: (9)
  252. 15015,
  253. // libmpeg3's "Unofficial economy rates": (10-13)
  254. 5005,
  255. 10010,
  256. 12012,
  257. 15015,
  258. // random, just to avoid segfault !never encode these
  259. 25025,
  260. 25025,
  261. };
  262. //FIXME move into mpeg12.c
  263. static void mpegvideo_extract_headers(AVCodecParserContext *s,
  264. AVCodecContext *avctx,
  265. const uint8_t *buf, int buf_size)
  266. {
  267. ParseContext1 *pc = s->priv_data;
  268. const uint8_t *buf_end;
  269. int32_t start_code;
  270. int frame_rate_index, ext_type, bytes_left;
  271. int frame_rate_ext_n, frame_rate_ext_d;
  272. int picture_structure, top_field_first, repeat_first_field, progressive_frame;
  273. int horiz_size_ext, vert_size_ext, bit_rate_ext;
  274. //FIXME replace the crap with get_bits()
  275. s->repeat_pict = 0;
  276. buf_end = buf + buf_size;
  277. while (buf < buf_end) {
  278. start_code= -1;
  279. buf= ff_find_start_code(buf, buf_end, &start_code);
  280. bytes_left = buf_end - buf;
  281. switch(start_code) {
  282. case PICTURE_START_CODE:
  283. if (bytes_left >= 2) {
  284. s->pict_type = (buf[1] >> 3) & 7;
  285. }
  286. break;
  287. case SEQ_START_CODE:
  288. if (bytes_left >= 7) {
  289. pc->width = (buf[0] << 4) | (buf[1] >> 4);
  290. pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
  291. avcodec_set_dimensions(avctx, pc->width, pc->height);
  292. frame_rate_index = buf[3] & 0xf;
  293. pc->frame_rate = avctx->time_base.den = frame_rate_tab[frame_rate_index];
  294. avctx->time_base.num = MPEG1_FRAME_RATE_BASE;
  295. avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
  296. avctx->codec_id = CODEC_ID_MPEG1VIDEO;
  297. avctx->sub_id = 1;
  298. }
  299. break;
  300. case EXT_START_CODE:
  301. if (bytes_left >= 1) {
  302. ext_type = (buf[0] >> 4);
  303. switch(ext_type) {
  304. case 0x1: /* sequence extension */
  305. if (bytes_left >= 6) {
  306. horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
  307. vert_size_ext = (buf[2] >> 5) & 3;
  308. bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
  309. frame_rate_ext_n = (buf[5] >> 5) & 3;
  310. frame_rate_ext_d = (buf[5] & 0x1f);
  311. pc->progressive_sequence = buf[1] & (1 << 3);
  312. avctx->has_b_frames= !(buf[5] >> 7);
  313. pc->width |=(horiz_size_ext << 12);
  314. pc->height |=( vert_size_ext << 12);
  315. avctx->bit_rate += (bit_rate_ext << 18) * 400;
  316. avcodec_set_dimensions(avctx, pc->width, pc->height);
  317. avctx->time_base.den = pc->frame_rate * (frame_rate_ext_n + 1);
  318. avctx->time_base.num = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
  319. avctx->codec_id = CODEC_ID_MPEG2VIDEO;
  320. avctx->sub_id = 2; /* forces MPEG2 */
  321. }
  322. break;
  323. case 0x8: /* picture coding extension */
  324. if (bytes_left >= 5) {
  325. picture_structure = buf[2]&3;
  326. top_field_first = buf[3] & (1 << 7);
  327. repeat_first_field = buf[3] & (1 << 1);
  328. progressive_frame = buf[4] & (1 << 7);
  329. /* check if we must repeat the frame */
  330. if (repeat_first_field) {
  331. if (pc->progressive_sequence) {
  332. if (top_field_first)
  333. s->repeat_pict = 4;
  334. else
  335. s->repeat_pict = 2;
  336. } else if (progressive_frame) {
  337. s->repeat_pict = 1;
  338. }
  339. }
  340. /* the packet only represents half a frame
  341. XXX,FIXME maybe find a different solution */
  342. if(picture_structure != 3)
  343. s->repeat_pict = -1;
  344. }
  345. break;
  346. }
  347. }
  348. break;
  349. case -1:
  350. goto the_end;
  351. default:
  352. /* we stop parsing when we encounter a slice. It ensures
  353. that this function takes a negligible amount of time */
  354. if (start_code >= SLICE_MIN_START_CODE &&
  355. start_code <= SLICE_MAX_START_CODE)
  356. goto the_end;
  357. break;
  358. }
  359. }
  360. the_end: ;
  361. }
  362. static int mpegvideo_parse(AVCodecParserContext *s,
  363. AVCodecContext *avctx,
  364. uint8_t **poutbuf, int *poutbuf_size,
  365. const uint8_t *buf, int buf_size)
  366. {
  367. ParseContext1 *pc1 = s->priv_data;
  368. ParseContext *pc= &pc1->pc;
  369. int next;
  370. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  371. next= buf_size;
  372. }else{
  373. next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
  374. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  375. *poutbuf = NULL;
  376. *poutbuf_size = 0;
  377. return buf_size;
  378. }
  379. }
  380. /* we have a full frame : we just parse the first few MPEG headers
  381. to have the full timing information. The time take by this
  382. function should be negligible for uncorrupted streams */
  383. mpegvideo_extract_headers(s, avctx, buf, buf_size);
  384. #if 0
  385. printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
  386. s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
  387. #endif
  388. *poutbuf = (uint8_t *)buf;
  389. *poutbuf_size = buf_size;
  390. return next;
  391. }
  392. static int mpegvideo_split(AVCodecContext *avctx,
  393. const uint8_t *buf, int buf_size)
  394. {
  395. int i;
  396. uint32_t state= -1;
  397. for(i=0; i<buf_size; i++){
  398. state= (state<<8) | buf[i];
  399. if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
  400. return i-3;
  401. }
  402. return 0;
  403. }
  404. void ff_parse_close(AVCodecParserContext *s)
  405. {
  406. ParseContext *pc = s->priv_data;
  407. av_free(pc->buffer);
  408. }
  409. static void parse1_close(AVCodecParserContext *s)
  410. {
  411. ParseContext1 *pc1 = s->priv_data;
  412. av_free(pc1->pc.buffer);
  413. av_free(pc1->enc);
  414. }
  415. /*************************/
  416. /* used by parser */
  417. /* XXX: make it use less memory */
  418. static int av_mpeg4_decode_header(AVCodecParserContext *s1,
  419. AVCodecContext *avctx,
  420. const uint8_t *buf, int buf_size)
  421. {
  422. ParseContext1 *pc = s1->priv_data;
  423. MpegEncContext *s = pc->enc;
  424. GetBitContext gb1, *gb = &gb1;
  425. int ret;
  426. s->avctx = avctx;
  427. s->current_picture_ptr = &s->current_picture;
  428. if (avctx->extradata_size && pc->first_picture){
  429. init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
  430. ret = ff_mpeg4_decode_picture_header(s, gb);
  431. }
  432. init_get_bits(gb, buf, 8 * buf_size);
  433. ret = ff_mpeg4_decode_picture_header(s, gb);
  434. if (s->width) {
  435. avcodec_set_dimensions(avctx, s->width, s->height);
  436. }
  437. s1->pict_type= s->pict_type;
  438. pc->first_picture = 0;
  439. return ret;
  440. }
  441. static int mpeg4video_parse_init(AVCodecParserContext *s)
  442. {
  443. ParseContext1 *pc = s->priv_data;
  444. pc->enc = av_mallocz(sizeof(MpegEncContext));
  445. if (!pc->enc)
  446. return -1;
  447. pc->first_picture = 1;
  448. return 0;
  449. }
  450. static int mpeg4video_parse(AVCodecParserContext *s,
  451. AVCodecContext *avctx,
  452. uint8_t **poutbuf, int *poutbuf_size,
  453. const uint8_t *buf, int buf_size)
  454. {
  455. ParseContext *pc = s->priv_data;
  456. int next;
  457. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  458. next= buf_size;
  459. }else{
  460. next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
  461. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  462. *poutbuf = NULL;
  463. *poutbuf_size = 0;
  464. return buf_size;
  465. }
  466. }
  467. av_mpeg4_decode_header(s, avctx, buf, buf_size);
  468. *poutbuf = (uint8_t *)buf;
  469. *poutbuf_size = buf_size;
  470. return next;
  471. }
  472. static int mpeg4video_split(AVCodecContext *avctx,
  473. const uint8_t *buf, int buf_size)
  474. {
  475. int i;
  476. uint32_t state= -1;
  477. for(i=0; i<buf_size; i++){
  478. state= (state<<8) | buf[i];
  479. if(state == 0x1B3 || state == 0x1B6)
  480. return i-3;
  481. }
  482. return 0;
  483. }
  484. /*************************/
  485. typedef struct MpegAudioParseContext {
  486. uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
  487. uint8_t *inbuf_ptr;
  488. int frame_size;
  489. int free_format_frame_size;
  490. int free_format_next_header;
  491. uint32_t header;
  492. int header_count;
  493. } MpegAudioParseContext;
  494. #define MPA_HEADER_SIZE 4
  495. /* header + layer + bitrate + freq + lsf/mpeg25 */
  496. #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
  497. #define SAME_HEADER_MASK \
  498. (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
  499. static int mpegaudio_parse_init(AVCodecParserContext *s1)
  500. {
  501. MpegAudioParseContext *s = s1->priv_data;
  502. s->inbuf_ptr = s->inbuf;
  503. return 0;
  504. }
  505. static int mpegaudio_parse(AVCodecParserContext *s1,
  506. AVCodecContext *avctx,
  507. uint8_t **poutbuf, int *poutbuf_size,
  508. const uint8_t *buf, int buf_size)
  509. {
  510. MpegAudioParseContext *s = s1->priv_data;
  511. int len, ret, sr;
  512. uint32_t header;
  513. const uint8_t *buf_ptr;
  514. *poutbuf = NULL;
  515. *poutbuf_size = 0;
  516. buf_ptr = buf;
  517. while (buf_size > 0) {
  518. len = s->inbuf_ptr - s->inbuf;
  519. if (s->frame_size == 0) {
  520. /* special case for next header for first frame in free
  521. format case (XXX: find a simpler method) */
  522. if (s->free_format_next_header != 0) {
  523. s->inbuf[0] = s->free_format_next_header >> 24;
  524. s->inbuf[1] = s->free_format_next_header >> 16;
  525. s->inbuf[2] = s->free_format_next_header >> 8;
  526. s->inbuf[3] = s->free_format_next_header;
  527. s->inbuf_ptr = s->inbuf + 4;
  528. s->free_format_next_header = 0;
  529. goto got_header;
  530. }
  531. /* no header seen : find one. We need at least MPA_HEADER_SIZE
  532. bytes to parse it */
  533. len = MPA_HEADER_SIZE - len;
  534. if (len > buf_size)
  535. len = buf_size;
  536. if (len > 0) {
  537. memcpy(s->inbuf_ptr, buf_ptr, len);
  538. buf_ptr += len;
  539. buf_size -= len;
  540. s->inbuf_ptr += len;
  541. }
  542. if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
  543. got_header:
  544. sr= avctx->sample_rate;
  545. header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  546. (s->inbuf[2] << 8) | s->inbuf[3];
  547. ret = mpa_decode_header(avctx, header);
  548. if (ret < 0) {
  549. s->header_count= -2;
  550. /* no sync found : move by one byte (inefficient, but simple!) */
  551. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  552. s->inbuf_ptr--;
  553. dprintf("skip %x\n", header);
  554. /* reset free format frame size to give a chance
  555. to get a new bitrate */
  556. s->free_format_frame_size = 0;
  557. } else {
  558. if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  559. s->header_count= -3;
  560. s->header= header;
  561. s->header_count++;
  562. s->frame_size = ret;
  563. #if 0
  564. /* free format: prepare to compute frame size */
  565. if (decode_header(s, header) == 1) {
  566. s->frame_size = -1;
  567. }
  568. #endif
  569. }
  570. if(s->header_count <= 0)
  571. avctx->sample_rate= sr; //FIXME ugly
  572. }
  573. } else
  574. #if 0
  575. if (s->frame_size == -1) {
  576. /* free format : find next sync to compute frame size */
  577. len = MPA_MAX_CODED_FRAME_SIZE - len;
  578. if (len > buf_size)
  579. len = buf_size;
  580. if (len == 0) {
  581. /* frame too long: resync */
  582. s->frame_size = 0;
  583. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  584. s->inbuf_ptr--;
  585. } else {
  586. uint8_t *p, *pend;
  587. uint32_t header1;
  588. int padding;
  589. memcpy(s->inbuf_ptr, buf_ptr, len);
  590. /* check for header */
  591. p = s->inbuf_ptr - 3;
  592. pend = s->inbuf_ptr + len - 4;
  593. while (p <= pend) {
  594. header = (p[0] << 24) | (p[1] << 16) |
  595. (p[2] << 8) | p[3];
  596. header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  597. (s->inbuf[2] << 8) | s->inbuf[3];
  598. /* check with high probability that we have a
  599. valid header */
  600. if ((header & SAME_HEADER_MASK) ==
  601. (header1 & SAME_HEADER_MASK)) {
  602. /* header found: update pointers */
  603. len = (p + 4) - s->inbuf_ptr;
  604. buf_ptr += len;
  605. buf_size -= len;
  606. s->inbuf_ptr = p;
  607. /* compute frame size */
  608. s->free_format_next_header = header;
  609. s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
  610. padding = (header1 >> 9) & 1;
  611. if (s->layer == 1)
  612. s->free_format_frame_size -= padding * 4;
  613. else
  614. s->free_format_frame_size -= padding;
  615. dprintf("free frame size=%d padding=%d\n",
  616. s->free_format_frame_size, padding);
  617. decode_header(s, header1);
  618. goto next_data;
  619. }
  620. p++;
  621. }
  622. /* not found: simply increase pointers */
  623. buf_ptr += len;
  624. s->inbuf_ptr += len;
  625. buf_size -= len;
  626. }
  627. } else
  628. #endif
  629. if (len < s->frame_size) {
  630. if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
  631. s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
  632. len = s->frame_size - len;
  633. if (len > buf_size)
  634. len = buf_size;
  635. memcpy(s->inbuf_ptr, buf_ptr, len);
  636. buf_ptr += len;
  637. s->inbuf_ptr += len;
  638. buf_size -= len;
  639. }
  640. // next_data:
  641. if (s->frame_size > 0 &&
  642. (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
  643. if(s->header_count > 0){
  644. *poutbuf = s->inbuf;
  645. *poutbuf_size = s->inbuf_ptr - s->inbuf;
  646. }
  647. s->inbuf_ptr = s->inbuf;
  648. s->frame_size = 0;
  649. break;
  650. }
  651. }
  652. return buf_ptr - buf;
  653. }
  654. /* also used for ADTS AAC */
  655. typedef struct AC3ParseContext {
  656. uint8_t inbuf[4096]; /* input buffer */
  657. uint8_t *inbuf_ptr;
  658. int frame_size;
  659. int header_size;
  660. int (*sync)(const uint8_t *buf, int *channels, int *sample_rate,
  661. int *bit_rate, int *samples);
  662. } AC3ParseContext;
  663. #define AC3_HEADER_SIZE 7
  664. #define AAC_HEADER_SIZE 7
  665. static const int ac3_sample_rates[4] = {
  666. 48000, 44100, 32000, 0
  667. };
  668. static const int ac3_frame_sizes[64][3] = {
  669. { 64, 69, 96 },
  670. { 64, 70, 96 },
  671. { 80, 87, 120 },
  672. { 80, 88, 120 },
  673. { 96, 104, 144 },
  674. { 96, 105, 144 },
  675. { 112, 121, 168 },
  676. { 112, 122, 168 },
  677. { 128, 139, 192 },
  678. { 128, 140, 192 },
  679. { 160, 174, 240 },
  680. { 160, 175, 240 },
  681. { 192, 208, 288 },
  682. { 192, 209, 288 },
  683. { 224, 243, 336 },
  684. { 224, 244, 336 },
  685. { 256, 278, 384 },
  686. { 256, 279, 384 },
  687. { 320, 348, 480 },
  688. { 320, 349, 480 },
  689. { 384, 417, 576 },
  690. { 384, 418, 576 },
  691. { 448, 487, 672 },
  692. { 448, 488, 672 },
  693. { 512, 557, 768 },
  694. { 512, 558, 768 },
  695. { 640, 696, 960 },
  696. { 640, 697, 960 },
  697. { 768, 835, 1152 },
  698. { 768, 836, 1152 },
  699. { 896, 975, 1344 },
  700. { 896, 976, 1344 },
  701. { 1024, 1114, 1536 },
  702. { 1024, 1115, 1536 },
  703. { 1152, 1253, 1728 },
  704. { 1152, 1254, 1728 },
  705. { 1280, 1393, 1920 },
  706. { 1280, 1394, 1920 },
  707. };
  708. static const int ac3_bitrates[64] = {
  709. 32, 32, 40, 40, 48, 48, 56, 56, 64, 64, 80, 80, 96, 96, 112, 112,
  710. 128, 128, 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384,
  711. 384, 448, 448, 512, 512, 576, 576, 640, 640,
  712. };
  713. static const int ac3_channels[8] = {
  714. 2, 1, 2, 3, 3, 4, 4, 5
  715. };
  716. static int aac_sample_rates[16] = {
  717. 96000, 88200, 64000, 48000, 44100, 32000,
  718. 24000, 22050, 16000, 12000, 11025, 8000, 7350
  719. };
  720. static int aac_channels[8] = {
  721. 0, 1, 2, 3, 4, 5, 6, 8
  722. };
  723. static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
  724. int *bit_rate, int *samples)
  725. {
  726. unsigned int fscod, frmsizecod, acmod, bsid, lfeon;
  727. GetBitContext bits;
  728. init_get_bits(&bits, buf, AC3_HEADER_SIZE * 8);
  729. if(get_bits(&bits, 16) != 0x0b77)
  730. return 0;
  731. skip_bits(&bits, 16); /* crc */
  732. fscod = get_bits(&bits, 2);
  733. frmsizecod = get_bits(&bits, 6);
  734. if(!ac3_sample_rates[fscod])
  735. return 0;
  736. bsid = get_bits(&bits, 5);
  737. if(bsid > 8)
  738. return 0;
  739. skip_bits(&bits, 3); /* bsmod */
  740. acmod = get_bits(&bits, 3);
  741. if(acmod & 1 && acmod != 1)
  742. skip_bits(&bits, 2); /* cmixlev */
  743. if(acmod & 4)
  744. skip_bits(&bits, 2); /* surmixlev */
  745. if(acmod & 2)
  746. skip_bits(&bits, 2); /* dsurmod */
  747. lfeon = get_bits1(&bits);
  748. *sample_rate = ac3_sample_rates[fscod];
  749. *bit_rate = ac3_bitrates[frmsizecod] * 1000;
  750. *channels = ac3_channels[acmod] + lfeon;
  751. *samples = 6 * 256;
  752. return ac3_frame_sizes[frmsizecod][fscod] * 2;
  753. }
  754. static int aac_sync(const uint8_t *buf, int *channels, int *sample_rate,
  755. int *bit_rate, int *samples)
  756. {
  757. GetBitContext bits;
  758. int size, rdb, ch, sr;
  759. init_get_bits(&bits, buf, AAC_HEADER_SIZE * 8);
  760. if(get_bits(&bits, 12) != 0xfff)
  761. return 0;
  762. skip_bits1(&bits); /* id */
  763. skip_bits(&bits, 2); /* layer */
  764. skip_bits1(&bits); /* protection_absent */
  765. skip_bits(&bits, 2); /* profile_objecttype */
  766. sr = get_bits(&bits, 4); /* sample_frequency_index */
  767. if(!aac_sample_rates[sr])
  768. return 0;
  769. skip_bits1(&bits); /* private_bit */
  770. ch = get_bits(&bits, 3); /* channel_configuration */
  771. if(!aac_channels[ch])
  772. return 0;
  773. skip_bits1(&bits); /* original/copy */
  774. skip_bits1(&bits); /* home */
  775. /* adts_variable_header */
  776. skip_bits1(&bits); /* copyright_identification_bit */
  777. skip_bits1(&bits); /* copyright_identification_start */
  778. size = get_bits(&bits, 13); /* aac_frame_length */
  779. skip_bits(&bits, 11); /* adts_buffer_fullness */
  780. rdb = get_bits(&bits, 2); /* number_of_raw_data_blocks_in_frame */
  781. *channels = aac_channels[ch];
  782. *sample_rate = aac_sample_rates[sr];
  783. *samples = (rdb + 1) * 1024;
  784. *bit_rate = size * 8 * *sample_rate / *samples;
  785. return size;
  786. }
  787. static int ac3_parse_init(AVCodecParserContext *s1)
  788. {
  789. AC3ParseContext *s = s1->priv_data;
  790. s->inbuf_ptr = s->inbuf;
  791. s->header_size = AC3_HEADER_SIZE;
  792. s->sync = ac3_sync;
  793. return 0;
  794. }
  795. static int aac_parse_init(AVCodecParserContext *s1)
  796. {
  797. AC3ParseContext *s = s1->priv_data;
  798. s->inbuf_ptr = s->inbuf;
  799. s->header_size = AAC_HEADER_SIZE;
  800. s->sync = aac_sync;
  801. return 0;
  802. }
  803. /* also used for ADTS AAC */
  804. static int ac3_parse(AVCodecParserContext *s1,
  805. AVCodecContext *avctx,
  806. uint8_t **poutbuf, int *poutbuf_size,
  807. const uint8_t *buf, int buf_size)
  808. {
  809. AC3ParseContext *s = s1->priv_data;
  810. const uint8_t *buf_ptr;
  811. int len, sample_rate, bit_rate, channels, samples;
  812. *poutbuf = NULL;
  813. *poutbuf_size = 0;
  814. buf_ptr = buf;
  815. while (buf_size > 0) {
  816. len = s->inbuf_ptr - s->inbuf;
  817. if (s->frame_size == 0) {
  818. /* no header seen : find one. We need at least s->header_size
  819. bytes to parse it */
  820. len = FFMIN(s->header_size - len, buf_size);
  821. memcpy(s->inbuf_ptr, buf_ptr, len);
  822. buf_ptr += len;
  823. s->inbuf_ptr += len;
  824. buf_size -= len;
  825. if ((s->inbuf_ptr - s->inbuf) == s->header_size) {
  826. len = s->sync(s->inbuf, &channels, &sample_rate, &bit_rate,
  827. &samples);
  828. if (len == 0) {
  829. /* no sync found : move by one byte (inefficient, but simple!) */
  830. memmove(s->inbuf, s->inbuf + 1, s->header_size - 1);
  831. s->inbuf_ptr--;
  832. } else {
  833. s->frame_size = len;
  834. /* update codec info */
  835. avctx->sample_rate = sample_rate;
  836. /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
  837. if(avctx->codec_id == CODEC_ID_AC3){
  838. if(avctx->channels!=1 && avctx->channels!=2){
  839. avctx->channels = channels;
  840. }
  841. } else {
  842. avctx->channels = channels;
  843. }
  844. avctx->bit_rate = bit_rate;
  845. avctx->frame_size = samples;
  846. }
  847. }
  848. } else {
  849. len = FFMIN(s->frame_size - len, buf_size);
  850. memcpy(s->inbuf_ptr, buf_ptr, len);
  851. buf_ptr += len;
  852. s->inbuf_ptr += len;
  853. buf_size -= len;
  854. if(s->inbuf_ptr - s->inbuf == s->frame_size){
  855. *poutbuf = s->inbuf;
  856. *poutbuf_size = s->frame_size;
  857. s->inbuf_ptr = s->inbuf;
  858. s->frame_size = 0;
  859. break;
  860. }
  861. }
  862. }
  863. return buf_ptr - buf;
  864. }
  865. AVCodecParser mpegvideo_parser = {
  866. { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
  867. sizeof(ParseContext1),
  868. NULL,
  869. mpegvideo_parse,
  870. parse1_close,
  871. mpegvideo_split,
  872. };
  873. AVCodecParser mpeg4video_parser = {
  874. { CODEC_ID_MPEG4 },
  875. sizeof(ParseContext1),
  876. mpeg4video_parse_init,
  877. mpeg4video_parse,
  878. parse1_close,
  879. mpeg4video_split,
  880. };
  881. AVCodecParser mpegaudio_parser = {
  882. { CODEC_ID_MP2, CODEC_ID_MP3 },
  883. sizeof(MpegAudioParseContext),
  884. mpegaudio_parse_init,
  885. mpegaudio_parse,
  886. NULL,
  887. };
  888. AVCodecParser ac3_parser = {
  889. { CODEC_ID_AC3 },
  890. sizeof(AC3ParseContext),
  891. ac3_parse_init,
  892. ac3_parse,
  893. NULL,
  894. };
  895. AVCodecParser aac_parser = {
  896. { CODEC_ID_AAC },
  897. sizeof(AC3ParseContext),
  898. aac_parse_init,
  899. ac3_parse,
  900. NULL,
  901. };