Libav
|
00001 /* 00002 * Dirac parser 00003 * 00004 * Copyright (c) 2007-2008 Marco Gerards <marco@gnu.org> 00005 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju@gmail.com> 00006 * 00007 * This file is part of FFmpeg. 00008 * 00009 * FFmpeg is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * FFmpeg is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with FFmpeg; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 /* Found a possible frame start but not a frame end */ 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 /* Found a possible frame start and a possible frame end */ 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 /* Need to check if we have a valid Parse Unit. We can't go by the 00154 * sync pattern 'BBCD' alone because arithmetic coding of the residual 00155 * and motion data can cause the pattern triggering a false start of 00156 * frame. So check if the previous parse offset of the next parse unit 00157 * is equal to the next parse offset of the current parse unit then 00158 * we can be pretty sure that we have a valid parse unit */ 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 /* All non-frame data must be accompanied by frame data. This is to 00169 * ensure that pts is set correctly. So if the current parse unit is 00170 * not frame data, wait for frame data to come along */ 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 /* Get the picture number to set the pts and dts*/ 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 = FF_B_TYPE; 00198 00199 /* Finally have a complete Dirac data unit */ 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 /* Assume that data has been packetized into an encapsulation unit. */ 00225 } else { 00226 next = find_frame_end(pc, buf, buf_size); 00227 if (!pc->is_synced && next == -1) { 00228 /* No frame start found yet. So throw away the entire buffer. */ 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 dirac_parser = { 00251 { CODEC_ID_DIRAC }, 00252 sizeof(DiracParseContext), 00253 NULL, 00254 dirac_parse, 00255 dirac_parse_close, 00256 };