1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/*****************************************************************
* gmerlin-avdecoder - a general purpose multimedia decoding library
*
* Copyright (c) 2001 - 2012 Members of the Gmerlin project
* gmerlin-general@lists.sourceforge.net
* http://gmerlin.sourceforge.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <stdlib.h>
#include <avdec_private.h>
#include <vc1_header.h>
#include <mpv_header.h>
#include <parser.h>
#include <videoparser_priv.h>
#define VC1_NEED_START 0
#define VC1_HAVE_START 1
#define VC1_NEED_END 2
#define VC1_HAVE_END 3
#define STATE_SEQUENCE 1
#define STATE_ENTRY_POINT 2
#define STATE_PICTURE 3
#define STATE_SYNC 100
#define LOG_DOMAIN "parse_vc1"
typedef struct
{
// int chunk_len; /* Len from one startcode to the next */
/* Unescaped data */
uint8_t * buf;
int buf_alloc;
int buf_len;
int has_picture_start;
int have_sh;
bgav_vc1_sequence_header_t sh;
int state;
} vc1_priv_t;
static void unescape_data(bgav_video_parser_t * parser,
const uint8_t * ptr, int len)
{
vc1_priv_t * priv = parser->priv;
if(priv->buf_alloc < len)
{
priv->buf_alloc = len + 1024;
priv->buf = realloc(priv->buf, priv->buf_alloc);
}
priv->buf_len =
bgav_vc1_unescape_buffer(ptr, len, priv->buf);
}
static void handle_sequence(bgav_video_parser_t * parser,
const uint8_t * ptr,
const uint8_t * end)
{
vc1_priv_t * priv = parser->priv;
if(priv->have_sh)
return;
unescape_data(parser, ptr, end - ptr);
bgav_vc1_sequence_header_read(parser->s->opt, &priv->sh,
priv->buf, priv->buf_len);
bgav_vc1_sequence_header_dump(&priv->sh);
if(priv->sh.profile == PROFILE_ADVANCED)
{
parser->format->timescale = priv->sh.h.adv.timescale;
parser->format->frame_duration = priv->sh.h.adv.frame_duration;
}
priv->have_sh = 1;
}
#if 0
static void handle_picture(bgav_video_parser_t * parser)
{
bgav_vc1_picture_header_adv_t aph;
vc1_priv_t * priv = parser->priv;
if(priv->sh.profile == PROFILE_ADVANCED)
{
unescape_data(parser);
bgav_vc1_picture_header_adv_read(parser->s->opt,
&aph,
priv->buf, priv->buf_len,
&priv->sh);
bgav_vc1_picture_header_adv_dump(&aph);
bgav_video_parser_set_coding_type(parser, aph.coding_type);
}
// else
}
static int parse_vc1(bgav_video_parser_t * parser)
{
const uint8_t * sc;
vc1_priv_t * priv = parser->priv;
switch(priv->state)
{
case VC1_NEED_START:
sc = bgav_mpv_find_startcode(parser->buf.buffer + parser->pos,
parser->buf.buffer + parser->buf.size);
if(!sc)
return PARSER_NEED_DATA;
bgav_video_parser_flush(parser, sc - parser->buf.buffer);
parser->pos = 0;
priv->state = VC1_HAVE_START;
break;
case VC1_HAVE_START:
switch(parser->buf.buffer[parser->pos+3])
{
case VC1_CODE_SEQUENCE:
/* Set picture start */
if(!priv->has_picture_start &&
!bgav_video_parser_set_picture_start(parser))
return PARSER_ERROR;
priv->has_picture_start = 1;
break;
case VC1_CODE_ENTRY_POINT:
/* Set picture start */
if(!priv->has_picture_start &&
!bgav_video_parser_set_picture_start(parser))
return PARSER_ERROR;
priv->has_picture_start = 1;
break;
case VC1_CODE_PICTURE:
/* Extract extradata */
if(!parser->s->ci.global_header)
{
bgav_video_parser_extract_header(parser);
// bgav_video_parser_flush(parser, parser->header_len);
return PARSER_CONTINUE;
}
/* Set picture start */
if(!priv->has_picture_start &&
!bgav_video_parser_set_picture_start(parser))
return PARSER_ERROR;
priv->has_picture_start = 0;
break;
}
/* Need to find the end */
priv->state = VC1_NEED_END;
break;
case VC1_NEED_END:
sc = bgav_mpv_find_startcode(parser->buf.buffer + parser->pos + 4,
parser->buf.buffer + parser->buf.size);
if(!sc)
return PARSER_NEED_DATA;
priv->chunk_len = sc - (parser->buf.buffer + parser->pos);
// fprintf(stderr, "Got nal %d bytes\n", priv->nal_len);
priv->state = VC1_HAVE_END;
break;
case VC1_HAVE_END:
if((parser->buf.buffer[parser->pos+3] == VC1_CODE_SEQUENCE) &&
!priv->have_sh)
handle_sequence(parser);
else if(parser->buf.buffer[parser->pos+3] == VC1_CODE_PICTURE)
handle_picture(parser);
parser->pos += priv->chunk_len;
priv->state = VC1_HAVE_START;
break;
}
return PARSER_CONTINUE;
}
#endif
static int find_frame_boundary_vc1(bgav_video_parser_t * parser, int * skip)
{
const uint8_t * sc;
vc1_priv_t * priv = parser->priv;
int new_state;
while(1)
{
sc = bgav_mpv_find_startcode(parser->buf.buffer + parser->pos,
parser->buf.buffer + parser->buf.size - 4);
if(!sc)
{
parser->pos = parser->buf.size - 4;
if(parser->pos < 0)
parser->pos = 0;
return 0;
}
new_state = -1;
switch(sc[3])
{
case VC1_CODE_SEQUENCE:
/* Sequence header */
new_state = STATE_SEQUENCE;
break;
case VC1_CODE_ENTRY_POINT:
new_state = STATE_ENTRY_POINT;
break;
case VC1_CODE_PICTURE:
new_state = STATE_PICTURE;
break;
}
parser->pos = sc - parser->buf.buffer;
if(new_state < 0)
parser->pos += 4;
else if(((new_state == STATE_PICTURE) && (priv->state == STATE_PICTURE)) ||
((new_state <= STATE_PICTURE) && (new_state < priv->state)))
{
*skip = 4;
parser->pos = sc - parser->buf.buffer;
priv->state = new_state;
return 1;
}
else
{
parser->pos += 4;
priv->state = new_state;
}
}
return 0;
}
static int parse_frame_vc1(bgav_video_parser_t * parser, bgav_packet_t * p,
int64_t prs_orig)
{
const uint8_t * ptr;
const uint8_t * sh_start = NULL;
const uint8_t * chunk_start;
const uint8_t * chunk_end;
vc1_priv_t * priv = parser->priv;
ptr = p->data;
chunk_start = p->data;
while(chunk_start < p->data + p->data_size)
{
ptr = chunk_start + 4;
chunk_end =
bgav_mpv_find_startcode(ptr, p->data +
(p->data_size - (ptr - p->data)));
if(!chunk_end)
chunk_end = p->data + p->data_size;
switch(chunk_start[3])
{
case VC1_CODE_SEQUENCE:
/* Sequence header */
sh_start = chunk_start;
handle_sequence(parser, chunk_start, chunk_end);
break;
case VC1_CODE_ENTRY_POINT:
if(!priv->have_sh)
{
PACKET_SET_SKIP(p);
return 1;
}
else if(sh_start && !parser->s->ci.global_header)
{
bgav_stream_set_extradata(parser->s,
sh_start, chunk_end - sh_start);
#if 0
fprintf(stderr, "Setting extradata %ld bytes\n",
chunk_end - sh_start);
gavl_hexdump(sh_start, chunk_end - sh_start + 4, 16);
#endif
}
break;
case VC1_CODE_PICTURE:
if(!priv->have_sh)
{
PACKET_SET_SKIP(p);
return 1;
}
p->duration = parser->format->frame_duration;
if(priv->sh.profile == PROFILE_ADVANCED)
{
bgav_vc1_picture_header_adv_t aph;
unescape_data(parser, chunk_start, chunk_end - chunk_start);
bgav_vc1_picture_header_adv_read(parser->s->opt,
&aph,
priv->buf, priv->buf_len,
&priv->sh);
// bgav_vc1_picture_header_adv_dump(&aph);
p->flags |= aph.coding_type;
return 1;
}
else
{
gavl_log(GAVL_LOG_ERROR,
LOG_DOMAIN,
"Profiles other than advanced are not supported");
return 0;
}
break;
}
chunk_start = chunk_end;
}
return 0;
}
static void cleanup_vc1(bgav_video_parser_t * parser)
{
vc1_priv_t * priv = parser->priv;
if(priv->buf)
free(priv->buf);
free(priv);
}
static void reset_vc1(bgav_video_parser_t * parser)
{
vc1_priv_t * priv = parser->priv;
priv->state = STATE_SYNC;
priv->has_picture_start = 0;
}
void bgav_video_parser_init_vc1(bgav_video_parser_t * parser)
{
vc1_priv_t * priv;
priv = calloc(1, sizeof(*priv));
parser->priv = priv;
priv->state = STATE_SYNC;
// parser->parse = parse_vc1;
parser->parse_frame = parse_frame_vc1;
parser->cleanup = cleanup_vc1;
parser->reset = reset_vc1;
parser->find_frame_boundary = find_frame_boundary_vc1;
}
|