yading@10: /* yading@10: * DCA parser yading@10: * Copyright (C) 2004 Gildas Bazin yading@10: * Copyright (C) 2004 Benjamin Zores yading@10: * Copyright (C) 2006 Benjamin Larsson yading@10: * Copyright (C) 2007 Konstantin Shishkov yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #include "parser.h" yading@10: #include "dca.h" yading@10: #include "get_bits.h" yading@10: yading@10: typedef struct DCAParseContext { yading@10: ParseContext pc; yading@10: uint32_t lastmarker; yading@10: int size; yading@10: int framesize; yading@10: int hd_pos; yading@10: } DCAParseContext; yading@10: yading@10: #define IS_MARKER(state, i, buf, buf_size) \ yading@10: ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \ yading@10: || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \ yading@10: || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE || state == DCA_HD_MARKER) yading@10: yading@10: /** yading@10: * Find the end of the current frame in the bitstream. yading@10: * @return the position of the first byte of the next frame, or -1 yading@10: */ yading@10: static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf, yading@10: int buf_size) yading@10: { yading@10: int start_found, i; yading@10: uint32_t state; yading@10: ParseContext *pc = &pc1->pc; yading@10: yading@10: start_found = pc->frame_start_found; yading@10: state = pc->state; yading@10: yading@10: i = 0; yading@10: if (!start_found) { yading@10: for (i = 0; i < buf_size; i++) { yading@10: state = (state << 8) | buf[i]; yading@10: if (IS_MARKER(state, i, buf, buf_size)) { yading@10: if (!pc1->lastmarker || state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER) { yading@10: start_found = 1; yading@10: pc1->lastmarker = state; yading@10: break; yading@10: } yading@10: } yading@10: } yading@10: } yading@10: if (start_found) { yading@10: for (; i < buf_size; i++) { yading@10: pc1->size++; yading@10: state = (state << 8) | buf[i]; yading@10: if (state == DCA_HD_MARKER && !pc1->hd_pos) yading@10: pc1->hd_pos = pc1->size; yading@10: if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER)) { yading@10: if(pc1->framesize > pc1->size) yading@10: continue; yading@10: // We have to check that we really read a full frame here, and that it isn't a pure HD frame, because their size is not constant. yading@10: if(!pc1->framesize && state == pc1->lastmarker && state != DCA_HD_MARKER){ yading@10: pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size; yading@10: } yading@10: pc->frame_start_found = 0; yading@10: pc->state = -1; yading@10: pc1->size = 0; yading@10: return i - 3; yading@10: } yading@10: } yading@10: } yading@10: pc->frame_start_found = start_found; yading@10: pc->state = state; yading@10: return END_NOT_FOUND; yading@10: } yading@10: yading@10: static av_cold int dca_parse_init(AVCodecParserContext * s) yading@10: { yading@10: DCAParseContext *pc1 = s->priv_data; yading@10: yading@10: pc1->lastmarker = 0; yading@10: return 0; yading@10: } yading@10: yading@10: static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration, yading@10: int *sample_rate) yading@10: { yading@10: GetBitContext gb; yading@10: uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 }; yading@10: int ret, sample_blocks, sr_code; yading@10: yading@10: if (buf_size < 12) yading@10: return AVERROR_INVALIDDATA; yading@10: yading@10: if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0) yading@10: return ret; yading@10: yading@10: init_get_bits(&gb, hdr, 96); yading@10: yading@10: skip_bits_long(&gb, 39); yading@10: sample_blocks = get_bits(&gb, 7) + 1; yading@10: if (sample_blocks < 8) yading@10: return AVERROR_INVALIDDATA; yading@10: *duration = 256 * (sample_blocks / 8); yading@10: yading@10: skip_bits(&gb, 20); yading@10: sr_code = get_bits(&gb, 4); yading@10: *sample_rate = avpriv_dca_sample_rates[sr_code]; yading@10: if (*sample_rate == 0) yading@10: return AVERROR_INVALIDDATA; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int dca_parse(AVCodecParserContext * s, yading@10: AVCodecContext * avctx, yading@10: const uint8_t ** poutbuf, int *poutbuf_size, yading@10: const uint8_t * buf, int buf_size) yading@10: { yading@10: DCAParseContext *pc1 = s->priv_data; yading@10: ParseContext *pc = &pc1->pc; yading@10: int next, duration, sample_rate; yading@10: yading@10: if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { yading@10: next = buf_size; yading@10: } else { yading@10: next = dca_find_frame_end(pc1, buf, buf_size); yading@10: yading@10: if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { yading@10: *poutbuf = NULL; yading@10: *poutbuf_size = 0; yading@10: return buf_size; yading@10: } yading@10: } yading@10: yading@10: /* read the duration and sample rate from the frame header */ yading@10: if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) { yading@10: s->duration = duration; yading@10: avctx->sample_rate = sample_rate; yading@10: } else yading@10: s->duration = 0; yading@10: yading@10: *poutbuf = buf; yading@10: *poutbuf_size = buf_size; yading@10: return next; yading@10: } yading@10: yading@10: AVCodecParser ff_dca_parser = { yading@10: .codec_ids = { AV_CODEC_ID_DTS }, yading@10: .priv_data_size = sizeof(DCAParseContext), yading@10: .parser_init = dca_parse_init, yading@10: .parser_parse = dca_parse, yading@10: .parser_close = ff_parse_close, yading@10: };