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