00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00030 #include "libavutil/intreadwrite.h"
00031 #include "parser.h"
00032
00033 #define DIRAC_PARSE_INFO_PREFIX 0x42424344
00034
00039 typedef struct DiracParseContext {
00040 int state;
00041 int is_synced;
00042 int sync_offset;
00043 int header_bytes_needed;
00044 int overread_index;
00045 int buffer_size;
00046 int index;
00047 uint8_t *buffer;
00048 int dirac_unit_size;
00049 uint8_t *dirac_unit;
00050 } DiracParseContext;
00051
00052 static int find_frame_end(DiracParseContext *pc,
00053 const uint8_t *buf, int buf_size)
00054 {
00055 uint32_t state = pc->state;
00056 int i = 0;
00057
00058 if (!pc->is_synced) {
00059 for (i = 0; i < buf_size; i++) {
00060 state = (state << 8) | buf[i];
00061 if (state == DIRAC_PARSE_INFO_PREFIX) {
00062 state = -1;
00063 pc->is_synced = 1;
00064 pc->header_bytes_needed = 9;
00065 pc->sync_offset = i;
00066 break;
00067 }
00068 }
00069 }
00070
00071 if (pc->is_synced) {
00072 pc->sync_offset = 0;
00073 for (; i < buf_size; i++) {
00074 if (state == DIRAC_PARSE_INFO_PREFIX) {
00075 if ((buf_size-i) >= pc->header_bytes_needed) {
00076 pc->state = -1;
00077 return i + pc->header_bytes_needed;
00078 } else {
00079 pc->header_bytes_needed = 9-(buf_size-i);
00080 break;
00081 }
00082 } else
00083 state = (state << 8) | buf[i];
00084 }
00085 }
00086 pc->state = state;
00087 return -1;
00088 }
00089
00090 typedef struct DiracParseUnit
00091 {
00092 int next_pu_offset;
00093 int prev_pu_offset;
00094 uint8_t pu_type;
00095 } DiracParseUnit;
00096
00097 static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
00098 int offset)
00099 {
00100 uint8_t *start = pc->buffer + offset;
00101 uint8_t *end = pc->buffer + pc->index;
00102 if (start < pc->buffer || (start+13 > end))
00103 return 0;
00104 pu->pu_type = start[4];
00105
00106 pu->next_pu_offset = AV_RB32(start+5);
00107 pu->prev_pu_offset = AV_RB32(start+9);
00108
00109 if (pu->pu_type == 0x10 && pu->next_pu_offset == 0)
00110 pu->next_pu_offset = 13;
00111
00112 return 1;
00113 }
00114
00115 static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
00116 int next, const uint8_t **buf, int *buf_size)
00117 {
00118 int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
00119 s->dts == AV_NOPTS_VALUE);
00120 DiracParseContext *pc = s->priv_data;
00121
00122 if (pc->overread_index) {
00123 memcpy(pc->buffer, pc->buffer + pc->overread_index,
00124 pc->index - pc->overread_index);
00125 pc->index -= pc->overread_index;
00126 pc->overread_index = 0;
00127 if (*buf_size == 0 && pc->buffer[4] == 0x10) {
00128 *buf = pc->buffer;
00129 *buf_size = pc->index;
00130 return 0;
00131 }
00132 }
00133
00134 if ( next == -1) {
00135
00136 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
00137 pc->index + (*buf_size -
00138 pc->sync_offset));
00139 pc->buffer = new_buffer;
00140 memcpy(pc->buffer+pc->index, (*buf + pc->sync_offset),
00141 *buf_size - pc->sync_offset);
00142 pc->index += *buf_size - pc->sync_offset;
00143 return -1;
00144 } else {
00145
00146 DiracParseUnit pu1, pu;
00147 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
00148 pc->index + next);
00149 pc->buffer = new_buffer;
00150 memcpy(pc->buffer + pc->index, *buf, next);
00151 pc->index += next;
00152
00153
00154
00155
00156
00157
00158
00159 if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
00160 !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
00161 pu.next_pu_offset != pu1.prev_pu_offset) {
00162 pc->index -= 9;
00163 *buf_size = next-9;
00164 pc->header_bytes_needed = 9;
00165 return -1;
00166 }
00167
00168
00169
00170
00171
00172 pc->dirac_unit = pc->buffer + pc->index - 13 -
00173 pu1.prev_pu_offset - pc->dirac_unit_size;
00174
00175 pc->dirac_unit_size += pu.next_pu_offset;
00176
00177 if ((pu.pu_type&0x08) != 0x08) {
00178 pc->header_bytes_needed = 9;
00179 *buf_size = next;
00180 return -1;
00181 }
00182
00183
00184 if (parse_timing_info) {
00185 uint8_t *cur_pu = pc->buffer +
00186 pc->index - 13 - pu1.prev_pu_offset;
00187 int pts = AV_RB32(cur_pu + 13);
00188 if (s->last_pts == 0 && s->last_dts == 0)
00189 s->dts = pts - 1;
00190 else
00191 s->dts = s->last_dts+1;
00192 s->pts = pts;
00193 if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
00194 avctx->has_b_frames = 1;
00195 }
00196 if (avctx->has_b_frames && s->pts == s->dts)
00197 s->pict_type = AV_PICTURE_TYPE_B;
00198
00199
00200 *buf = pc->dirac_unit;
00201 *buf_size = pc->dirac_unit_size;
00202
00203 pc->dirac_unit_size = 0;
00204 pc->overread_index = pc->index-13;
00205 pc->header_bytes_needed = 9;
00206 }
00207 return next;
00208 }
00209
00210 static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
00211 const uint8_t **poutbuf, int *poutbuf_size,
00212 const uint8_t *buf, int buf_size)
00213 {
00214 DiracParseContext *pc = s->priv_data;
00215 int next;
00216
00217 *poutbuf = NULL;
00218 *poutbuf_size = 0;
00219
00220 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
00221 next = buf_size;
00222 *poutbuf = buf;
00223 *poutbuf_size = buf_size;
00224
00225 } else {
00226 next = find_frame_end(pc, buf, buf_size);
00227 if (!pc->is_synced && next == -1) {
00228
00229 return buf_size;
00230 }
00231
00232 if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0) {
00233 return buf_size;
00234 }
00235 }
00236
00237 *poutbuf = buf;
00238 *poutbuf_size = buf_size;
00239 return next;
00240 }
00241
00242 static void dirac_parse_close(AVCodecParserContext *s)
00243 {
00244 DiracParseContext *pc = s->priv_data;
00245
00246 if (pc->buffer_size > 0)
00247 av_free(pc->buffer);
00248 }
00249
00250 AVCodecParser ff_dirac_parser = {
00251 .codec_ids = { CODEC_ID_DIRAC },
00252 .priv_data_size = sizeof(DiracParseContext),
00253 .parser_parse = dirac_parse,
00254 .parser_close = dirac_parse_close,
00255 };