parser.c 26 KB

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