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