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.

612 lines
18KB

  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. s->pict_type = FF_I_TYPE;
  68. return s;
  69. }
  70. /**
  71. *
  72. * @param buf input
  73. * @param buf_size input length, to signal EOF, this should be 0 (so that the last frame can be output)
  74. * @param pts input presentation timestamp
  75. * @param dts input decoding timestamp
  76. * @param poutbuf will contain a pointer to the first byte of the output frame
  77. * @param poutbuf_size will contain the length of the output frame
  78. * @return the number of bytes of the input bitstream used
  79. *
  80. * Example:
  81. * @code
  82. * while(in_len){
  83. * len = av_parser_parse(myparser, AVCodecContext, &data, &size,
  84. * in_data, in_len,
  85. * pts, dts);
  86. * in_data += len;
  87. * in_len -= len;
  88. *
  89. * if(size)
  90. * decode_frame(data, size);
  91. * }
  92. * @endcode
  93. */
  94. int av_parser_parse(AVCodecParserContext *s,
  95. AVCodecContext *avctx,
  96. uint8_t **poutbuf, int *poutbuf_size,
  97. const uint8_t *buf, int buf_size,
  98. int64_t pts, int64_t dts)
  99. {
  100. int index, i, k;
  101. uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
  102. if (buf_size == 0) {
  103. /* padding is always necessary even if EOF, so we add it here */
  104. memset(dummy_buf, 0, sizeof(dummy_buf));
  105. buf = dummy_buf;
  106. } else {
  107. /* add a new packet descriptor */
  108. k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
  109. s->cur_frame_start_index = k;
  110. s->cur_frame_offset[k] = s->cur_offset;
  111. s->cur_frame_pts[k] = pts;
  112. s->cur_frame_dts[k] = dts;
  113. /* fill first PTS/DTS */
  114. if (s->fetch_timestamp){
  115. s->fetch_timestamp=0;
  116. s->last_pts = pts;
  117. s->last_dts = dts;
  118. s->last_offset = 0;
  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. s->offset = s->last_offset;
  133. /* offset of the next frame */
  134. s->last_frame_offset = s->cur_offset + index;
  135. /* find the packet in which the new frame starts. It
  136. is tricky because of MPEG video start codes
  137. which can begin in one packet and finish in
  138. another packet. In the worst case, an MPEG
  139. video start code could be in 4 different
  140. packets. */
  141. k = s->cur_frame_start_index;
  142. for(i = 0; i < AV_PARSER_PTS_NB; i++) {
  143. if (s->last_frame_offset >= s->cur_frame_offset[k])
  144. break;
  145. k = (k - 1) & (AV_PARSER_PTS_NB - 1);
  146. }
  147. s->last_pts = s->cur_frame_pts[k];
  148. s->last_dts = s->cur_frame_dts[k];
  149. s->last_offset = s->last_frame_offset - s->cur_frame_offset[k];
  150. /* some parsers tell us the packet size even before seeing the first byte of the next packet,
  151. so the next pts/dts is in the next chunk */
  152. if(index == buf_size){
  153. s->fetch_timestamp=1;
  154. }
  155. }
  156. if (index < 0)
  157. index = 0;
  158. s->cur_offset += index;
  159. return index;
  160. }
  161. /**
  162. *
  163. * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed
  164. * @deprecated use AVBitstreamFilter
  165. */
  166. int av_parser_change(AVCodecParserContext *s,
  167. AVCodecContext *avctx,
  168. uint8_t **poutbuf, int *poutbuf_size,
  169. const uint8_t *buf, int buf_size, int keyframe){
  170. if(s && s->parser->split){
  171. if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
  172. int i= s->parser->split(avctx, buf, buf_size);
  173. buf += i;
  174. buf_size -= i;
  175. }
  176. }
  177. /* cast to avoid warning about discarding qualifiers */
  178. *poutbuf= (uint8_t *) buf;
  179. *poutbuf_size= buf_size;
  180. if(avctx->extradata){
  181. if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
  182. /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
  183. /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
  184. int size= buf_size + avctx->extradata_size;
  185. *poutbuf_size= size;
  186. *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  187. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  188. memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  189. return 1;
  190. }
  191. }
  192. return 0;
  193. }
  194. void av_parser_close(AVCodecParserContext *s)
  195. {
  196. if (s->parser->parser_close)
  197. s->parser->parser_close(s);
  198. av_free(s->priv_data);
  199. av_free(s);
  200. }
  201. /*****************************************************/
  202. /**
  203. * combines the (truncated) bitstream to a complete frame
  204. * @returns -1 if no complete frame could be created
  205. */
  206. int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
  207. {
  208. #if 0
  209. if(pc->overread){
  210. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  211. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  212. }
  213. #endif
  214. /* Copy overread bytes from last frame into buffer. */
  215. for(; pc->overread>0; pc->overread--){
  216. pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
  217. }
  218. /* flush remaining if EOF */
  219. if(!*buf_size && next == END_NOT_FOUND){
  220. next= 0;
  221. }
  222. pc->last_index= pc->index;
  223. /* copy into buffer end return */
  224. if(next == END_NOT_FOUND){
  225. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  226. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  227. pc->index += *buf_size;
  228. return -1;
  229. }
  230. *buf_size=
  231. pc->overread_index= pc->index + next;
  232. /* append to buffer */
  233. if(pc->index){
  234. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  235. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  236. pc->index = 0;
  237. *buf= pc->buffer;
  238. }
  239. /* store overread bytes */
  240. for(;next < 0; next++){
  241. pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
  242. pc->overread++;
  243. }
  244. #if 0
  245. if(pc->overread){
  246. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  247. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  248. }
  249. #endif
  250. return 0;
  251. }
  252. void ff_parse_close(AVCodecParserContext *s)
  253. {
  254. ParseContext *pc = s->priv_data;
  255. av_free(pc->buffer);
  256. }
  257. void ff_parse1_close(AVCodecParserContext *s)
  258. {
  259. ParseContext1 *pc1 = s->priv_data;
  260. av_free(pc1->pc.buffer);
  261. av_free(pc1->enc);
  262. }
  263. /*************************/
  264. int ff_mpeg4video_split(AVCodecContext *avctx,
  265. const uint8_t *buf, int buf_size)
  266. {
  267. int i;
  268. uint32_t state= -1;
  269. for(i=0; i<buf_size; i++){
  270. state= (state<<8) | buf[i];
  271. if(state == 0x1B3 || state == 0x1B6)
  272. return i-3;
  273. }
  274. return 0;
  275. }
  276. /*************************/
  277. #if defined(CONFIG_AC3_PARSER) || defined(CONFIG_AAC_PARSER)
  278. /* also used for ADTS AAC */
  279. typedef struct AC3ParseContext {
  280. uint8_t *inbuf_ptr;
  281. int frame_size;
  282. int header_size;
  283. int (*sync)(const uint8_t *buf, int *channels, int *sample_rate,
  284. int *bit_rate, int *samples);
  285. uint8_t inbuf[8192]; /* input buffer */
  286. } AC3ParseContext;
  287. #define AC3_HEADER_SIZE 7
  288. #define AAC_HEADER_SIZE 7
  289. #ifdef CONFIG_AC3_PARSER
  290. static const uint8_t eac3_blocks[4] = {
  291. 1, 2, 3, 6
  292. };
  293. #endif /* CONFIG_AC3_PARSER */
  294. #ifdef CONFIG_AAC_PARSER
  295. static const int aac_sample_rates[16] = {
  296. 96000, 88200, 64000, 48000, 44100, 32000,
  297. 24000, 22050, 16000, 12000, 11025, 8000, 7350
  298. };
  299. static const int aac_channels[8] = {
  300. 0, 1, 2, 3, 4, 5, 6, 8
  301. };
  302. #endif
  303. #ifdef CONFIG_AC3_PARSER
  304. int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr)
  305. {
  306. GetBitContext gbc;
  307. memset(hdr, 0, sizeof(*hdr));
  308. init_get_bits(&gbc, buf, 54);
  309. hdr->sync_word = get_bits(&gbc, 16);
  310. if(hdr->sync_word != 0x0B77)
  311. return -1;
  312. /* read ahead to bsid to make sure this is AC-3, not E-AC-3 */
  313. hdr->bsid = show_bits_long(&gbc, 29) & 0x1F;
  314. if(hdr->bsid > 10)
  315. return -2;
  316. hdr->crc1 = get_bits(&gbc, 16);
  317. hdr->fscod = get_bits(&gbc, 2);
  318. if(hdr->fscod == 3)
  319. return -3;
  320. hdr->frmsizecod = get_bits(&gbc, 6);
  321. if(hdr->frmsizecod > 37)
  322. return -4;
  323. skip_bits(&gbc, 5); // skip bsid, already got it
  324. hdr->bsmod = get_bits(&gbc, 3);
  325. hdr->acmod = get_bits(&gbc, 3);
  326. if((hdr->acmod & 1) && hdr->acmod != 1) {
  327. hdr->cmixlev = get_bits(&gbc, 2);
  328. }
  329. if(hdr->acmod & 4) {
  330. hdr->surmixlev = get_bits(&gbc, 2);
  331. }
  332. if(hdr->acmod == 2) {
  333. hdr->dsurmod = get_bits(&gbc, 2);
  334. }
  335. hdr->lfeon = get_bits1(&gbc);
  336. hdr->halfratecod = FFMAX(hdr->bsid, 8) - 8;
  337. hdr->sample_rate = ff_ac3_freqs[hdr->fscod] >> hdr->halfratecod;
  338. hdr->bit_rate = (ff_ac3_bitratetab[hdr->frmsizecod>>1] * 1000) >> hdr->halfratecod;
  339. hdr->channels = ff_ac3_channels[hdr->acmod] + hdr->lfeon;
  340. hdr->frame_size = ff_ac3_frame_sizes[hdr->frmsizecod][hdr->fscod] * 2;
  341. return 0;
  342. }
  343. static int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
  344. int *bit_rate, int *samples)
  345. {
  346. int err;
  347. unsigned int fscod, acmod, bsid, lfeon;
  348. unsigned int strmtyp, substreamid, frmsiz, fscod2, numblkscod;
  349. GetBitContext bits;
  350. AC3HeaderInfo hdr;
  351. err = ff_ac3_parse_header(buf, &hdr);
  352. if(err < 0 && err != -2)
  353. return 0;
  354. bsid = hdr.bsid;
  355. if(bsid <= 10) { /* Normal AC-3 */
  356. *sample_rate = hdr.sample_rate;
  357. *bit_rate = hdr.bit_rate;
  358. *channels = hdr.channels;
  359. *samples = AC3_FRAME_SIZE;
  360. return hdr.frame_size;
  361. } else if (bsid > 10 && bsid <= 16) { /* Enhanced AC-3 */
  362. init_get_bits(&bits, &buf[2], (AC3_HEADER_SIZE-2) * 8);
  363. strmtyp = get_bits(&bits, 2);
  364. substreamid = get_bits(&bits, 3);
  365. if (strmtyp != 0 || substreamid != 0)
  366. return 0; /* Currently don't support additional streams */
  367. frmsiz = get_bits(&bits, 11) + 1;
  368. fscod = get_bits(&bits, 2);
  369. if (fscod == 3) {
  370. fscod2 = get_bits(&bits, 2);
  371. numblkscod = 3;
  372. if(fscod2 == 3)
  373. return 0;
  374. *sample_rate = ff_ac3_freqs[fscod2] / 2;
  375. } else {
  376. numblkscod = get_bits(&bits, 2);
  377. *sample_rate = ff_ac3_freqs[fscod];
  378. }
  379. acmod = get_bits(&bits, 3);
  380. lfeon = get_bits1(&bits);
  381. *samples = eac3_blocks[numblkscod] * 256;
  382. *bit_rate = frmsiz * (*sample_rate) * 16 / (*samples);
  383. *channels = ff_ac3_channels[acmod] + lfeon;
  384. return frmsiz * 2;
  385. }
  386. /* Unsupported bitstream version */
  387. return 0;
  388. }
  389. #endif /* CONFIG_AC3_PARSER */
  390. #ifdef CONFIG_AAC_PARSER
  391. static int aac_sync(const uint8_t *buf, int *channels, int *sample_rate,
  392. int *bit_rate, int *samples)
  393. {
  394. GetBitContext bits;
  395. int size, rdb, ch, sr;
  396. init_get_bits(&bits, buf, AAC_HEADER_SIZE * 8);
  397. if(get_bits(&bits, 12) != 0xfff)
  398. return 0;
  399. skip_bits1(&bits); /* id */
  400. skip_bits(&bits, 2); /* layer */
  401. skip_bits1(&bits); /* protection_absent */
  402. skip_bits(&bits, 2); /* profile_objecttype */
  403. sr = get_bits(&bits, 4); /* sample_frequency_index */
  404. if(!aac_sample_rates[sr])
  405. return 0;
  406. skip_bits1(&bits); /* private_bit */
  407. ch = get_bits(&bits, 3); /* channel_configuration */
  408. if(!aac_channels[ch])
  409. return 0;
  410. skip_bits1(&bits); /* original/copy */
  411. skip_bits1(&bits); /* home */
  412. /* adts_variable_header */
  413. skip_bits1(&bits); /* copyright_identification_bit */
  414. skip_bits1(&bits); /* copyright_identification_start */
  415. size = get_bits(&bits, 13); /* aac_frame_length */
  416. skip_bits(&bits, 11); /* adts_buffer_fullness */
  417. rdb = get_bits(&bits, 2); /* number_of_raw_data_blocks_in_frame */
  418. *channels = aac_channels[ch];
  419. *sample_rate = aac_sample_rates[sr];
  420. *samples = (rdb + 1) * 1024;
  421. *bit_rate = size * 8 * *sample_rate / *samples;
  422. return size;
  423. }
  424. #endif /* CONFIG_AAC_PARSER */
  425. #ifdef CONFIG_AC3_PARSER
  426. static int ac3_parse_init(AVCodecParserContext *s1)
  427. {
  428. AC3ParseContext *s = s1->priv_data;
  429. s->inbuf_ptr = s->inbuf;
  430. s->header_size = AC3_HEADER_SIZE;
  431. s->sync = ac3_sync;
  432. return 0;
  433. }
  434. #endif
  435. #ifdef CONFIG_AAC_PARSER
  436. static int aac_parse_init(AVCodecParserContext *s1)
  437. {
  438. AC3ParseContext *s = s1->priv_data;
  439. s->inbuf_ptr = s->inbuf;
  440. s->header_size = AAC_HEADER_SIZE;
  441. s->sync = aac_sync;
  442. return 0;
  443. }
  444. #endif
  445. /* also used for ADTS AAC */
  446. static int ac3_parse(AVCodecParserContext *s1,
  447. AVCodecContext *avctx,
  448. uint8_t **poutbuf, int *poutbuf_size,
  449. const uint8_t *buf, int buf_size)
  450. {
  451. AC3ParseContext *s = s1->priv_data;
  452. const uint8_t *buf_ptr;
  453. int len, sample_rate, bit_rate, channels, samples;
  454. *poutbuf = NULL;
  455. *poutbuf_size = 0;
  456. buf_ptr = buf;
  457. while (buf_size > 0) {
  458. len = s->inbuf_ptr - s->inbuf;
  459. if (s->frame_size == 0) {
  460. /* no header seen : find one. We need at least s->header_size
  461. bytes to parse it */
  462. len = FFMIN(s->header_size - len, buf_size);
  463. memcpy(s->inbuf_ptr, buf_ptr, len);
  464. buf_ptr += len;
  465. s->inbuf_ptr += len;
  466. buf_size -= len;
  467. if ((s->inbuf_ptr - s->inbuf) == s->header_size) {
  468. len = s->sync(s->inbuf, &channels, &sample_rate, &bit_rate,
  469. &samples);
  470. if (len == 0) {
  471. /* no sync found : move by one byte (inefficient, but simple!) */
  472. memmove(s->inbuf, s->inbuf + 1, s->header_size - 1);
  473. s->inbuf_ptr--;
  474. } else {
  475. s->frame_size = len;
  476. /* update codec info */
  477. avctx->sample_rate = sample_rate;
  478. /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
  479. if(avctx->codec_id == CODEC_ID_AC3){
  480. if(avctx->channels!=1 && avctx->channels!=2){
  481. avctx->channels = channels;
  482. }
  483. } else {
  484. avctx->channels = channels;
  485. }
  486. avctx->bit_rate = bit_rate;
  487. avctx->frame_size = samples;
  488. }
  489. }
  490. } else {
  491. len = FFMIN(s->frame_size - len, buf_size);
  492. memcpy(s->inbuf_ptr, buf_ptr, len);
  493. buf_ptr += len;
  494. s->inbuf_ptr += len;
  495. buf_size -= len;
  496. if(s->inbuf_ptr - s->inbuf == s->frame_size){
  497. *poutbuf = s->inbuf;
  498. *poutbuf_size = s->frame_size;
  499. s->inbuf_ptr = s->inbuf;
  500. s->frame_size = 0;
  501. break;
  502. }
  503. }
  504. }
  505. return buf_ptr - buf;
  506. }
  507. #endif /* CONFIG_AC3_PARSER || CONFIG_AAC_PARSER */
  508. #ifdef CONFIG_AC3_PARSER
  509. AVCodecParser ac3_parser = {
  510. { CODEC_ID_AC3 },
  511. sizeof(AC3ParseContext),
  512. ac3_parse_init,
  513. ac3_parse,
  514. NULL,
  515. };
  516. #endif
  517. #ifdef CONFIG_AAC_PARSER
  518. AVCodecParser aac_parser = {
  519. { CODEC_ID_AAC },
  520. sizeof(AC3ParseContext),
  521. aac_parse_init,
  522. ac3_parse,
  523. NULL,
  524. };
  525. #endif