|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "avs3.h" |
|
#include "get_bits.h" |
|
#include "parser.h" |
|
|
|
static int avs3_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) |
|
{ |
|
int pic_found = pc->frame_start_found; |
|
uint32_t state = pc->state; |
|
int cur = 0; |
|
|
|
if (!pic_found) { |
|
for (; cur < buf_size; ++cur) { |
|
state = (state << 8) | buf[cur]; |
|
if (AVS3_ISPIC(buf[cur])){ |
|
cur++; |
|
pic_found = 1; |
|
break; |
|
} |
|
} |
|
} |
|
|
|
if (pic_found) { |
|
if (!buf_size) |
|
return END_NOT_FOUND; |
|
for (; cur < buf_size; ++cur) { |
|
state = (state << 8) | buf[cur]; |
|
if ((state & 0xFFFFFF00) == 0x100 && AVS3_ISUNIT(state & 0xFF)) { |
|
pc->frame_start_found = 0; |
|
pc->state = -1; |
|
return cur - 3; |
|
} |
|
} |
|
} |
|
|
|
pc->frame_start_found = pic_found; |
|
pc->state = state; |
|
|
|
return END_NOT_FOUND; |
|
} |
|
|
|
static void parse_avs3_nal_units(AVCodecParserContext *s, const uint8_t *buf, |
|
int buf_size, AVCodecContext *avctx) |
|
{ |
|
if (buf_size < 5) { |
|
return; |
|
} |
|
|
|
if (buf[0] == 0x0 && buf[1] == 0x0 && buf[2] == 0x1) { |
|
if (buf[3] == AVS3_SEQ_START_CODE) { |
|
GetBitContext gb; |
|
int profile, ratecode, low_delay; |
|
|
|
init_get_bits8(&gb, buf + 4, buf_size - 4); |
|
|
|
s->key_frame = 1; |
|
s->pict_type = AV_PICTURE_TYPE_I; |
|
|
|
profile = get_bits(&gb, 8); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
skip_bits(&gb, 47); |
|
|
|
if (profile == AVS3_PROFILE_BASELINE_MAIN10) { |
|
int sample_precision = get_bits(&gb, 3); |
|
if (sample_precision == 1) { |
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
|
} else if (sample_precision == 2) { |
|
avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE; |
|
} else { |
|
avctx->pix_fmt = AV_PIX_FMT_NONE; |
|
} |
|
} |
|
|
|
|
|
|
|
skip_bits(&gb, 5); |
|
|
|
ratecode = get_bits(&gb, 4); |
|
|
|
|
|
|
|
|
|
|
|
skip_bits(&gb, 32); |
|
|
|
low_delay = get_bits(&gb, 1); |
|
avctx->has_b_frames = FFMAX(avctx->has_b_frames, !low_delay); |
|
|
|
avctx->framerate.num = ff_avs3_frame_rate_tab[ratecode].num; |
|
avctx->framerate.den = ff_avs3_frame_rate_tab[ratecode].den; |
|
|
|
s->width = s->coded_width = avctx->width; |
|
s->height = s->coded_height = avctx->height; |
|
|
|
av_log(avctx, AV_LOG_DEBUG, |
|
"AVS3 parse seq HDR: profile %d; coded size: %dx%d; frame rate code: %d\n", |
|
profile, avctx->width, avctx->height, ratecode); |
|
|
|
} else if (buf[3] == AVS3_INTRA_PIC_START_CODE) { |
|
s->key_frame = 1; |
|
s->pict_type = AV_PICTURE_TYPE_I; |
|
} else if (buf[3] == AVS3_INTER_PIC_START_CODE){ |
|
s->key_frame = 0; |
|
if (buf_size > 9) { |
|
int pic_code_type = buf[8] & 0x3; |
|
if (pic_code_type == 1 || pic_code_type == 3) { |
|
s->pict_type = AV_PICTURE_TYPE_P; |
|
} else { |
|
s->pict_type = AV_PICTURE_TYPE_B; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
static int avs3_parse(AVCodecParserContext *s, AVCodecContext *avctx, |
|
const uint8_t **poutbuf, int *poutbuf_size, |
|
const uint8_t *buf, int buf_size) |
|
{ |
|
ParseContext *pc = s->priv_data; |
|
int next; |
|
|
|
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
|
next = buf_size; |
|
} else { |
|
next = avs3_find_frame_end(pc, buf, buf_size); |
|
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
|
*poutbuf = NULL; |
|
*poutbuf_size = 0; |
|
return buf_size; |
|
} |
|
} |
|
|
|
parse_avs3_nal_units(s, buf, buf_size, avctx); |
|
|
|
*poutbuf = buf; |
|
*poutbuf_size = buf_size; |
|
|
|
return next; |
|
} |
|
|
|
const AVCodecParser ff_avs3_parser = { |
|
.codec_ids = { AV_CODEC_ID_AVS3 }, |
|
.priv_data_size = sizeof(ParseContext), |
|
.parser_parse = avs3_parse, |
|
.parser_close = ff_parse_close, |
|
}; |
|
|