00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "parser.h"
00026 #include "dca.h"
00027 #include "dca_parser.h"
00028 #include "get_bits.h"
00029 #include "put_bits.h"
00030
00031 typedef struct DCAParseContext {
00032 ParseContext pc;
00033 uint32_t lastmarker;
00034 int size;
00035 int framesize;
00036 int hd_pos;
00037 } DCAParseContext;
00038
00039 #define IS_MARKER(state, i, buf, buf_size) \
00040 ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
00041 || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
00042 || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE || state == DCA_HD_MARKER)
00043
00048 static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
00049 int buf_size)
00050 {
00051 int start_found, i;
00052 uint32_t state;
00053 ParseContext *pc = &pc1->pc;
00054
00055 start_found = pc->frame_start_found;
00056 state = pc->state;
00057
00058 i = 0;
00059 if (!start_found) {
00060 for (i = 0; i < buf_size; i++) {
00061 state = (state << 8) | buf[i];
00062 if (IS_MARKER(state, i, buf, buf_size)) {
00063 if (!pc1->lastmarker || state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER) {
00064 start_found = 1;
00065 pc1->lastmarker = state;
00066 break;
00067 }
00068 }
00069 }
00070 }
00071 if (start_found) {
00072 for (; i < buf_size; i++) {
00073 pc1->size++;
00074 state = (state << 8) | buf[i];
00075 if (state == DCA_HD_MARKER && !pc1->hd_pos)
00076 pc1->hd_pos = pc1->size;
00077 if (IS_MARKER(state, i, buf, buf_size) && (state == pc1->lastmarker || pc1->lastmarker == DCA_HD_MARKER)) {
00078 if(pc1->framesize > pc1->size)
00079 continue;
00080
00081 if(!pc1->framesize && state == pc1->lastmarker && state != DCA_HD_MARKER){
00082 pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
00083 }
00084 pc->frame_start_found = 0;
00085 pc->state = -1;
00086 pc1->size = 0;
00087 return i - 3;
00088 }
00089 }
00090 }
00091 pc->frame_start_found = start_found;
00092 pc->state = state;
00093 return END_NOT_FOUND;
00094 }
00095
00096 static av_cold int dca_parse_init(AVCodecParserContext * s)
00097 {
00098 DCAParseContext *pc1 = s->priv_data;
00099
00100 pc1->lastmarker = 0;
00101 return 0;
00102 }
00103
00104 int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
00105 int max_size)
00106 {
00107 uint32_t mrk;
00108 int i, tmp;
00109 const uint16_t *ssrc = (const uint16_t *) src;
00110 uint16_t *sdst = (uint16_t *) dst;
00111 PutBitContext pb;
00112
00113 if ((unsigned) src_size > (unsigned) max_size)
00114 src_size = max_size;
00115
00116 mrk = AV_RB32(src);
00117 switch (mrk) {
00118 case DCA_MARKER_RAW_BE:
00119 memcpy(dst, src, src_size);
00120 return src_size;
00121 case DCA_MARKER_RAW_LE:
00122 for (i = 0; i < (src_size + 1) >> 1; i++)
00123 *sdst++ = av_bswap16(*ssrc++);
00124 return src_size;
00125 case DCA_MARKER_14B_BE:
00126 case DCA_MARKER_14B_LE:
00127 init_put_bits(&pb, dst, max_size);
00128 for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
00129 tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
00130 put_bits(&pb, 14, tmp);
00131 }
00132 flush_put_bits(&pb);
00133 return (put_bits_count(&pb) + 7) >> 3;
00134 default:
00135 return AVERROR_INVALIDDATA;
00136 }
00137 }
00138
00139 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
00140 int *sample_rate)
00141 {
00142 GetBitContext gb;
00143 uint8_t hdr[12 + FF_INPUT_BUFFER_PADDING_SIZE] = { 0 };
00144 int ret, sample_blocks, sr_code;
00145
00146 if (buf_size < 12)
00147 return AVERROR_INVALIDDATA;
00148
00149 if ((ret = ff_dca_convert_bitstream(buf, 12, hdr, 12)) < 0)
00150 return ret;
00151
00152 init_get_bits(&gb, hdr, 96);
00153
00154 skip_bits_long(&gb, 39);
00155 sample_blocks = get_bits(&gb, 7) + 1;
00156 if (sample_blocks < 8)
00157 return AVERROR_INVALIDDATA;
00158 *duration = 256 * (sample_blocks / 8);
00159
00160 skip_bits(&gb, 20);
00161 sr_code = get_bits(&gb, 4);
00162 *sample_rate = avpriv_dca_sample_rates[sr_code];
00163 if (*sample_rate == 0)
00164 return AVERROR_INVALIDDATA;
00165
00166 return 0;
00167 }
00168
00169 static int dca_parse(AVCodecParserContext * s,
00170 AVCodecContext * avctx,
00171 const uint8_t ** poutbuf, int *poutbuf_size,
00172 const uint8_t * buf, int buf_size)
00173 {
00174 DCAParseContext *pc1 = s->priv_data;
00175 ParseContext *pc = &pc1->pc;
00176 int next, duration, sample_rate;
00177
00178 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
00179 next = buf_size;
00180 } else {
00181 next = dca_find_frame_end(pc1, buf, buf_size);
00182
00183 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00184 *poutbuf = NULL;
00185 *poutbuf_size = 0;
00186 return buf_size;
00187 }
00188 }
00189
00190
00191 if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
00192 s->duration = duration;
00193 if (!avctx->sample_rate)
00194 avctx->sample_rate = sample_rate;
00195 } else
00196 s->duration = 0;
00197
00198 *poutbuf = buf;
00199 *poutbuf_size = buf_size;
00200 return next;
00201 }
00202
00203 AVCodecParser ff_dca_parser = {
00204 .codec_ids = { AV_CODEC_ID_DTS },
00205 .priv_data_size = sizeof(DCAParseContext),
00206 .parser_init = dca_parse_init,
00207 .parser_parse = dca_parse,
00208 .parser_close = ff_parse_close,
00209 };