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 } DCAParseContext;
00036
00037 #define IS_MARKER(state, i, buf, buf_size) \
00038 ((state == DCA_MARKER_14B_LE && (i < buf_size-2) && (buf[i+1] & 0xF0) == 0xF0 && buf[i+2] == 0x07) \
00039 || (state == DCA_MARKER_14B_BE && (i < buf_size-2) && buf[i+1] == 0x07 && (buf[i+2] & 0xF0) == 0xF0) \
00040 || state == DCA_MARKER_RAW_LE || state == DCA_MARKER_RAW_BE)
00041
00046 static int dca_find_frame_end(DCAParseContext * pc1, const uint8_t * buf,
00047 int buf_size)
00048 {
00049 int start_found, i;
00050 uint32_t state;
00051 ParseContext *pc = &pc1->pc;
00052
00053 start_found = pc->frame_start_found;
00054 state = pc->state;
00055
00056 i = 0;
00057 if (!start_found) {
00058 for (i = 0; i < buf_size; i++) {
00059 state = (state << 8) | buf[i];
00060 if (IS_MARKER(state, i, buf, buf_size)) {
00061 if (pc1->lastmarker && state == pc1->lastmarker) {
00062 start_found = 1;
00063 break;
00064 } else if (!pc1->lastmarker) {
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 state = (state << 8) | buf[i];
00075 if (state == pc1->lastmarker && IS_MARKER(state, i, buf, buf_size)) {
00076 pc->frame_start_found = 0;
00077 pc->state = -1;
00078 return i - 3;
00079 }
00080 }
00081 }
00082 pc->frame_start_found = start_found;
00083 pc->state = state;
00084 return END_NOT_FOUND;
00085 }
00086
00087 static int dca_parse_init(AVCodecParserContext * s)
00088 {
00089 DCAParseContext *pc1 = s->priv_data;
00090
00091 pc1->lastmarker = 0;
00092 return 0;
00093 }
00094
00095 static int dca_parse(AVCodecParserContext * s,
00096 AVCodecContext * avctx,
00097 const uint8_t ** poutbuf, int *poutbuf_size,
00098 const uint8_t * buf, int buf_size)
00099 {
00100 DCAParseContext *pc1 = s->priv_data;
00101 ParseContext *pc = &pc1->pc;
00102 int next;
00103
00104 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
00105 next = buf_size;
00106 } else {
00107 next = dca_find_frame_end(pc1, buf, buf_size);
00108
00109 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00110 *poutbuf = NULL;
00111 *poutbuf_size = 0;
00112 return buf_size;
00113 }
00114 }
00115 *poutbuf = buf;
00116 *poutbuf_size = buf_size;
00117 return next;
00118 }
00119
00120 AVCodecParser dca_parser = {
00121 {CODEC_ID_DTS},
00122 sizeof(DCAParseContext),
00123 dca_parse_init,
00124 dca_parse,
00125 ff_parse_close,
00126 };