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