00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00029 #include "parser.h"
00030 #include "dca.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)
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) {
00065 start_found = 1;
00066 break;
00067 } else if (!pc1->lastmarker) {
00068 start_found = 1;
00069 pc1->lastmarker = state;
00070 break;
00071 }
00072 }
00073 }
00074 }
00075 if (start_found) {
00076 for (; i < buf_size; i++) {
00077 pc1->size++;
00078 state = (state << 8) | buf[i];
00079 if (state == DCA_HD_MARKER && !pc1->hd_pos)
00080 pc1->hd_pos = pc1->size;
00081 if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size)) {
00082 if(pc1->framesize > pc1->size)
00083 continue;
00084 if(!pc1->framesize){
00085 pc1->framesize = pc1->hd_pos ? pc1->hd_pos : pc1->size;
00086 }
00087 pc->frame_start_found = 0;
00088 pc->state = -1;
00089 pc1->size = 0;
00090 return i - 3;
00091 }
00092 }
00093 }
00094 pc->frame_start_found = start_found;
00095 pc->state = state;
00096 return END_NOT_FOUND;
00097 }
00098
00099 static av_cold int dca_parse_init(AVCodecParserContext * s)
00100 {
00101 DCAParseContext *pc1 = s->priv_data;
00102
00103 pc1->lastmarker = 0;
00104 return 0;
00105 }
00106
00107 static int dca_parse(AVCodecParserContext * s,
00108 AVCodecContext * avctx,
00109 const uint8_t ** poutbuf, int *poutbuf_size,
00110 const uint8_t * buf, int buf_size)
00111 {
00112 DCAParseContext *pc1 = s->priv_data;
00113 ParseContext *pc = &pc1->pc;
00114 int next;
00115
00116 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
00117 next = buf_size;
00118 } else {
00119 next = dca_find_frame_end(pc1, buf, buf_size);
00120
00121 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00122 *poutbuf = NULL;
00123 *poutbuf_size = 0;
00124 return buf_size;
00125 }
00126 }
00127 *poutbuf = buf;
00128 *poutbuf_size = buf_size;
00129 return next;
00130 }
00131
00132 AVCodecParser dca_parser = {
00133 {CODEC_ID_DTS},
00134 sizeof(DCAParseContext),
00135 dca_parse_init,
00136 dca_parse,
00137 ff_parse_close,
00138 };