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.

904 lines
28KB

  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. #include "parser.h"
  26. AVCodecParser *av_first_parser = NULL;
  27. void av_register_codec_parser(AVCodecParser *parser)
  28. {
  29. parser->next = av_first_parser;
  30. av_first_parser = parser;
  31. }
  32. AVCodecParserContext *av_parser_init(int codec_id)
  33. {
  34. AVCodecParserContext *s;
  35. AVCodecParser *parser;
  36. int ret;
  37. if(codec_id == CODEC_ID_NONE)
  38. return NULL;
  39. for(parser = av_first_parser; parser != NULL; parser = parser->next) {
  40. if (parser->codec_ids[0] == codec_id ||
  41. parser->codec_ids[1] == codec_id ||
  42. parser->codec_ids[2] == codec_id ||
  43. parser->codec_ids[3] == codec_id ||
  44. parser->codec_ids[4] == codec_id)
  45. goto found;
  46. }
  47. return NULL;
  48. found:
  49. s = av_mallocz(sizeof(AVCodecParserContext));
  50. if (!s)
  51. return NULL;
  52. s->parser = parser;
  53. s->priv_data = av_mallocz(parser->priv_data_size);
  54. if (!s->priv_data) {
  55. av_free(s);
  56. return NULL;
  57. }
  58. if (parser->parser_init) {
  59. ret = parser->parser_init(s);
  60. if (ret != 0) {
  61. av_free(s->priv_data);
  62. av_free(s);
  63. return NULL;
  64. }
  65. }
  66. s->fetch_timestamp=1;
  67. return s;
  68. }
  69. /**
  70. *
  71. * @param buf input
  72. * @param buf_size input length, to signal EOF, this should be 0 (so that the last frame can be output)
  73. * @param pts input presentation timestamp
  74. * @param dts input decoding timestamp
  75. * @param poutbuf will contain a pointer to the first byte of the output frame
  76. * @param poutbuf_size will contain the length of the output frame
  77. * @return the number of bytes of the input bitstream used
  78. *
  79. * Example:
  80. * @code
  81. * while(in_len){
  82. * len = av_parser_parse(myparser, AVCodecContext, &data, &size,
  83. * in_data, in_len,
  84. * pts, dts);
  85. * in_data += len;
  86. * in_len -= len;
  87. *
  88. * if(size)
  89. * decode_frame(data, size);
  90. * }
  91. * @endcode
  92. */
  93. int av_parser_parse(AVCodecParserContext *s,
  94. AVCodecContext *avctx,
  95. uint8_t **poutbuf, int *poutbuf_size,
  96. const uint8_t *buf, int buf_size,
  97. int64_t pts, int64_t dts)
  98. {
  99. int index, i, k;
  100. uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
  101. if (buf_size == 0) {
  102. /* padding is always necessary even if EOF, so we add it here */
  103. memset(dummy_buf, 0, sizeof(dummy_buf));
  104. buf = dummy_buf;
  105. } else {
  106. /* add a new packet descriptor */
  107. k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
  108. s->cur_frame_start_index = k;
  109. s->cur_frame_offset[k] = s->cur_offset;
  110. s->cur_frame_pts[k] = pts;
  111. s->cur_frame_dts[k] = dts;
  112. /* fill first PTS/DTS */
  113. if (s->fetch_timestamp){
  114. s->fetch_timestamp=0;
  115. s->last_pts = pts;
  116. s->last_dts = dts;
  117. s->cur_frame_pts[k] =
  118. s->cur_frame_dts[k] = AV_NOPTS_VALUE;
  119. }
  120. }
  121. /* WARNING: the returned index can be negative */
  122. index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
  123. //av_log(NULL, AV_LOG_DEBUG, "parser: in:%"PRId64", %"PRId64", out:%"PRId64", %"PRId64", in:%d out:%d id:%d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id);
  124. /* update the file pointer */
  125. if (*poutbuf_size) {
  126. /* fill the data for the current frame */
  127. s->frame_offset = s->last_frame_offset;
  128. s->pts = s->last_pts;
  129. s->dts = s->last_dts;
  130. /* offset of the next frame */
  131. s->last_frame_offset = s->cur_offset + index;
  132. /* find the packet in which the new frame starts. It
  133. is tricky because of MPEG video start codes
  134. which can begin in one packet and finish in
  135. another packet. In the worst case, an MPEG
  136. video start code could be in 4 different
  137. packets. */
  138. k = s->cur_frame_start_index;
  139. for(i = 0; i < AV_PARSER_PTS_NB; i++) {
  140. if (s->last_frame_offset >= s->cur_frame_offset[k])
  141. break;
  142. k = (k - 1) & (AV_PARSER_PTS_NB - 1);
  143. }
  144. s->last_pts = s->cur_frame_pts[k];
  145. s->last_dts = s->cur_frame_dts[k];
  146. /* some parsers tell us the packet size even before seeing the first byte of the next packet,
  147. so the next pts/dts is in the next chunk */
  148. if(index == buf_size){
  149. s->fetch_timestamp=1;
  150. }
  151. }
  152. if (index < 0)
  153. index = 0;
  154. s->cur_offset += index;
  155. return index;
  156. }
  157. /**
  158. *
  159. * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed
  160. * @deprecated use AVBitstreamFilter
  161. */
  162. int av_parser_change(AVCodecParserContext *s,
  163. AVCodecContext *avctx,
  164. uint8_t **poutbuf, int *poutbuf_size,
  165. const uint8_t *buf, int buf_size, int keyframe){
  166. if(s && s->parser->split){
  167. if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
  168. int i= s->parser->split(avctx, buf, buf_size);
  169. buf += i;
  170. buf_size -= i;
  171. }
  172. }
  173. /* cast to avoid warning about discarding qualifiers */
  174. *poutbuf= (uint8_t *) buf;
  175. *poutbuf_size= buf_size;
  176. if(avctx->extradata){
  177. if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
  178. /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
  179. /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
  180. int size= buf_size + avctx->extradata_size;
  181. *poutbuf_size= size;
  182. *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  183. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  184. memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  185. return 1;
  186. }
  187. }
  188. return 0;
  189. }
  190. void av_parser_close(AVCodecParserContext *s)
  191. {
  192. if (s->parser->parser_close)
  193. s->parser->parser_close(s);
  194. av_free(s->priv_data);
  195. av_free(s);
  196. }
  197. /*****************************************************/
  198. /**
  199. * combines the (truncated) bitstream to a complete frame
  200. * @returns -1 if no complete frame could be created
  201. */
  202. int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
  203. {
  204. #if 0
  205. if(pc->overread){
  206. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  207. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  208. }
  209. #endif
  210. /* copy overreaded bytes from last frame into buffer */
  211. for(; pc->overread>0; pc->overread--){
  212. pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
  213. }
  214. /* flush remaining if EOF */
  215. if(!*buf_size && next == END_NOT_FOUND){
  216. next= 0;
  217. }
  218. pc->last_index= pc->index;
  219. /* copy into buffer end return */
  220. if(next == END_NOT_FOUND){
  221. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  222. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  223. pc->index += *buf_size;
  224. return -1;
  225. }
  226. *buf_size=
  227. pc->overread_index= pc->index + next;
  228. /* append to buffer */
  229. if(pc->index){
  230. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  231. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  232. pc->index = 0;
  233. *buf= pc->buffer;
  234. }
  235. /* store overread bytes */
  236. for(;next < 0; next++){
  237. pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
  238. pc->overread++;
  239. }
  240. #if 0
  241. if(pc->overread){
  242. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  243. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  244. }
  245. #endif
  246. return 0;
  247. }
  248. void ff_parse_close(AVCodecParserContext *s)
  249. {
  250. ParseContext *pc = s->priv_data;
  251. av_free(pc->buffer);
  252. }
  253. void ff_parse1_close(AVCodecParserContext *s)
  254. {
  255. ParseContext1 *pc1 = s->priv_data;
  256. av_free(pc1->pc.buffer);
  257. av_free(pc1->enc);
  258. }
  259. /*************************/
  260. #ifdef CONFIG_MPEG4VIDEO_PARSER
  261. /* used by parser */
  262. /* XXX: make it use less memory */
  263. static int av_mpeg4_decode_header(AVCodecParserContext *s1,
  264. AVCodecContext *avctx,
  265. const uint8_t *buf, int buf_size)
  266. {
  267. ParseContext1 *pc = s1->priv_data;
  268. MpegEncContext *s = pc->enc;
  269. GetBitContext gb1, *gb = &gb1;
  270. int ret;
  271. s->avctx = avctx;
  272. s->current_picture_ptr = &s->current_picture;
  273. if (avctx->extradata_size && pc->first_picture){
  274. init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
  275. ret = ff_mpeg4_decode_picture_header(s, gb);
  276. }
  277. init_get_bits(gb, buf, 8 * buf_size);
  278. ret = ff_mpeg4_decode_picture_header(s, gb);
  279. if (s->width) {
  280. avcodec_set_dimensions(avctx, s->width, s->height);
  281. }
  282. s1->pict_type= s->pict_type;
  283. pc->first_picture = 0;
  284. return ret;
  285. }
  286. static int mpeg4video_parse_init(AVCodecParserContext *s)
  287. {
  288. ParseContext1 *pc = s->priv_data;
  289. pc->enc = av_mallocz(sizeof(MpegEncContext));
  290. if (!pc->enc)
  291. return -1;
  292. pc->first_picture = 1;
  293. return 0;
  294. }
  295. static int mpeg4video_parse(AVCodecParserContext *s,
  296. AVCodecContext *avctx,
  297. uint8_t **poutbuf, int *poutbuf_size,
  298. const uint8_t *buf, int buf_size)
  299. {
  300. ParseContext *pc = s->priv_data;
  301. int next;
  302. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  303. next= buf_size;
  304. }else{
  305. next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
  306. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  307. *poutbuf = NULL;
  308. *poutbuf_size = 0;
  309. return buf_size;
  310. }
  311. }
  312. av_mpeg4_decode_header(s, avctx, buf, buf_size);
  313. *poutbuf = (uint8_t *)buf;
  314. *poutbuf_size = buf_size;
  315. return next;
  316. }
  317. #endif
  318. int ff_mpeg4video_split(AVCodecContext *avctx,
  319. const uint8_t *buf, int buf_size)
  320. {
  321. int i;
  322. uint32_t state= -1;
  323. for(i=0; i<buf_size; i++){
  324. state= (state<<8) | buf[i];
  325. if(state == 0x1B3 || state == 0x1B6)
  326. return i-3;
  327. }
  328. return 0;
  329. }
  330. /*************************/
  331. #ifdef CONFIG_MPEGAUDIO_PARSER
  332. typedef struct MpegAudioParseContext {
  333. uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */
  334. uint8_t *inbuf_ptr;
  335. int frame_size;
  336. int free_format_frame_size;
  337. int free_format_next_header;
  338. uint32_t header;
  339. int header_count;
  340. } MpegAudioParseContext;
  341. #define MPA_HEADER_SIZE 4
  342. /* header + layer + bitrate + freq + lsf/mpeg25 */
  343. #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
  344. #define SAME_HEADER_MASK \
  345. (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
  346. static int mpegaudio_parse_init(AVCodecParserContext *s1)
  347. {
  348. MpegAudioParseContext *s = s1->priv_data;
  349. s->inbuf_ptr = s->inbuf;
  350. return 0;
  351. }
  352. static int mpegaudio_parse(AVCodecParserContext *s1,
  353. AVCodecContext *avctx,
  354. uint8_t **poutbuf, int *poutbuf_size,
  355. const uint8_t *buf, int buf_size)
  356. {
  357. MpegAudioParseContext *s = s1->priv_data;
  358. int len, ret, sr;
  359. uint32_t header;
  360. const uint8_t *buf_ptr;
  361. *poutbuf = NULL;
  362. *poutbuf_size = 0;
  363. buf_ptr = buf;
  364. while (buf_size > 0) {
  365. len = s->inbuf_ptr - s->inbuf;
  366. if (s->frame_size == 0) {
  367. /* special case for next header for first frame in free
  368. format case (XXX: find a simpler method) */
  369. if (s->free_format_next_header != 0) {
  370. s->inbuf[0] = s->free_format_next_header >> 24;
  371. s->inbuf[1] = s->free_format_next_header >> 16;
  372. s->inbuf[2] = s->free_format_next_header >> 8;
  373. s->inbuf[3] = s->free_format_next_header;
  374. s->inbuf_ptr = s->inbuf + 4;
  375. s->free_format_next_header = 0;
  376. goto got_header;
  377. }
  378. /* no header seen : find one. We need at least MPA_HEADER_SIZE
  379. bytes to parse it */
  380. len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
  381. if (len > 0) {
  382. memcpy(s->inbuf_ptr, buf_ptr, len);
  383. buf_ptr += len;
  384. buf_size -= len;
  385. s->inbuf_ptr += len;
  386. }
  387. if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
  388. got_header:
  389. header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  390. (s->inbuf[2] << 8) | s->inbuf[3];
  391. ret = mpa_decode_header(avctx, header, &sr);
  392. if (ret < 0) {
  393. s->header_count= -2;
  394. /* no sync found : move by one byte (inefficient, but simple!) */
  395. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  396. s->inbuf_ptr--;
  397. dprintf("skip %x\n", header);
  398. /* reset free format frame size to give a chance
  399. to get a new bitrate */
  400. s->free_format_frame_size = 0;
  401. } else {
  402. if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
  403. s->header_count= -3;
  404. s->header= header;
  405. s->header_count++;
  406. s->frame_size = ret;
  407. #if 0
  408. /* free format: prepare to compute frame size */
  409. if (decode_header(s, header) == 1) {
  410. s->frame_size = -1;
  411. }
  412. #endif
  413. }
  414. if(s->header_count > 1)
  415. avctx->sample_rate= sr;
  416. }
  417. } else
  418. #if 0
  419. if (s->frame_size == -1) {
  420. /* free format : find next sync to compute frame size */
  421. len = MPA_MAX_CODED_FRAME_SIZE - len;
  422. if (len > buf_size)
  423. len = buf_size;
  424. if (len == 0) {
  425. /* frame too long: resync */
  426. s->frame_size = 0;
  427. memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
  428. s->inbuf_ptr--;
  429. } else {
  430. uint8_t *p, *pend;
  431. uint32_t header1;
  432. int padding;
  433. memcpy(s->inbuf_ptr, buf_ptr, len);
  434. /* check for header */
  435. p = s->inbuf_ptr - 3;
  436. pend = s->inbuf_ptr + len - 4;
  437. while (p <= pend) {
  438. header = (p[0] << 24) | (p[1] << 16) |
  439. (p[2] << 8) | p[3];
  440. header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
  441. (s->inbuf[2] << 8) | s->inbuf[3];
  442. /* check with high probability that we have a
  443. valid header */
  444. if ((header & SAME_HEADER_MASK) ==
  445. (header1 & SAME_HEADER_MASK)) {
  446. /* header found: update pointers */
  447. len = (p + 4) - s->inbuf_ptr;
  448. buf_ptr += len;
  449. buf_size -= len;
  450. s->inbuf_ptr = p;
  451. /* compute frame size */
  452. s->free_format_next_header = header;
  453. s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
  454. padding = (header1 >> 9) & 1;
  455. if (s->layer == 1)
  456. s->free_format_frame_size -= padding * 4;
  457. else
  458. s->free_format_frame_size -= padding;
  459. dprintf("free frame size=%d padding=%d\n",
  460. s->free_format_frame_size, padding);
  461. decode_header(s, header1);
  462. goto next_data;
  463. }
  464. p++;
  465. }
  466. /* not found: simply increase pointers */
  467. buf_ptr += len;
  468. s->inbuf_ptr += len;
  469. buf_size -= len;
  470. }
  471. } else
  472. #endif
  473. if (len < s->frame_size) {
  474. if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
  475. s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
  476. len = FFMIN(s->frame_size - len, buf_size);
  477. memcpy(s->inbuf_ptr, buf_ptr, len);
  478. buf_ptr += len;
  479. s->inbuf_ptr += len;
  480. buf_size -= len;
  481. }
  482. if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
  483. && buf_size + buf_ptr - buf >= s->frame_size){
  484. if(s->header_count > 0){
  485. *poutbuf = buf;
  486. *poutbuf_size = s->frame_size;
  487. }
  488. buf_ptr = buf + s->frame_size;
  489. s->inbuf_ptr = s->inbuf;
  490. s->frame_size = 0;
  491. break;
  492. }
  493. // next_data:
  494. if (s->frame_size > 0 &&
  495. (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
  496. if(s->header_count > 0){
  497. *poutbuf = s->inbuf;
  498. *poutbuf_size = s->inbuf_ptr - s->inbuf;
  499. }
  500. s->inbuf_ptr = s->inbuf;
  501. s->frame_size = 0;
  502. break;
  503. }
  504. }
  505. return buf_ptr - buf;
  506. }
  507. #endif /* CONFIG_MPEGAUDIO_PARSER */
  508. #if defined(CONFIG_AC3_PARSER) || defined(CONFIG_AAC_PARSER)
  509. /* also used for ADTS AAC */
  510. typedef struct AC3ParseContext {
  511. uint8_t *inbuf_ptr;
  512. int frame_size;
  513. int header_size;
  514. int (*sync)(const uint8_t *buf, int *channels, int *sample_rate,
  515. int *bit_rate, int *samples);
  516. uint8_t inbuf[8192]; /* input buffer */
  517. } AC3ParseContext;
  518. #define AC3_HEADER_SIZE 7
  519. #define AAC_HEADER_SIZE 7
  520. #ifdef CONFIG_AC3_PARSER
  521. static const int ac3_sample_rates[4] = {
  522. 48000, 44100, 32000, 0
  523. };
  524. static const int ac3_frame_sizes[64][3] = {
  525. { 64, 69, 96 },
  526. { 64, 70, 96 },
  527. { 80, 87, 120 },
  528. { 80, 88, 120 },
  529. { 96, 104, 144 },
  530. { 96, 105, 144 },
  531. { 112, 121, 168 },
  532. { 112, 122, 168 },
  533. { 128, 139, 192 },
  534. { 128, 140, 192 },
  535. { 160, 174, 240 },
  536. { 160, 175, 240 },
  537. { 192, 208, 288 },
  538. { 192, 209, 288 },
  539. { 224, 243, 336 },
  540. { 224, 244, 336 },
  541. { 256, 278, 384 },
  542. { 256, 279, 384 },
  543. { 320, 348, 480 },
  544. { 320, 349, 480 },
  545. { 384, 417, 576 },
  546. { 384, 418, 576 },
  547. { 448, 487, 672 },
  548. { 448, 488, 672 },
  549. { 512, 557, 768 },
  550. { 512, 558, 768 },
  551. { 640, 696, 960 },
  552. { 640, 697, 960 },
  553. { 768, 835, 1152 },
  554. { 768, 836, 1152 },
  555. { 896, 975, 1344 },
  556. { 896, 976, 1344 },
  557. { 1024, 1114, 1536 },
  558. { 1024, 1115, 1536 },
  559. { 1152, 1253, 1728 },
  560. { 1152, 1254, 1728 },
  561. { 1280, 1393, 1920 },
  562. { 1280, 1394, 1920 },
  563. };
  564. static const int ac3_bitrates[64] = {
  565. 32, 32, 40, 40, 48, 48, 56, 56, 64, 64, 80, 80, 96, 96, 112, 112,
  566. 128, 128, 160, 160, 192, 192, 224, 224, 256, 256, 320, 320, 384,
  567. 384, 448, 448, 512, 512, 576, 576, 640, 640,
  568. };
  569. static const uint8_t ac3_channels[8] = {
  570. 2, 1, 2, 3, 3, 4, 4, 5
  571. };
  572. static const uint8_t eac3_blocks[4] = {
  573. 1, 2, 3, 6
  574. };
  575. #endif /* CONFIG_AC3_PARSER */
  576. #ifdef CONFIG_AAC_PARSER
  577. static const int aac_sample_rates[16] = {
  578. 96000, 88200, 64000, 48000, 44100, 32000,
  579. 24000, 22050, 16000, 12000, 11025, 8000, 7350
  580. };
  581. static const int aac_channels[8] = {
  582. 0, 1, 2, 3, 4, 5, 6, 8
  583. };
  584. #endif
  585. #ifdef CONFIG_AC3_PARSER
  586. static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
  587. int *bit_rate, int *samples)
  588. {
  589. unsigned int fscod, frmsizecod, acmod, bsid, lfeon, halfratecod;
  590. unsigned int strmtyp, substreamid, frmsiz, fscod2, numblkscod;
  591. GetBitContext bits;
  592. init_get_bits(&bits, buf, AC3_HEADER_SIZE * 8);
  593. if(get_bits(&bits, 16) != 0x0b77)
  594. return 0;
  595. bsid = show_bits_long(&bits, 29) & 0x1f;
  596. if(bsid <= 10) { /* Normal AC-3 */
  597. skip_bits(&bits, 16); /* crc */
  598. fscod = get_bits(&bits, 2);
  599. frmsizecod = get_bits(&bits, 6);
  600. if(fscod == 3)
  601. return 0;
  602. skip_bits(&bits, 5); /* bsid */
  603. skip_bits(&bits, 3); /* bsmod */
  604. acmod = get_bits(&bits, 3);
  605. if(acmod & 1 && acmod != 1)
  606. skip_bits(&bits, 2); /* cmixlev */
  607. if(acmod & 4)
  608. skip_bits(&bits, 2); /* surmixlev */
  609. if(acmod & 2)
  610. skip_bits(&bits, 2); /* dsurmod */
  611. lfeon = get_bits1(&bits);
  612. halfratecod = FFMAX(bsid, 8) - 8;
  613. *sample_rate = ac3_sample_rates[fscod] >> halfratecod;
  614. *bit_rate = (ac3_bitrates[frmsizecod] * 1000) >> halfratecod;
  615. *channels = ac3_channels[acmod] + lfeon;
  616. *samples = 6 * 256;
  617. return ac3_frame_sizes[frmsizecod][fscod] * 2;
  618. } else if (bsid > 10 && bsid <= 16) { /* Enhanced AC-3 */
  619. strmtyp = get_bits(&bits, 2);
  620. substreamid = get_bits(&bits, 3);
  621. if (strmtyp != 0 || substreamid != 0)
  622. return 0; /* Currently don't support additional streams */
  623. frmsiz = get_bits(&bits, 11) + 1;
  624. fscod = get_bits(&bits, 2);
  625. if (fscod == 3) {
  626. fscod2 = get_bits(&bits, 2);
  627. numblkscod = 3;
  628. if(fscod2 == 3)
  629. return 0;
  630. *sample_rate = ac3_sample_rates[fscod2] / 2;
  631. } else {
  632. numblkscod = get_bits(&bits, 2);
  633. *sample_rate = ac3_sample_rates[fscod];
  634. }
  635. acmod = get_bits(&bits, 3);
  636. lfeon = get_bits1(&bits);
  637. *samples = eac3_blocks[numblkscod] * 256;
  638. *bit_rate = frmsiz * (*sample_rate) * 16 / (*samples);
  639. *channels = ac3_channels[acmod] + lfeon;
  640. return frmsiz * 2;
  641. }
  642. /* Unsupported bitstream version */
  643. return 0;
  644. }
  645. #endif /* CONFIG_AC3_PARSER */
  646. #ifdef CONFIG_AAC_PARSER
  647. static int aac_sync(const uint8_t *buf, int *channels, int *sample_rate,
  648. int *bit_rate, int *samples)
  649. {
  650. GetBitContext bits;
  651. int size, rdb, ch, sr;
  652. init_get_bits(&bits, buf, AAC_HEADER_SIZE * 8);
  653. if(get_bits(&bits, 12) != 0xfff)
  654. return 0;
  655. skip_bits1(&bits); /* id */
  656. skip_bits(&bits, 2); /* layer */
  657. skip_bits1(&bits); /* protection_absent */
  658. skip_bits(&bits, 2); /* profile_objecttype */
  659. sr = get_bits(&bits, 4); /* sample_frequency_index */
  660. if(!aac_sample_rates[sr])
  661. return 0;
  662. skip_bits1(&bits); /* private_bit */
  663. ch = get_bits(&bits, 3); /* channel_configuration */
  664. if(!aac_channels[ch])
  665. return 0;
  666. skip_bits1(&bits); /* original/copy */
  667. skip_bits1(&bits); /* home */
  668. /* adts_variable_header */
  669. skip_bits1(&bits); /* copyright_identification_bit */
  670. skip_bits1(&bits); /* copyright_identification_start */
  671. size = get_bits(&bits, 13); /* aac_frame_length */
  672. skip_bits(&bits, 11); /* adts_buffer_fullness */
  673. rdb = get_bits(&bits, 2); /* number_of_raw_data_blocks_in_frame */
  674. *channels = aac_channels[ch];
  675. *sample_rate = aac_sample_rates[sr];
  676. *samples = (rdb + 1) * 1024;
  677. *bit_rate = size * 8 * *sample_rate / *samples;
  678. return size;
  679. }
  680. #endif /* CONFIG_AAC_PARSER */
  681. #ifdef CONFIG_AC3_PARSER
  682. static int ac3_parse_init(AVCodecParserContext *s1)
  683. {
  684. AC3ParseContext *s = s1->priv_data;
  685. s->inbuf_ptr = s->inbuf;
  686. s->header_size = AC3_HEADER_SIZE;
  687. s->sync = ac3_sync;
  688. return 0;
  689. }
  690. #endif
  691. #ifdef CONFIG_AAC_PARSER
  692. static int aac_parse_init(AVCodecParserContext *s1)
  693. {
  694. AC3ParseContext *s = s1->priv_data;
  695. s->inbuf_ptr = s->inbuf;
  696. s->header_size = AAC_HEADER_SIZE;
  697. s->sync = aac_sync;
  698. return 0;
  699. }
  700. #endif
  701. /* also used for ADTS AAC */
  702. static int ac3_parse(AVCodecParserContext *s1,
  703. AVCodecContext *avctx,
  704. uint8_t **poutbuf, int *poutbuf_size,
  705. const uint8_t *buf, int buf_size)
  706. {
  707. AC3ParseContext *s = s1->priv_data;
  708. const uint8_t *buf_ptr;
  709. int len, sample_rate, bit_rate, channels, samples;
  710. *poutbuf = NULL;
  711. *poutbuf_size = 0;
  712. buf_ptr = buf;
  713. while (buf_size > 0) {
  714. len = s->inbuf_ptr - s->inbuf;
  715. if (s->frame_size == 0) {
  716. /* no header seen : find one. We need at least s->header_size
  717. bytes to parse it */
  718. len = FFMIN(s->header_size - len, buf_size);
  719. memcpy(s->inbuf_ptr, buf_ptr, len);
  720. buf_ptr += len;
  721. s->inbuf_ptr += len;
  722. buf_size -= len;
  723. if ((s->inbuf_ptr - s->inbuf) == s->header_size) {
  724. len = s->sync(s->inbuf, &channels, &sample_rate, &bit_rate,
  725. &samples);
  726. if (len == 0) {
  727. /* no sync found : move by one byte (inefficient, but simple!) */
  728. memmove(s->inbuf, s->inbuf + 1, s->header_size - 1);
  729. s->inbuf_ptr--;
  730. } else {
  731. s->frame_size = len;
  732. /* update codec info */
  733. avctx->sample_rate = sample_rate;
  734. /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
  735. if(avctx->codec_id == CODEC_ID_AC3){
  736. if(avctx->channels!=1 && avctx->channels!=2){
  737. avctx->channels = channels;
  738. }
  739. } else {
  740. avctx->channels = channels;
  741. }
  742. avctx->bit_rate = bit_rate;
  743. avctx->frame_size = samples;
  744. }
  745. }
  746. } else {
  747. len = FFMIN(s->frame_size - len, buf_size);
  748. memcpy(s->inbuf_ptr, buf_ptr, len);
  749. buf_ptr += len;
  750. s->inbuf_ptr += len;
  751. buf_size -= len;
  752. if(s->inbuf_ptr - s->inbuf == s->frame_size){
  753. *poutbuf = s->inbuf;
  754. *poutbuf_size = s->frame_size;
  755. s->inbuf_ptr = s->inbuf;
  756. s->frame_size = 0;
  757. break;
  758. }
  759. }
  760. }
  761. return buf_ptr - buf;
  762. }
  763. #endif /* CONFIG_AC3_PARSER || CONFIG_AAC_PARSER */
  764. #ifdef CONFIG_MPEG4VIDEO_PARSER
  765. AVCodecParser mpeg4video_parser = {
  766. { CODEC_ID_MPEG4 },
  767. sizeof(ParseContext1),
  768. mpeg4video_parse_init,
  769. mpeg4video_parse,
  770. ff_parse1_close,
  771. ff_mpeg4video_split,
  772. };
  773. #endif
  774. #ifdef CONFIG_MPEGAUDIO_PARSER
  775. AVCodecParser mpegaudio_parser = {
  776. { CODEC_ID_MP2, CODEC_ID_MP3 },
  777. sizeof(MpegAudioParseContext),
  778. mpegaudio_parse_init,
  779. mpegaudio_parse,
  780. NULL,
  781. };
  782. #endif
  783. #ifdef CONFIG_AC3_PARSER
  784. AVCodecParser ac3_parser = {
  785. { CODEC_ID_AC3 },
  786. sizeof(AC3ParseContext),
  787. ac3_parse_init,
  788. ac3_parse,
  789. NULL,
  790. };
  791. #endif
  792. #ifdef CONFIG_AAC_PARSER
  793. AVCodecParser aac_parser = {
  794. { CODEC_ID_AAC },
  795. sizeof(AC3ParseContext),
  796. aac_parse_init,
  797. ac3_parse,
  798. NULL,
  799. };
  800. #endif