ffmpeg-cvslog
Threads by month
- ----- 2026 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
August 2015
- 3 participants
- 664 discussions
ffmpeg | branch: master | Donny Yang <work(a)kota.moe> | Wed Aug 19 06:41:23 2015 +0000| [51ca70322296f59666dda6c59bfa35ff9bf17dd5] | committer: Michael Niedermayer
apng: Support inter-frame compression
The current algorithm is just "try all the combinations, and pick the best".
It's not very fast either, probably due to a lot of copying, but will do for
an initial implementation.
Signed-off-by: Donny Yang <work(a)kota.moe>
Signed-off-by: Michael Niedermayer <michael(a)niedermayer.cc>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=51ca70322296f5966…
---
libavcodec/pngenc.c | 420 ++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 384 insertions(+), 36 deletions(-)
diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index af8ca4e..f6ad830 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -36,6 +36,14 @@
#define IOBUF_SIZE 4096
+typedef struct APNGFctlChunk {
+ uint32_t sequence_number;
+ uint32_t width, height;
+ uint32_t x_offset, y_offset;
+ uint16_t delay_num, delay_den;
+ uint8_t dispose_op, blend_op;
+} APNGFctlChunk;
+
typedef struct PNGEncContext {
AVClass *class;
HuffYUVEncDSPContext hdsp;
@@ -59,6 +67,12 @@ typedef struct PNGEncContext {
// APNG
uint32_t palette_checksum; // Used to ensure a single unique palette
uint32_t sequence_number;
+
+ AVFrame *prev_frame;
+ AVFrame *last_frame;
+ APNGFctlChunk last_frame_fctl;
+ uint8_t *last_frame_packet;
+ size_t last_frame_packet_size;
} PNGEncContext;
static void png_get_interlaced_row(uint8_t *dst, int row_size,
@@ -403,7 +417,7 @@ static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
uint8_t *progressive_buf = NULL;
uint8_t *top_buf = NULL;
- row_size = (avctx->width * s->bits_per_pixel + 7) >> 3;
+ row_size = (pict->width * s->bits_per_pixel + 7) >> 3;
crow_base = av_malloc((row_size + 32) << (s->filter_type == PNG_FILTER_VALUE_MIXED));
if (!crow_base) {
@@ -430,16 +444,16 @@ static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
for (pass = 0; pass < NB_PASSES; pass++) {
/* NOTE: a pass is completely omitted if no pixels would be
* output */
- pass_row_size = ff_png_pass_row_size(pass, s->bits_per_pixel, avctx->width);
+ pass_row_size = ff_png_pass_row_size(pass, s->bits_per_pixel, pict->width);
if (pass_row_size > 0) {
top = NULL;
- for (y = 0; y < avctx->height; y++)
+ for (y = 0; y < pict->height; y++)
if ((ff_png_pass_ymask[pass] << (y & 7)) & 0x80) {
ptr = p->data[0] + y * p->linesize[0];
FFSWAP(uint8_t *, progressive_buf, top_buf);
png_get_interlaced_row(progressive_buf, pass_row_size,
s->bits_per_pixel, pass,
- ptr, avctx->width);
+ ptr, pict->width);
crow = png_choose_filter(s, crow_buf, progressive_buf,
top, pass_row_size, s->bits_per_pixel >> 3);
png_write_row(avctx, crow, pass_row_size + 1);
@@ -449,7 +463,7 @@ static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
}
} else {
top = NULL;
- for (y = 0; y < avctx->height; y++) {
+ for (y = 0; y < pict->height; y++) {
ptr = p->data[0] + y * p->linesize[0];
crow = png_choose_filter(s, crow_buf, ptr, top,
row_size, s->bits_per_pixel >> 3);
@@ -530,6 +544,272 @@ static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
return 0;
}
+static int apng_do_inverse_blend(AVFrame *output, const AVFrame *input,
+ APNGFctlChunk *fctl_chunk, uint8_t bpp)
+{
+ // output: background, input: foreground
+ // output the image such that when blended with the background, will produce the foreground
+
+ unsigned int x, y;
+ unsigned int leftmost_x = input->width;
+ unsigned int rightmost_x = 0;
+ unsigned int topmost_y = input->height;
+ unsigned int bottommost_y = 0;
+ const uint8_t *input_data = input->data[0];
+ uint8_t *output_data = output->data[0];
+ ptrdiff_t input_linesize = input->linesize[0];
+ ptrdiff_t output_linesize = output->linesize[0];
+
+ // Find bounding box of changes
+ for (y = 0; y < input->height; ++y) {
+ for (x = 0; x < input->width; ++x) {
+ if (!memcmp(input_data + bpp * x, output_data + bpp * x, bpp))
+ continue;
+
+ if (x < leftmost_x)
+ leftmost_x = x;
+ if (x >= rightmost_x)
+ rightmost_x = x + 1;
+ if (y < topmost_y)
+ topmost_y = y;
+ if (y >= bottommost_y)
+ bottommost_y = y + 1;
+ }
+
+ input_data += input_linesize;
+ output_data += output_linesize;
+ }
+
+ if (leftmost_x == input->width && rightmost_x == 0) {
+ // Empty frame
+ // APNG does not support empty frames, so we make it a 1x1 frame
+ leftmost_x = topmost_y = 0;
+ rightmost_x = bottommost_y = 1;
+ }
+
+ // Do actual inverse blending
+ if (fctl_chunk->blend_op == APNG_BLEND_OP_SOURCE) {
+ output_data = output->data[0];
+ for (y = topmost_y; y < bottommost_y; ++y) {
+ memcpy(output_data,
+ input->data[0] + input_linesize * y + bpp * leftmost_x,
+ bpp * (rightmost_x - leftmost_x));
+ output_data += output_linesize;
+ }
+ } else { // APNG_BLEND_OP_OVER
+ size_t transparent_palette_index;
+ uint32_t *palette;
+
+ switch (input->format) {
+ case AV_PIX_FMT_RGBA64BE:
+ case AV_PIX_FMT_YA16BE:
+ case AV_PIX_FMT_RGBA:
+ case AV_PIX_FMT_GRAY8A:
+ break;
+
+ case AV_PIX_FMT_PAL8:
+ palette = (uint32_t*)input->data[1];
+ for (transparent_palette_index = 0; transparent_palette_index < 256; ++transparent_palette_index)
+ if (palette[transparent_palette_index] >> 24 == 0)
+ break;
+ break;
+
+ default:
+ // No alpha, so blending not possible
+ return -1;
+ }
+
+ for (y = topmost_y; y < bottommost_y; ++y) {
+ uint8_t *foreground = input->data[0] + input_linesize * y + bpp * leftmost_x;
+ uint8_t *background = output->data[0] + output_linesize * y + bpp * leftmost_x;
+ output_data = output->data[0] + output_linesize * (y - topmost_y);
+ for (x = leftmost_x; x < rightmost_x; ++x, foreground += bpp, background += bpp, output_data += bpp) {
+ if (!memcmp(foreground, background, bpp)) {
+ if (input->format == AV_PIX_FMT_PAL8) {
+ if (transparent_palette_index == 256) {
+ // Need fully transparent colour, but none exists
+ return -1;
+ }
+
+ *output_data = transparent_palette_index;
+ } else {
+ memset(output_data, 0, bpp);
+ }
+ continue;
+ }
+
+ // Check for special alpha values, since full inverse
+ // alpha-on-alpha blending is rarely possible, and when
+ // possible, doesn't compress much better than
+ // APNG_BLEND_OP_SOURCE blending
+ switch (input->format) {
+ case AV_PIX_FMT_RGBA64BE:
+ if (((uint16_t*)foreground)[3] == 0xffff ||
+ ((uint16_t*)background)[3] == 0)
+ break;
+ return -1;
+
+ case AV_PIX_FMT_YA16BE:
+ if (((uint16_t*)foreground)[1] == 0xffff ||
+ ((uint16_t*)background)[1] == 0)
+ break;
+ return -1;
+
+ case AV_PIX_FMT_RGBA:
+ if (foreground[3] == 0xff || background[3] == 0)
+ break;
+ return -1;
+
+ case AV_PIX_FMT_GRAY8A:
+ if (foreground[1] == 0xff || background[1] == 0)
+ break;
+ return -1;
+
+ case AV_PIX_FMT_PAL8:
+ if (palette[*foreground] >> 24 == 0xff ||
+ palette[*background] >> 24 == 0)
+ break;
+ return -1;
+ }
+
+ memmove(output_data, foreground, bpp);
+ }
+ }
+ }
+
+ output->width = rightmost_x - leftmost_x;
+ output->height = bottommost_y - topmost_y;
+ fctl_chunk->width = output->width;
+ fctl_chunk->height = output->height;
+ fctl_chunk->x_offset = leftmost_x;
+ fctl_chunk->y_offset = topmost_y;
+
+ return 0;
+}
+
+static int apng_encode_frame(AVCodecContext *avctx, const AVFrame *pict,
+ APNGFctlChunk *best_fctl_chunk, APNGFctlChunk *best_last_fctl_chunk)
+{
+ PNGEncContext *s = avctx->priv_data;
+ int ret;
+ unsigned int y;
+ AVFrame* diffFrame;
+ uint8_t bpp = (s->bits_per_pixel + 7) >> 3;
+ uint8_t *original_bytestream, *original_bytestream_end;
+ uint8_t *temp_bytestream = 0, *temp_bytestream_end;
+ uint32_t best_sequence_number;
+ uint8_t *best_bytestream;
+ size_t best_bytestream_size = SIZE_MAX;
+ APNGFctlChunk last_fctl_chunk = *best_last_fctl_chunk;
+ APNGFctlChunk fctl_chunk = *best_fctl_chunk;
+
+ if (avctx->frame_number == 0) {
+ best_fctl_chunk->width = pict->width;
+ best_fctl_chunk->height = pict->height;
+ best_fctl_chunk->x_offset = 0;
+ best_fctl_chunk->y_offset = 0;
+ best_fctl_chunk->blend_op = APNG_BLEND_OP_SOURCE;
+ return encode_frame(avctx, pict);
+ }
+
+ diffFrame = av_frame_alloc();
+ if (!diffFrame)
+ return AVERROR(ENOMEM);
+
+ diffFrame->format = pict->format;
+ diffFrame->width = pict->width;
+ diffFrame->height = pict->height;
+ if ((ret = av_frame_get_buffer(diffFrame, 32)) < 0)
+ goto fail;
+
+ original_bytestream = s->bytestream;
+ original_bytestream_end = s->bytestream_end;
+
+ temp_bytestream = av_malloc(original_bytestream_end - original_bytestream);
+ temp_bytestream_end = temp_bytestream + (original_bytestream_end - original_bytestream);
+ if (!temp_bytestream) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ for (last_fctl_chunk.dispose_op = 0; last_fctl_chunk.dispose_op < 3; ++last_fctl_chunk.dispose_op) {
+ // 0: APNG_DISPOSE_OP_NONE
+ // 1: APNG_DISPOSE_OP_BACKGROUND
+ // 2: APNG_DISPOSE_OP_PREVIOUS
+
+ for (fctl_chunk.blend_op = 0; fctl_chunk.blend_op < 2; ++fctl_chunk.blend_op) {
+ // 0: APNG_BLEND_OP_SOURCE
+ // 1: APNG_BLEND_OP_OVER
+
+ uint32_t original_sequence_number = s->sequence_number, sequence_number;
+ uint8_t *bytestream_start = s->bytestream;
+ size_t bytestream_size;
+
+ // Do disposal
+ if (last_fctl_chunk.dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
+ memcpy(diffFrame->data[0], s->last_frame->data[0],
+ s->last_frame->linesize[0] * s->last_frame->height);
+
+ if (last_fctl_chunk.dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
+ for (y = last_fctl_chunk.y_offset; y < last_fctl_chunk.y_offset + last_fctl_chunk.height; ++y) {
+ size_t row_start = s->last_frame->linesize[0] * y + bpp * last_fctl_chunk.x_offset;
+ memset(diffFrame->data[0] + row_start, 0, bpp * last_fctl_chunk.width);
+ }
+ }
+ } else {
+ if (!s->prev_frame)
+ continue;
+
+ memcpy(diffFrame->data[0], s->prev_frame->data[0],
+ s->prev_frame->linesize[0] * s->prev_frame->height);
+ }
+
+ // Do inverse blending
+ if (apng_do_inverse_blend(diffFrame, pict, &fctl_chunk, bpp) < 0)
+ continue;
+
+ // Do encoding
+ ret = encode_frame(avctx, diffFrame);
+ sequence_number = s->sequence_number;
+ s->sequence_number = original_sequence_number;
+ bytestream_size = s->bytestream - bytestream_start;
+ s->bytestream = bytestream_start;
+ if (ret < 0)
+ goto fail;
+
+ if (bytestream_size < best_bytestream_size) {
+ *best_fctl_chunk = fctl_chunk;
+ *best_last_fctl_chunk = last_fctl_chunk;
+
+ best_sequence_number = sequence_number;
+ best_bytestream = s->bytestream;
+ best_bytestream_size = bytestream_size;
+
+ if (best_bytestream == original_bytestream) {
+ s->bytestream = temp_bytestream;
+ s->bytestream_end = temp_bytestream_end;
+ } else {
+ s->bytestream = original_bytestream;
+ s->bytestream_end = original_bytestream_end;
+ }
+ }
+ }
+ }
+
+ s->sequence_number = best_sequence_number;
+ s->bytestream = original_bytestream + best_bytestream_size;
+ s->bytestream_end = original_bytestream_end;
+ if (best_bytestream != original_bytestream)
+ memcpy(original_bytestream, best_bytestream, best_bytestream_size);
+
+ ret = 0;
+
+fail:
+ av_freep(&temp_bytestream);
+ av_frame_free(&diffFrame);
+ return ret;
+}
+
static int encode_apng(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *pict, int *got_packet)
{
@@ -537,9 +817,9 @@ static int encode_apng(AVCodecContext *avctx, AVPacket *pkt,
int ret;
int enc_row_size;
size_t max_packet_size;
- uint8_t buf[26];
+ APNGFctlChunk fctl_chunk;
- if (avctx->codec_id == AV_CODEC_ID_APNG && s->color_type == PNG_COLOR_TYPE_PALETTE) {
+ if (pict && avctx->codec_id == AV_CODEC_ID_APNG && s->color_type == PNG_COLOR_TYPE_PALETTE) {
uint32_t checksum = ~av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), ~0U, pict->data[1], 256 * sizeof(uint32_t));
if (avctx->frame_number == 0) {
@@ -560,47 +840,111 @@ static int encode_apng(AVCodecContext *avctx, AVPacket *pkt,
);
if (max_packet_size > INT_MAX)
return AVERROR(ENOMEM);
- ret = ff_alloc_packet2(avctx, pkt, max_packet_size, 0);
- if (ret < 0)
- return ret;
-
- s->bytestream_start =
- s->bytestream = pkt->data;
- s->bytestream_end = pkt->data + pkt->size;
if (avctx->frame_number == 0) {
+ s->bytestream = avctx->extradata = av_malloc(FF_MIN_BUFFER_SIZE);
+ if (!avctx->extradata)
+ return AVERROR(ENOMEM);
+
ret = encode_headers(avctx, pict);
if (ret < 0)
return ret;
- avctx->extradata = av_malloc(s->bytestream - s->bytestream_start);
- if (!avctx->extradata)
+ avctx->extradata_size = s->bytestream - avctx->extradata;
+
+ s->last_frame_packet = av_malloc(max_packet_size);
+ if (!s->last_frame_packet)
return AVERROR(ENOMEM);
- avctx->extradata_size = s->bytestream - s->bytestream_start;
- memcpy(avctx->extradata, s->bytestream_start, s->bytestream - s->bytestream_start);
+ } else if (s->last_frame) {
+ ret = ff_alloc_packet2(avctx, pkt, max_packet_size, 0);
+ if (ret < 0)
+ return ret;
- s->bytestream = s->bytestream_start;
+ memcpy(pkt->data, s->last_frame_packet, s->last_frame_packet_size);
+ pkt->size = s->last_frame_packet_size;
+ pkt->pts = pkt->dts = s->last_frame->pts;
}
- AV_WB32(buf, s->sequence_number);
- AV_WB32(buf + 4, avctx->width);
- AV_WB32(buf + 8, avctx->height);
- AV_WB32(buf + 12, 0); // x offset
- AV_WB32(buf + 16, 0); // y offset
- AV_WB16(buf + 20, 0); // delay numerator (filled in during muxing)
- AV_WB16(buf + 22, 0); // delay denominator
- buf[24] = APNG_DISPOSE_OP_BACKGROUND;
- buf[25] = APNG_BLEND_OP_SOURCE;
- png_write_chunk(&s->bytestream, MKTAG('f', 'c', 'T', 'L'), buf, 26);
- ++s->sequence_number;
+ if (pict) {
+ s->bytestream_start =
+ s->bytestream = s->last_frame_packet;
+ s->bytestream_end = s->bytestream + max_packet_size;
- ret = encode_frame(avctx, pict);
- if (ret < 0)
- return ret;
+ // We're encoding the frame first, so we have to do a bit of shuffling around
+ // to have the image data write to the correct place in the buffer
+ fctl_chunk.sequence_number = s->sequence_number;
+ ++s->sequence_number;
+ s->bytestream += 26 + 12;
- pkt->size = s->bytestream - s->bytestream_start;
- pkt->flags |= AV_PKT_FLAG_KEY;
- *got_packet = 1;
+ ret = apng_encode_frame(avctx, pict, &fctl_chunk, &s->last_frame_fctl);
+ if (ret < 0)
+ return ret;
+
+ fctl_chunk.delay_num = 0; // delay filled in during muxing
+ fctl_chunk.delay_den = 0;
+ } else {
+ s->last_frame_fctl.dispose_op = APNG_DISPOSE_OP_NONE;
+ }
+
+ if (s->last_frame) {
+ uint8_t* last_fctl_chunk_start = pkt->data;
+ uint8_t buf[26];
+
+ AV_WB32(buf + 0, s->last_frame_fctl.sequence_number);
+ AV_WB32(buf + 4, s->last_frame_fctl.width);
+ AV_WB32(buf + 8, s->last_frame_fctl.height);
+ AV_WB32(buf + 12, s->last_frame_fctl.x_offset);
+ AV_WB32(buf + 16, s->last_frame_fctl.y_offset);
+ AV_WB16(buf + 20, s->last_frame_fctl.delay_num);
+ AV_WB16(buf + 22, s->last_frame_fctl.delay_den);
+ buf[24] = s->last_frame_fctl.dispose_op;
+ buf[25] = s->last_frame_fctl.blend_op;
+ png_write_chunk(&last_fctl_chunk_start, MKTAG('f', 'c', 'T', 'L'), buf, 26);
+
+ *got_packet = 1;
+ }
+
+ if (pict) {
+ if (!s->last_frame) {
+ s->last_frame = av_frame_alloc();
+ if (!s->last_frame)
+ return AVERROR(ENOMEM);
+ } else if (s->last_frame_fctl.dispose_op != APNG_DISPOSE_OP_PREVIOUS) {
+ if (!s->prev_frame) {
+ s->prev_frame = av_frame_alloc();
+ if (!s->prev_frame)
+ return AVERROR(ENOMEM);
+
+ s->prev_frame->format = pict->format;
+ s->prev_frame->width = pict->width;
+ s->prev_frame->height = pict->height;
+ if ((ret = av_frame_get_buffer(s->prev_frame, 32)) < 0)
+ return ret;
+ }
+
+ // Do disposal, but not blending
+ memcpy(s->prev_frame->data[0], s->last_frame->data[0],
+ s->last_frame->linesize[0] * s->last_frame->height);
+ if (s->last_frame_fctl.dispose_op == APNG_DISPOSE_OP_BACKGROUND) {
+ uint32_t y;
+ uint8_t bpp = (s->bits_per_pixel + 7) >> 3;
+ for (y = s->last_frame_fctl.y_offset; y < s->last_frame_fctl.y_offset + s->last_frame_fctl.height; ++y) {
+ size_t row_start = s->last_frame->linesize[0] * y + bpp * s->last_frame_fctl.x_offset;
+ memset(s->prev_frame->data[0] + row_start, 0, bpp * s->last_frame_fctl.width);
+ }
+ }
+ }
+
+ av_frame_unref(s->last_frame);
+ ret = av_frame_ref(s->last_frame, (AVFrame*)pict);
+ if (ret < 0)
+ return ret;
+
+ s->last_frame_fctl = fctl_chunk;
+ s->last_frame_packet_size = s->bytestream - s->bytestream_start;
+ } else {
+ av_frame_free(&s->last_frame);
+ }
return 0;
}
@@ -713,6 +1057,9 @@ static av_cold int png_enc_close(AVCodecContext *avctx)
PNGEncContext *s = avctx->priv_data;
deflateEnd(&s->zstream);
+ av_frame_free(&s->last_frame);
+ av_frame_free(&s->prev_frame);
+ av_freep(&s->last_frame_packet);
return 0;
}
@@ -768,6 +1115,7 @@ AVCodec ff_apng_encoder = {
.init = png_enc_init,
.close = png_enc_close,
.encode2 = encode_apng,
+ .capabilities = CODEC_CAP_DELAY,
.pix_fmts = (const enum AVPixelFormat[]) {
AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGBA64BE,
1
0
ffmpeg | branch: master | Michael Niedermayer <michael(a)niedermayer.cc> | Fri Aug 28 03:34:40 2015 +0200| [3322f0d4158fec271347d37f0b950ba3afceb02a] | committer: Michael Niedermayer
avcodec/dnxhddata: Fix inconsistent table entry
Fixes segfault
Fixes Ticket4809
Signed-off-by: Michael Niedermayer <michael(a)niedermayer.cc>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=3322f0d4158fec271…
---
libavcodec/dnxhddata.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libavcodec/dnxhddata.c b/libavcodec/dnxhddata.c
index 85e3829..9d2e4e8 100644
--- a/libavcodec/dnxhddata.c
+++ b/libavcodec/dnxhddata.c
@@ -1103,7 +1103,7 @@ const CIDEntry ff_dnxhd_cid_table[] = {
dnxhd_1237_ac_flags,
dnxhd_1237_run_codes, dnxhd_1237_run_bits, dnxhd_1237_run,
{ 63, 84, 100, 110 } },
- { 1260, 1440, 1080, 1, 417792, 417792, 4, 8, 3,
+ { 1260, 1440, 1080, 1, 835584, 417792, 4, 8, 3,
dnxhd_1260_luma_weight, dnxhd_1260_chroma_weight,
dnxhd_1237_dc_codes, dnxhd_1237_dc_bits,
dnxhd_1237_ac_codes, dnxhd_1237_ac_bits, dnxhd_1237_ac_level,
1
0
ffmpeg | branch: master | rogerdpack <rogerpack2005(a)gmail.com> | Thu Aug 27 16:16:44 2015 -0600| [832b4a4a438b73d720878d77aa3db46b08d79937] | committer: Timothy Gu
configure: Print out enabled programs
Better message that ffplay is not going to be built by printing out what
will be built.
Based on a patch by Moritz Barsnick.
Signed-off-by: rogerdpack <rogerpack2005(a)gmail.com>
Reviewed-by: Ganesh Ajjanagadde <gajjanag(a)mit.edu>
Signed-off-by: Timothy Gu <timothygu99(a)gmail.com>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=832b4a4a438b73d72…
---
configure | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/configure b/configure
index 8840027..5913090 100755
--- a/configure
+++ b/configure
@@ -5958,6 +5958,10 @@ test -n "$random_seed" &&
echo "random seed ${random_seed}"
echo
+echo "Enabled programs:"
+print_enabled '' $PROGRAM_LIST | print_3_columns
+echo
+
echo "External libraries:"
print_enabled '' $EXTERNAL_LIBRARY_LIST | print_3_columns
echo
1
0
ffmpeg | branch: master | Zhang Rui <bbcallen(a)gmail.com> | Thu Aug 27 14:00:30 2015 +0800| [92d378067ee3ed47fb9076e37d5fb7613c6aac5f] | committer: Michael Niedermayer
ffplay: remove unused include libavutil/colorspace.h
Signed-off-by: Michael Niedermayer <michael(a)niedermayer.cc>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=92d378067ee3ed47f…
---
ffplay.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/ffplay.c b/ffplay.c
index cde88db..d0e1822 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -31,7 +31,6 @@
#include <stdint.h>
#include "libavutil/avstring.h"
-#include "libavutil/colorspace.h"
#include "libavutil/eval.h"
#include "libavutil/mathematics.h"
#include "libavutil/pixdesc.h"
1
0
ffmpeg | branch: master | Michael Niedermayer <michael(a)niedermayer.cc> | Thu Aug 27 23:40:25 2015 +0200| [e2b19a533dbb4cb0fba85261ea835316381bc907] | committer: Michael Niedermayer
avformat/segment: atomically update list if possible
Fixes Ticket4802
Signed-off-by: Michael Niedermayer <michael(a)niedermayer.cc>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=e2b19a533dbb4cb0f…
---
libavformat/segment.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/libavformat/segment.c b/libavformat/segment.c
index 7f9367c..36417f2 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -114,6 +114,9 @@ typedef struct SegmentContext {
int reference_stream_index;
int break_non_keyframes;
+ int use_rename;
+ char temp_list_filename[1024];
+
SegmentListEntry cur_entry;
SegmentListEntry *segment_list_entries;
SegmentListEntry *segment_list_entries_end;
@@ -259,7 +262,8 @@ static int segment_list_open(AVFormatContext *s)
SegmentContext *seg = s->priv_data;
int ret;
- ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
+ snprintf(seg->temp_list_filename, sizeof(seg->temp_list_filename), seg->use_rename ? "%s.tmp" : "%s", seg->list);
+ ret = avio_open2(&seg->list_pb, seg->temp_list_filename, AVIO_FLAG_WRITE,
&s->interrupt_callback, NULL);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Failed to open segment list '%s'\n", seg->list);
@@ -370,6 +374,8 @@ static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
if (seg->list_type == LIST_TYPE_M3U8 && is_last)
avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
avio_closep(&seg->list_pb);
+ if (seg->use_rename)
+ ff_rename(seg->temp_list_filename, seg->list, s);
} else {
segment_list_print_entry(seg->list_pb, seg->list_type, &seg->cur_entry, s);
avio_flush(seg->list_pb);
@@ -646,9 +652,13 @@ static int seg_write_header(AVFormatContext *s)
else if (av_match_ext(seg->list, "ffcat,ffconcat")) seg->list_type = LIST_TYPE_FFCONCAT;
else seg->list_type = LIST_TYPE_FLAT;
}
- if (!seg->list_size && seg->list_type != LIST_TYPE_M3U8)
+ if (!seg->list_size && seg->list_type != LIST_TYPE_M3U8) {
if ((ret = segment_list_open(s)) < 0)
goto fail;
+ } else {
+ const char *proto = avio_find_protocol_name(s->filename);
+ seg->use_rename = proto && !strcmp(proto, "file");
+ }
}
if (seg->list_type == LIST_TYPE_EXT)
av_log(s, AV_LOG_WARNING, "'ext' list type option is deprecated in favor of 'csv'\n");
1
0
avfilter/af_amerge: use the name 's' for the pointer to the private context
by git@videolan.org 27 Aug '15
by git@videolan.org 27 Aug '15
27 Aug '15
ffmpeg | branch: master | Ganesh Ajjanagadde <gajjanagadde(a)gmail.com> | Wed Aug 26 12:53:44 2015 -0400| [9ba511d5d3d767d53e486587817d4a108bdd4f3b] | committer: Paul B Mahol
avfilter/af_amerge: use the name 's' for the pointer to the private context
Signed-off-by: Ganesh Ajjanagadde <gajjanagadde(a)gmail.com>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=9ba511d5d3d767d53…
---
libavfilter/af_amerge.c | 106 +++++++++++++++++++++++------------------------
1 file changed, 53 insertions(+), 53 deletions(-)
diff --git a/libavfilter/af_amerge.c b/libavfilter/af_amerge.c
index 62a11f7..9c3cfed 100644
--- a/libavfilter/af_amerge.c
+++ b/libavfilter/af_amerge.c
@@ -60,27 +60,27 @@ AVFILTER_DEFINE_CLASS(amerge);
static av_cold void uninit(AVFilterContext *ctx)
{
- AMergeContext *am = ctx->priv;
+ AMergeContext *s = ctx->priv;
int i;
- for (i = 0; i < am->nb_inputs; i++) {
- if (am->in)
- ff_bufqueue_discard_all(&am->in[i].queue);
+ for (i = 0; i < s->nb_inputs; i++) {
+ if (s->in)
+ ff_bufqueue_discard_all(&s->in[i].queue);
if (ctx->input_pads)
av_freep(&ctx->input_pads[i].name);
}
- av_freep(&am->in);
+ av_freep(&s->in);
}
static int query_formats(AVFilterContext *ctx)
{
- AMergeContext *am = ctx->priv;
+ AMergeContext *s = ctx->priv;
int64_t inlayout[SWR_CH_MAX], outlayout = 0;
AVFilterFormats *formats;
AVFilterChannelLayouts *layouts;
int i, overlap = 0, nb_ch = 0;
- for (i = 0; i < am->nb_inputs; i++) {
+ for (i = 0; i < s->nb_inputs; i++) {
if (!ctx->inputs[i]->in_channel_layouts ||
!ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
av_log(ctx, AV_LOG_WARNING,
@@ -93,11 +93,11 @@ static int query_formats(AVFilterContext *ctx)
av_get_channel_layout_string(buf, sizeof(buf), 0, inlayout[i]);
av_log(ctx, AV_LOG_INFO, "Using \"%s\" for input %d\n", buf, i + 1);
}
- am->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
+ s->in[i].nb_ch = av_get_channel_layout_nb_channels(inlayout[i]);
if (outlayout & inlayout[i])
overlap++;
outlayout |= inlayout[i];
- nb_ch += am->in[i].nb_ch;
+ nb_ch += s->in[i].nb_ch;
}
if (nb_ch > SWR_CH_MAX) {
av_log(ctx, AV_LOG_ERROR, "Too many channels (max %d)\n", SWR_CH_MAX);
@@ -108,7 +108,7 @@ static int query_formats(AVFilterContext *ctx)
"Input channel layouts overlap: "
"output layout will be determined by the number of distinct input channels\n");
for (i = 0; i < nb_ch; i++)
- am->route[i] = i;
+ s->route[i] = i;
outlayout = av_get_default_channel_layout(nb_ch);
if (!outlayout)
outlayout = ((int64_t)1 << nb_ch) - 1;
@@ -116,17 +116,17 @@ static int query_formats(AVFilterContext *ctx)
int *route[SWR_CH_MAX];
int c, out_ch_number = 0;
- route[0] = am->route;
- for (i = 1; i < am->nb_inputs; i++)
- route[i] = route[i - 1] + am->in[i - 1].nb_ch;
+ route[0] = s->route;
+ for (i = 1; i < s->nb_inputs; i++)
+ route[i] = route[i - 1] + s->in[i - 1].nb_ch;
for (c = 0; c < 64; c++)
- for (i = 0; i < am->nb_inputs; i++)
+ for (i = 0; i < s->nb_inputs; i++)
if ((inlayout[i] >> c) & 1)
*(route[i]++) = out_ch_number++;
}
formats = ff_make_format_list(ff_packed_sample_fmts_array);
ff_set_common_formats(ctx, formats);
- for (i = 0; i < am->nb_inputs; i++) {
+ for (i = 0; i < s->nb_inputs; i++) {
layouts = NULL;
ff_add_channel_layout(&layouts, inlayout[i]);
ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
@@ -141,11 +141,11 @@ static int query_formats(AVFilterContext *ctx)
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
- AMergeContext *am = ctx->priv;
+ AMergeContext *s = ctx->priv;
AVBPrint bp;
int i;
- for (i = 1; i < am->nb_inputs; i++) {
+ for (i = 1; i < s->nb_inputs; i++) {
if (ctx->inputs[i]->sample_rate != ctx->inputs[0]->sample_rate) {
av_log(ctx, AV_LOG_ERROR,
"Inputs must have the same sample rate "
@@ -154,12 +154,12 @@ static int config_output(AVFilterLink *outlink)
return AVERROR(EINVAL);
}
}
- am->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
+ s->bps = av_get_bytes_per_sample(ctx->outputs[0]->format);
outlink->sample_rate = ctx->inputs[0]->sample_rate;
outlink->time_base = ctx->inputs[0]->time_base;
av_bprint_init(&bp, 0, 1);
- for (i = 0; i < am->nb_inputs; i++) {
+ for (i = 0; i < s->nb_inputs; i++) {
av_bprintf(&bp, "%sin%d:", i ? " + " : "", i);
av_bprint_channel_layout(&bp, -1, ctx->inputs[i]->channel_layout);
}
@@ -173,11 +173,11 @@ static int config_output(AVFilterLink *outlink)
static int request_frame(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
- AMergeContext *am = ctx->priv;
+ AMergeContext *s = ctx->priv;
int i, ret;
- for (i = 0; i < am->nb_inputs; i++)
- if (!am->in[i].nb_samples)
+ for (i = 0; i < s->nb_inputs; i++)
+ if (!s->in[i].nb_samples)
if ((ret = ff_request_frame(ctx->inputs[i])) < 0)
return ret;
return 0;
@@ -223,27 +223,27 @@ static inline void copy_samples(int nb_inputs, struct amerge_input in[],
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
{
AVFilterContext *ctx = inlink->dst;
- AMergeContext *am = ctx->priv;
+ AMergeContext *s = ctx->priv;
AVFilterLink *const outlink = ctx->outputs[0];
int input_number;
int nb_samples, ns, i;
AVFrame *outbuf, *inbuf[SWR_CH_MAX];
uint8_t *ins[SWR_CH_MAX], *outs;
- for (input_number = 0; input_number < am->nb_inputs; input_number++)
+ for (input_number = 0; input_number < s->nb_inputs; input_number++)
if (inlink == ctx->inputs[input_number])
break;
- av_assert1(input_number < am->nb_inputs);
- if (ff_bufqueue_is_full(&am->in[input_number].queue)) {
+ av_assert1(input_number < s->nb_inputs);
+ if (ff_bufqueue_is_full(&s->in[input_number].queue)) {
av_frame_free(&insamples);
return AVERROR(ENOMEM);
}
- ff_bufqueue_add(ctx, &am->in[input_number].queue, av_frame_clone(insamples));
- am->in[input_number].nb_samples += insamples->nb_samples;
+ ff_bufqueue_add(ctx, &s->in[input_number].queue, av_frame_clone(insamples));
+ s->in[input_number].nb_samples += insamples->nb_samples;
av_frame_free(&insamples);
- nb_samples = am->in[0].nb_samples;
- for (i = 1; i < am->nb_inputs; i++)
- nb_samples = FFMIN(nb_samples, am->in[i].nb_samples);
+ nb_samples = s->in[0].nb_samples;
+ for (i = 1; i < s->nb_inputs; i++)
+ nb_samples = FFMIN(nb_samples, s->in[i].nb_samples);
if (!nb_samples)
return 0;
@@ -251,15 +251,15 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
if (!outbuf)
return AVERROR(ENOMEM);
outs = outbuf->data[0];
- for (i = 0; i < am->nb_inputs; i++) {
- inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
+ for (i = 0; i < s->nb_inputs; i++) {
+ inbuf[i] = ff_bufqueue_peek(&s->in[i].queue, 0);
ins[i] = inbuf[i]->data[0] +
- am->in[i].pos * am->in[i].nb_ch * am->bps;
+ s->in[i].pos * s->in[i].nb_ch * s->bps;
}
av_frame_copy_props(outbuf, inbuf[0]);
outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
inbuf[0]->pts +
- av_rescale_q(am->in[0].pos,
+ av_rescale_q(s->in[0].pos,
av_make_q(1, ctx->inputs[0]->sample_rate),
ctx->outputs[0]->time_base);
@@ -269,34 +269,34 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
while (nb_samples) {
ns = nb_samples;
- for (i = 0; i < am->nb_inputs; i++)
- ns = FFMIN(ns, inbuf[i]->nb_samples - am->in[i].pos);
+ for (i = 0; i < s->nb_inputs; i++)
+ ns = FFMIN(ns, inbuf[i]->nb_samples - s->in[i].pos);
/* Unroll the most common sample formats: speed +~350% for the loop,
+~13% overall (including two common decoders) */
- switch (am->bps) {
+ switch (s->bps) {
case 1:
- copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 1);
+ copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, 1);
break;
case 2:
- copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 2);
+ copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, 2);
break;
case 4:
- copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, 4);
+ copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, 4);
break;
default:
- copy_samples(am->nb_inputs, am->in, am->route, ins, &outs, ns, am->bps);
+ copy_samples(s->nb_inputs, s->in, s->route, ins, &outs, ns, s->bps);
break;
}
nb_samples -= ns;
- for (i = 0; i < am->nb_inputs; i++) {
- am->in[i].nb_samples -= ns;
- am->in[i].pos += ns;
- if (am->in[i].pos == inbuf[i]->nb_samples) {
- am->in[i].pos = 0;
+ for (i = 0; i < s->nb_inputs; i++) {
+ s->in[i].nb_samples -= ns;
+ s->in[i].pos += ns;
+ if (s->in[i].pos == inbuf[i]->nb_samples) {
+ s->in[i].pos = 0;
av_frame_free(&inbuf[i]);
- ff_bufqueue_get(&am->in[i].queue);
- inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
+ ff_bufqueue_get(&s->in[i].queue);
+ inbuf[i] = ff_bufqueue_peek(&s->in[i].queue, 0);
ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
}
}
@@ -306,13 +306,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
static av_cold int init(AVFilterContext *ctx)
{
- AMergeContext *am = ctx->priv;
+ AMergeContext *s = ctx->priv;
int i;
- am->in = av_calloc(am->nb_inputs, sizeof(*am->in));
- if (!am->in)
+ s->in = av_calloc(s->nb_inputs, sizeof(*s->in));
+ if (!s->in)
return AVERROR(ENOMEM);
- for (i = 0; i < am->nb_inputs; i++) {
+ for (i = 0; i < s->nb_inputs; i++) {
char *name = av_asprintf("in%d", i);
AVFilterPad pad = {
.name = name,
1
0
ffmpeg | branch: master | Paul B Mahol <onemda(a)gmail.com> | Fri Jul 24 07:09:16 2015 +0000| [15f4b3db58a6d325d4b894277cd5f1502b54285f] | committer: Paul B Mahol
avfilter: add framerate video filter
Signed-off-by: Paul B Mahol <onemda(a)gmail.com>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=15f4b3db58a6d325d…
---
Changelog | 1 +
doc/filters.texi | 45 ++++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/version.h | 2 +-
libavfilter/vf_framerate.c | 592 ++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 641 insertions(+), 1 deletion(-)
diff --git a/Changelog b/Changelog
index 99aae00..5911761 100644
--- a/Changelog
+++ b/Changelog
@@ -37,6 +37,7 @@ version <next>:
- hstack and vstack filter
- Support DNx100 (1440x1080@8)
- VAAPI hevc hwaccel
+- framerate filter
version 2.7:
diff --git a/doc/filters.texi b/doc/filters.texi
index 178252f..5bef4b1 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -6015,6 +6015,51 @@ ffmpeg -i LEFT -i RIGHT -filter_complex framepack=frameseq OUTPUT
ffmpeg -i LEFT -i RIGHT -filter_complex [0:v]scale=w=iw/2[left],[1:v]scale=w=iw/2[right],[left][right]framepack=sbs OUTPUT
@end example
+@section framerate
+
+Change the frame rate by interpolating new video output frames from the source
+frames.
+
+This filter is not designed to function correctly with interlaced media. If
+you wish to change the frame rate of interlaced media then you are required
+to deinterlace before this filter and re-interlace after this filter.
+
+A description of the accepted options follows.
+
+@table @option
+@item fps
+Specify the output frames per second. This option can also be specified
+as a value alone. The default is @code{50}.
+
+@item interp_start
+Specify the start of a range where the output frame will be created as a
+linear interpolation of two frames. The range is [@code{0}-@code{255}],
+the default is @code{15}.
+
+@item interp_end
+Specify the end of a range where the output frame will be created as a
+linear interpolation of two frames. The range is [@code{0}-@code{255}],
+the default is @code{240}.
+
+@item scene
+Specify the level at which a scene change is detected as a value between
+0 and 100 to indicate a new scene; a low value reflects a low
+probability for the current frame to introduce a new scene, while a higher
+value means the current frame is more likely to be one.
+The default is @code{7}.
+
+@item flags
+Specify flags influencing the filter process.
+
+Available value for @var{flags} is:
+
+@table @option
+@item scene_change_detect, scd
+Enable scene change detection using the value of the option @var{scene}.
+This flag is enabled by default.
+@end table
+@end table
+
@section framestep
Select one frame every N-th frame.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 58f990a..ac7412d 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -142,6 +142,7 @@ OBJS-$(CONFIG_FIND_RECT_FILTER) += vf_find_rect.o lavfutils.o
OBJS-$(CONFIG_FORMAT_FILTER) += vf_format.o
OBJS-$(CONFIG_FPS_FILTER) += vf_fps.o
OBJS-$(CONFIG_FRAMEPACK_FILTER) += vf_framepack.o
+OBJS-$(CONFIG_FRAMERATE_FILTER) += vf_framerate.o
OBJS-$(CONFIG_FRAMESTEP_FILTER) += vf_framestep.o
OBJS-$(CONFIG_FREI0R_FILTER) += vf_frei0r.o
OBJS-$(CONFIG_FSPP_FILTER) += vf_fspp.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 0a1bdfd..9068e83 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -158,6 +158,7 @@ void avfilter_register_all(void)
REGISTER_FILTER(FORMAT, format, vf);
REGISTER_FILTER(FPS, fps, vf);
REGISTER_FILTER(FRAMEPACK, framepack, vf);
+ REGISTER_FILTER(FRAMERATE, framerate, vf);
REGISTER_FILTER(FRAMESTEP, framestep, vf);
REGISTER_FILTER(FREI0R, frei0r, vf);
REGISTER_FILTER(FSPP, fspp, vf);
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 6b836d3..15eb540 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,7 +30,7 @@
#include "libavutil/version.h"
#define LIBAVFILTER_VERSION_MAJOR 5
-#define LIBAVFILTER_VERSION_MINOR 38
+#define LIBAVFILTER_VERSION_MINOR 39
#define LIBAVFILTER_VERSION_MICRO 100
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
diff --git a/libavfilter/vf_framerate.c b/libavfilter/vf_framerate.c
new file mode 100644
index 0000000..5c9da19
--- /dev/null
+++ b/libavfilter/vf_framerate.c
@@ -0,0 +1,592 @@
+/*
+ * Copyright (C) 2012 Mark Himsley
+ *
+ * get_scene_score() Copyright (c) 2011 Stefano Sabatini
+ * taken from libavfilter/vf_select.c
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * filter for upsampling or downsampling a progressive source
+ */
+
+#define DEBUG
+
+#include "libavutil/avassert.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/internal.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/pixelutils.h"
+
+#include "avfilter.h"
+#include "internal.h"
+#include "video.h"
+
+#define N_SRCE 3
+
+typedef struct FrameRateContext {
+ const AVClass *class;
+ // parameters
+ AVRational dest_frame_rate; ///< output frames per second
+ int flags; ///< flags affecting frame rate conversion algorithm
+ double scene_score; ///< score that denotes a scene change has happened
+ int interp_start; ///< start of range to apply linear interpolation
+ int interp_end; ///< end of range to apply linear interpolation
+
+ int line_size[4]; ///< bytes of pixel data per line for each plane
+ int vsub;
+
+ int frst, next, prev, crnt, last;
+ int pending_srce_frames; ///< how many input frames are still waiting to be processed
+ int flush; ///< are we flushing final frames
+ int pending_end_frame; ///< flag indicating we are waiting to call filter_frame()
+
+ AVRational srce_time_base; ///< timebase of source
+
+ AVRational dest_time_base; ///< timebase of destination
+ int32_t dest_frame_num;
+ int64_t last_dest_frame_pts; ///< pts of the last frame output
+ int64_t average_srce_pts_dest_delta;///< average input pts delta converted from input rate to output rate
+ int64_t average_dest_pts_delta; ///< calculated average output pts delta
+
+ av_pixelutils_sad_fn sad; ///< Sum of the absolute difference function (scene detect only)
+ double prev_mafd; ///< previous MAFD (scene detect only)
+
+ AVFrame *srce[N_SRCE]; ///< buffered source frames
+ int64_t srce_pts_dest[N_SRCE]; ///< pts for source frames scaled to output timebase
+ int64_t pts; ///< pts of frame we are working on
+} FrameRateContext;
+
+#define OFFSET(x) offsetof(FrameRateContext, x)
+#define V AV_OPT_FLAG_VIDEO_PARAM
+#define F AV_OPT_FLAG_FILTERING_PARAM
+#define FRAMERATE_FLAG_SCD 01
+
+static const AVOption framerate_options[] = {
+ {"fps", "required output frames per second rate", OFFSET(dest_frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="50"}, 0, INT_MAX, V|F },
+
+ {"interp_start", "point to start linear interpolation", OFFSET(interp_start), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, V|F },
+ {"interp_end", "point to end linear interpolation", OFFSET(interp_end), AV_OPT_TYPE_INT, {.i64=240}, 0, 255, V|F },
+ {"scene", "scene change level", OFFSET(scene_score), AV_OPT_TYPE_DOUBLE, {.dbl=7.0}, 0, INT_MAX, V|F },
+
+ {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 0, INT_MAX, V|F, "flags" },
+ {"scene_change_detect", "enable scene change detection", 0, AV_OPT_TYPE_CONST, {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
+ {"scd", "enable scene change detection", 0, AV_OPT_TYPE_CONST, {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
+
+ {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(framerate);
+
+static void next_source(AVFilterContext *ctx)
+{
+ FrameRateContext *s = ctx->priv;
+ int i;
+
+ ff_dlog(ctx, "next_source()\n");
+
+ if (s->srce[s->last] && s->srce[s->last] != s->srce[s->last-1]) {
+ ff_dlog(ctx, "next_source() unlink %d\n", s->last);
+ av_frame_free(&s->srce[s->last]);
+ }
+ for (i = s->last; i > s->frst; i--) {
+ ff_dlog(ctx, "next_source() copy %d to %d\n", i - 1, i);
+ s->srce[i] = s->srce[i - 1];
+ }
+ ff_dlog(ctx, "next_source() make %d null\n", s->frst);
+ s->srce[s->frst] = NULL;
+}
+
+static double get_scene_score(AVFilterContext *ctx, AVFrame *crnt, AVFrame *next)
+{
+ FrameRateContext *s = ctx->priv;
+ double ret = 0;
+
+ ff_dlog(ctx, "get_scene_score()\n");
+
+ if (crnt &&
+ crnt->height == next->height &&
+ crnt->width == next->width) {
+ int x, y;
+ int64_t sad;
+ double mafd, diff;
+ uint8_t *p1 = crnt->data[0];
+ uint8_t *p2 = next->data[0];
+ const int p1_linesize = crnt->linesize[0];
+ const int p2_linesize = next->linesize[0];
+
+ ff_dlog(ctx, "get_scene_score() process\n");
+
+ for (sad = y = 0; y < crnt->height; y += 8) {
+ for (x = 0; x < p1_linesize; x += 8) {
+ sad += s->sad(p1 + y * p1_linesize + x,
+ p1_linesize,
+ p2 + y * p2_linesize + x,
+ p2_linesize);
+ }
+ }
+ emms_c();
+ mafd = sad / (crnt->height * crnt->width * 3);
+ diff = fabs(mafd - s->prev_mafd);
+ ret = av_clipf(FFMIN(mafd, diff), 0, 100.0);
+ s->prev_mafd = mafd;
+ }
+ ff_dlog(ctx, "get_scene_score() result is:%f\n", ret);
+ return ret;
+}
+
+static int process_work_frame(AVFilterContext *ctx, int stop)
+{
+ FrameRateContext *s = ctx->priv;
+ AVFilterLink *outlink = ctx->outputs[0];
+ int64_t work_next_pts;
+ AVFrame *copy_src1, *copy_src2, *work;
+ int interpolate;
+
+ ff_dlog(ctx, "process_work_frame()\n");
+
+ ff_dlog(ctx, "process_work_frame() pending_input_frames %d\n", s->pending_srce_frames);
+
+ if (s->srce[s->prev]) ff_dlog(ctx, "process_work_frame() srce prev pts:%"PRId64"\n", s->srce[s->prev]->pts);
+ if (s->srce[s->crnt]) ff_dlog(ctx, "process_work_frame() srce crnt pts:%"PRId64"\n", s->srce[s->crnt]->pts);
+ if (s->srce[s->next]) ff_dlog(ctx, "process_work_frame() srce next pts:%"PRId64"\n", s->srce[s->next]->pts);
+
+ if (!s->srce[s->crnt]) {
+ // the filter cannot do anything
+ ff_dlog(ctx, "process_work_frame() no current frame cached: move on to next frame, do not output a frame\n");
+ next_source(ctx);
+ return 0;
+ }
+
+ work_next_pts = s->pts + s->average_dest_pts_delta;
+
+ ff_dlog(ctx, "process_work_frame() work crnt pts:%"PRId64"\n", s->pts);
+ ff_dlog(ctx, "process_work_frame() work next pts:%"PRId64"\n", work_next_pts);
+ if (s->srce[s->prev])
+ ff_dlog(ctx, "process_work_frame() srce prev pts:%"PRId64" at dest time base:%u/%u\n",
+ s->srce_pts_dest[s->prev], s->dest_time_base.num, s->dest_time_base.den);
+ if (s->srce[s->crnt])
+ ff_dlog(ctx, "process_work_frame() srce crnt pts:%"PRId64" at dest time base:%u/%u\n",
+ s->srce_pts_dest[s->crnt], s->dest_time_base.num, s->dest_time_base.den);
+ if (s->srce[s->next])
+ ff_dlog(ctx, "process_work_frame() srce next pts:%"PRId64" at dest time base:%u/%u\n",
+ s->srce_pts_dest[s->next], s->dest_time_base.num, s->dest_time_base.den);
+
+ av_assert0(s->srce[s->next]);
+
+ // should filter be skipping input frame (output frame rate is lower than input frame rate)
+ if (!s->flush && s->pts >= s->srce_pts_dest[s->next]) {
+ ff_dlog(ctx, "process_work_frame() work crnt pts >= srce next pts: SKIP FRAME, move on to next frame, do not output a frame\n");
+ next_source(ctx);
+ s->pending_srce_frames--;
+ return 0;
+ }
+
+ // calculate interpolation
+ interpolate = (int) ((s->pts - s->srce_pts_dest[s->crnt]) * 256.0 / s->average_srce_pts_dest_delta);
+ ff_dlog(ctx, "process_work_frame() interpolate:%d/256\n", interpolate);
+ copy_src1 = s->srce[s->crnt];
+ if (interpolate > s->interp_end) {
+ ff_dlog(ctx, "process_work_frame() source is:NEXT\n");
+ copy_src1 = s->srce[s->next];
+ }
+ if (s->srce[s->prev] && interpolate < -s->interp_end) {
+ ff_dlog(ctx, "process_work_frame() source is:PREV\n");
+ copy_src1 = s->srce[s->prev];
+ }
+
+ // decide whether to blend two frames
+ if ((interpolate >= s->interp_start && interpolate <= s->interp_end) || (interpolate <= -s->interp_start && interpolate >= -s->interp_end)) {
+ double interpolate_scene_score = 0;
+
+ if (interpolate > 0) {
+ ff_dlog(ctx, "process_work_frame() interpolate source is:NEXT\n");
+ copy_src2 = s->srce[s->next];
+ } else {
+ ff_dlog(ctx, "process_work_frame() interpolate source is:PREV\n");
+ copy_src2 = s->srce[s->prev];
+ }
+ if ((s->flags & FRAMERATE_FLAG_SCD) && copy_src2) {
+ interpolate_scene_score = get_scene_score(ctx, copy_src1, copy_src2);
+ ff_dlog(ctx, "process_work_frame() interpolate scene score:%f\n", interpolate_scene_score);
+ }
+ // decide if the shot-change detection allows us to blend two frames
+ if (interpolate_scene_score < s->scene_score && copy_src2) {
+ uint16_t src2_factor = abs(interpolate);
+ uint16_t src1_factor = 256 - src2_factor;
+ int plane, line, pixel;
+
+ // get work-space for output frame
+ work = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+ if (!work)
+ return AVERROR(ENOMEM);
+
+ av_frame_copy_props(work, s->srce[s->crnt]);
+
+ ff_dlog(ctx, "process_work_frame() INTERPOLATE to create work frame\n");
+ for (plane = 0; plane < 4 && copy_src1->data[plane] && copy_src2->data[plane]; plane++) {
+ int cpy_line_width = s->line_size[plane];
+ uint8_t *cpy_src1_data = copy_src1->data[plane];
+ int cpy_src1_line_size = copy_src1->linesize[plane];
+ uint8_t *cpy_src2_data = copy_src2->data[plane];
+ int cpy_src2_line_size = copy_src2->linesize[plane];
+ int cpy_src_h = (plane > 0 && plane < 3) ? (copy_src1->height >> s->vsub) : (copy_src1->height);
+ uint8_t *cpy_dst_data = work->data[plane];
+ int cpy_dst_line_size = work->linesize[plane];
+ if (plane <1 || plane >2) {
+ // luma or alpha
+ for (line = 0; line < cpy_src_h; line++) {
+ for (pixel = 0; pixel < cpy_line_width; pixel++) {
+ // integer version of (src1 * src1_factor) + (src2 + src2_factor) + 0.5
+ // 0.5 is for rounding
+ // 128 is the integer representation of 0.5 << 8
+ cpy_dst_data[pixel] = ((cpy_src1_data[pixel] * src1_factor) + (cpy_src2_data[pixel] * src2_factor) + 128) >> 8;
+ }
+ cpy_src1_data += cpy_src1_line_size;
+ cpy_src2_data += cpy_src2_line_size;
+ cpy_dst_data += cpy_dst_line_size;
+ }
+ } else {
+ // chroma
+ for (line = 0; line < cpy_src_h; line++) {
+ for (pixel = 0; pixel < cpy_line_width; pixel++) {
+ // as above
+ // because U and V are based around 128 we have to subtract 128 from the components.
+ // 32896 is the integer representation of 128.5 << 8
+ cpy_dst_data[pixel] = (((cpy_src1_data[pixel] - 128) * src1_factor) + ((cpy_src2_data[pixel] - 128) * src2_factor) + 32896) >> 8;
+ }
+ cpy_src1_data += cpy_src1_line_size;
+ cpy_src2_data += cpy_src2_line_size;
+ cpy_dst_data += cpy_dst_line_size;
+ }
+ }
+ }
+ goto copy_done;
+ }
+ else {
+ ff_dlog(ctx, "process_work_frame() CUT - DON'T INTERPOLATE\n");
+ }
+ }
+
+ ff_dlog(ctx, "process_work_frame() COPY to the work frame\n");
+ // copy the frame we decided is our base source
+ work = av_frame_clone(copy_src1);
+ if (!work)
+ return AVERROR(ENOMEM);
+
+copy_done:
+ work->pts = s->pts;
+
+ // should filter be re-using input frame (output frame rate is higher than input frame rate)
+ if (!s->flush && (work_next_pts + s->average_dest_pts_delta) < (s->srce_pts_dest[s->crnt] + s->average_srce_pts_dest_delta)) {
+ ff_dlog(ctx, "process_work_frame() REPEAT FRAME\n");
+ } else {
+ ff_dlog(ctx, "process_work_frame() CONSUME FRAME, move to next frame\n");
+ s->pending_srce_frames--;
+ next_source(ctx);
+ }
+ ff_dlog(ctx, "process_work_frame() output a frame\n");
+ s->dest_frame_num++;
+ if (stop)
+ s->pending_end_frame = 0;
+ s->last_dest_frame_pts = work->pts;
+
+ return ff_filter_frame(ctx->outputs[0], work);
+}
+
+static void set_srce_frame_dest_pts(AVFilterContext *ctx)
+{
+ FrameRateContext *s = ctx->priv;
+
+ ff_dlog(ctx, "set_srce_frame_output_pts()\n");
+
+ // scale the input pts from the timebase difference between input and output
+ if (s->srce[s->prev])
+ s->srce_pts_dest[s->prev] = av_rescale_q(s->srce[s->prev]->pts, s->srce_time_base, s->dest_time_base);
+ if (s->srce[s->crnt])
+ s->srce_pts_dest[s->crnt] = av_rescale_q(s->srce[s->crnt]->pts, s->srce_time_base, s->dest_time_base);
+ if (s->srce[s->next])
+ s->srce_pts_dest[s->next] = av_rescale_q(s->srce[s->next]->pts, s->srce_time_base, s->dest_time_base);
+}
+
+static void set_work_frame_pts(AVFilterContext *ctx)
+{
+ FrameRateContext *s = ctx->priv;
+ int64_t pts, average_srce_pts_delta = 0;
+
+ ff_dlog(ctx, "set_work_frame_pts()\n");
+
+ av_assert0(s->srce[s->next]);
+ av_assert0(s->srce[s->crnt]);
+
+ ff_dlog(ctx, "set_work_frame_pts() srce crnt pts:%"PRId64"\n", s->srce[s->crnt]->pts);
+ ff_dlog(ctx, "set_work_frame_pts() srce next pts:%"PRId64"\n", s->srce[s->next]->pts);
+ if (s->srce[s->prev])
+ ff_dlog(ctx, "set_work_frame_pts() srce prev pts:%"PRId64"\n", s->srce[s->prev]->pts);
+
+ average_srce_pts_delta = s->average_srce_pts_dest_delta;
+ ff_dlog(ctx, "set_work_frame_pts() initial average srce pts:%"PRId64"\n", average_srce_pts_delta);
+
+ // calculate the PTS delta
+ if ((pts = (s->srce[s->next]->pts - s->srce[s->crnt]->pts))) {
+ average_srce_pts_delta = average_srce_pts_delta?((average_srce_pts_delta+pts)>>1):pts;
+ } else if (s->srce[s->prev] && (pts = (s->srce[s->crnt]->pts - s->srce[s->prev]->pts))) {
+ average_srce_pts_delta = average_srce_pts_delta?((average_srce_pts_delta+pts)>>1):pts;
+ }
+
+ s->average_srce_pts_dest_delta = av_rescale_q(average_srce_pts_delta, s->srce_time_base, s->dest_time_base);
+ ff_dlog(ctx, "set_work_frame_pts() average srce pts:%"PRId64"\n", average_srce_pts_delta);
+ ff_dlog(ctx, "set_work_frame_pts() average srce pts:%"PRId64" at dest time base:%u/%u\n",
+ s->average_srce_pts_dest_delta, s->dest_time_base.num, s->dest_time_base.den);
+
+ set_srce_frame_dest_pts(ctx);
+
+ if (ctx->inputs[0] && !s->average_dest_pts_delta) {
+ int64_t d = av_q2d(av_inv_q(av_mul_q(s->srce_time_base, s->dest_frame_rate)));
+ if (d == 0) { // FIXME
+ av_log(ctx, AV_LOG_WARNING, "Buggy path reached, use settb filter before this filter!\n");
+ d = av_q2d(av_mul_q(ctx->inputs[0]->time_base, s->dest_frame_rate));
+ }
+ s->average_dest_pts_delta = av_rescale_q(d, s->srce_time_base, s->dest_time_base);
+ ff_dlog(ctx, "set_frame_pts() average output pts from input timebase\n");
+ ff_dlog(ctx, "set_work_frame_pts() average dest pts delta:%"PRId64"\n", s->average_dest_pts_delta);
+ }
+
+ if (!s->dest_frame_num) {
+ s->pts = s->last_dest_frame_pts = s->srce_pts_dest[s->crnt];
+ } else {
+ s->pts = s->last_dest_frame_pts + s->average_dest_pts_delta;
+ }
+
+ ff_dlog(ctx, "set_work_frame_pts() calculated pts:%"PRId64" at dest time base:%u/%u\n",
+ s->pts, s->dest_time_base.num, s->dest_time_base.den);
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+ FrameRateContext *s = ctx->priv;
+
+ s->dest_frame_num = 0;
+
+ s->crnt = (N_SRCE)>>1;
+ s->last = N_SRCE - 1;
+
+ s->next = s->crnt - 1;
+ s->prev = s->crnt + 1;
+
+ return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ FrameRateContext *s = ctx->priv;
+ int i;
+
+ for (i = s->frst + 1; i > s->last; i++) {
+ if (s->srce[i] && (s->srce[i] != s->srce[i + 1]))
+ av_frame_free(&s->srce[i]);
+ }
+ av_frame_free(&s->srce[s->last]);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ static const enum AVPixelFormat pix_fmts[] = {
+ AV_PIX_FMT_YUV410P,
+ AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P,
+ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
+ AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
+ AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
+ AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
+ AV_PIX_FMT_NONE
+ };
+
+ AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+ if (!fmts_list)
+ return AVERROR(ENOMEM);
+ return ff_set_common_formats(ctx, fmts_list);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ FrameRateContext *s = ctx->priv;
+ const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
+ int plane;
+
+ for (plane = 0; plane < 4; plane++) {
+ s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
+ plane);
+ }
+
+ s->vsub = pix_desc->log2_chroma_h;
+
+ s->sad = av_pixelutils_get_sad_fn(3, 3, 2, s); // 8x8 both sources aligned
+ if (!s->sad)
+ return AVERROR(EINVAL);
+
+ s->srce_time_base = inlink->time_base;
+
+ return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
+{
+ AVFilterContext *ctx = inlink->dst;
+ FrameRateContext *s = ctx->priv;
+
+ // we have one new frame
+ s->pending_srce_frames++;
+
+ if (inpicref->interlaced_frame)
+ av_log(ctx, AV_LOG_WARNING, "Interlaced frame found - the output will not be correct.\n");
+
+ // store the pointer to the new frame
+ av_frame_free(&s->srce[s->frst]);
+ s->srce[s->frst] = inpicref;
+
+ if (!s->pending_end_frame && s->srce[s->crnt]) {
+ set_work_frame_pts(ctx);
+ s->pending_end_frame = 1;
+ } else {
+ set_srce_frame_dest_pts(ctx);
+ }
+
+ return process_work_frame(ctx, 1);
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+ AVFilterContext *ctx = outlink->src;
+ FrameRateContext *s = ctx->priv;
+ int exact;
+
+ ff_dlog(ctx, "config_output()\n");
+
+ ff_dlog(ctx,
+ "config_output() input time base:%u/%u (%f)\n",
+ ctx->inputs[0]->time_base.num,ctx->inputs[0]->time_base.den,
+ av_q2d(ctx->inputs[0]->time_base));
+
+ // make sure timebase is small enough to hold the framerate
+
+ exact = av_reduce(&s->dest_time_base.num, &s->dest_time_base.den,
+ av_gcd((int64_t)s->srce_time_base.num * s->dest_frame_rate.num,
+ (int64_t)s->srce_time_base.den * s->dest_frame_rate.den ),
+ (int64_t)s->srce_time_base.den * s->dest_frame_rate.num, INT_MAX);
+
+ av_log(ctx, AV_LOG_INFO,
+ "time base:%u/%u -> %u/%u exact:%d\n",
+ s->srce_time_base.num, s->srce_time_base.den,
+ s->dest_time_base.num, s->dest_time_base.den, exact);
+ if (!exact) {
+ av_log(ctx, AV_LOG_WARNING, "Timebase conversion is not exact\n");
+ }
+
+ outlink->frame_rate = s->dest_frame_rate;
+ outlink->time_base = s->dest_time_base;
+ outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+
+ ff_dlog(ctx,
+ "config_output() output time base:%u/%u (%f) w:%d h:%d\n",
+ outlink->time_base.num, outlink->time_base.den,
+ av_q2d(outlink->time_base),
+ outlink->w, outlink->h);
+
+
+ av_log(ctx, AV_LOG_INFO, "fps -> fps:%u/%u scene score:%f interpolate start:%d end:%d\n",
+ s->dest_frame_rate.num, s->dest_frame_rate.den,
+ s->scene_score, s->interp_start, s->interp_end);
+
+ return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+ AVFilterContext *ctx = outlink->src;
+ FrameRateContext *s = ctx->priv;
+ int val, i;
+
+ ff_dlog(ctx, "request_frame()\n");
+
+ // if there is no "next" frame AND we are not in flush then get one from our input filter
+ if (!s->srce[s->frst] && !s->flush) {
+ ff_dlog(ctx, "request_frame() call source's request_frame()\n");
+ if ((val = ff_request_frame(outlink->src->inputs[0])) < 0) {
+ ff_dlog(ctx, "request_frame() source's request_frame() returned error:%d\n", val);
+ return val;
+ }
+ ff_dlog(ctx, "request_frame() source's request_frame() returned:%d\n", val);
+ return 0;
+ }
+
+ ff_dlog(ctx, "request_frame() REPEAT or FLUSH\n");
+
+ if (s->pending_srce_frames <= 0) {
+ ff_dlog(ctx, "request_frame() nothing else to do, return:EOF\n");
+ return AVERROR_EOF;
+ }
+
+ // otherwise, make brand-new frame and pass to our output filter
+ ff_dlog(ctx, "request_frame() FLUSH\n");
+
+ // back fill at end of file when source has no more frames
+ for (i = s->last; i > s->frst; i--) {
+ if (!s->srce[i - 1] && s->srce[i]) {
+ ff_dlog(ctx, "request_frame() copy:%d to:%d\n", i, i - 1);
+ s->srce[i - 1] = s->srce[i];
+ }
+ }
+
+ set_work_frame_pts(ctx);
+ return process_work_frame(ctx, 0);
+}
+
+static const AVFilterPad framerate_inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .config_props = config_input,
+ .filter_frame = filter_frame,
+ },
+ { NULL }
+};
+
+static const AVFilterPad framerate_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .request_frame = request_frame,
+ .config_props = config_output,
+ },
+ { NULL }
+};
+
+AVFilter ff_vf_framerate = {
+ .name = "framerate",
+ .description = NULL_IF_CONFIG_SMALL("Upsamples or downsamples progressive source between specified frame rates."),
+ .priv_size = sizeof(FrameRateContext),
+ .priv_class = &framerate_class,
+ .init = init,
+ .uninit = uninit,
+ .query_formats = query_formats,
+ .inputs = framerate_inputs,
+ .outputs = framerate_outputs,
+};
1
0
27 Aug '15
ffmpeg | branch: master | Stefano Sabatini <stefasab(a)gmail.com> | Thu Aug 27 17:41:52 2015 +0200| [57cd2f7777a316a447301a7d4b5d1c01da200661] | committer: Stefano Sabatini
lavf/mpegenc: provide better feedback in case of invalid media type
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=57cd2f7777a316a44…
---
libavformat/mpegenc.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
index 2520f49..b3ee2a0 100644
--- a/libavformat/mpegenc.c
+++ b/libavformat/mpegenc.c
@@ -410,7 +410,9 @@ static av_cold int mpeg_mux_init(AVFormatContext *ctx)
stream->max_buffer_size = 16 * 1024;
break;
default:
- return -1;
+ av_log(ctx, AV_LOG_ERROR, "Invalid media type %s for output stream #%d\n",
+ av_get_media_type_string(st->codec->codec_type), i);
+ return AVERROR(EINVAL);
}
stream->fifo = av_fifo_alloc(16);
if (!stream->fifo)
1
0
ffmpeg | branch: master | lummax <luogpg(a)googlemail.com> | Thu Aug 27 12:40:30 2015 +0200| [0c800b27611cadd64f46fd1bbd9dc8bb87fe168b] | committer: Michael Niedermayer
avcodec: Assert on codec->encode2 in encode_audio2
Assert on `avctx->codec->encode2` to avoid a SEGFAULT on the subsequent
function call.
avcodec_encode_video2() uses a similar assertion.
Calling the wrong function on a stream is a serious inconsistency
which could at other places be potentially dangerous and exploitable,
it is thus safer to stop execution and not continue with such
inconsistency after returning an error.
Commit-message-extended-by commiter
Signed-off-by: Michael Niedermayer <michael(a)niedermayer.cc>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=0c800b27611cadd64…
---
libavcodec/utils.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 4da16ee..f51070c 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -1953,6 +1953,8 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
}
}
+ av_assert0(avctx->codec->encode2);
+
ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
if (!ret) {
if (*got_packet_ptr) {
1
0
ffmpeg | branch: master | Timo Rothenpieler <timo(a)rothenpieler.org> | Wed Aug 26 14:55:40 2015 +0200| [1dd854e10fe19d3a9cee38f886f66707c082c132] | committer: Timo Rothenpieler
vaapi: Add hevc hwaccel support
Signed-off-by: Timo Rothenpieler <timo(a)rothenpieler.org>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=1dd854e10fe19d3a9…
---
Changelog | 1 +
configure | 4 +
libavcodec/Makefile | 1 +
libavcodec/allcodecs.c | 1 +
libavcodec/hevc.c | 5 +-
libavcodec/vaapi_hevc.c | 490 +++++++++++++++++++++++++++++++++++++++++++++++
libavcodec/version.h | 2 +-
7 files changed, 502 insertions(+), 2 deletions(-)
diff --git a/Changelog b/Changelog
index 844f5a5..99aae00 100644
--- a/Changelog
+++ b/Changelog
@@ -36,6 +36,7 @@ version <next>:
- waveform filter
- hstack and vstack filter
- Support DNx100 (1440x1080@8)
+- VAAPI hevc hwaccel
version 2.7:
diff --git a/configure b/configure
index 0c3794b..8840027 100755
--- a/configure
+++ b/configure
@@ -2419,6 +2419,8 @@ hevc_d3d11va_hwaccel_select="hevc_decoder"
hevc_dxva2_hwaccel_deps="dxva2 DXVA_PicParams_HEVC"
hevc_dxva2_hwaccel_select="hevc_decoder"
hevc_qsv_hwaccel_deps="libmfx"
+hevc_vaapi_hwaccel_deps="vaapi VAPictureParameterBufferHEVC"
+hevc_vaapi_hwaccel_select="hevc_decoder"
hevc_vdpau_hwaccel_deps="vdpau VdpPictureInfoHEVC"
hevc_vdpau_hwaccel_select="hevc_decoder"
mpeg_vdpau_decoder_deps="vdpau"
@@ -5148,6 +5150,8 @@ check_type "windows.h dxva.h" "DXVA_PicParams_HEVC" -DWINAPI_FAMILY=WINAPI_FAMIL
check_type "windows.h d3d11.h" "ID3D11VideoDecoder"
check_type "d3d9.h dxva2api.h" DXVA2_ConfigPictureDecode -D_WIN32_WINNT=0x0602
+check_type "va/va.h" "VAPictureParameterBufferHEVC"
+
check_type "vdpau/vdpau.h" "VdpPictureInfoHEVC"
if ! disabled w32threads && ! enabled pthreads; then
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3c50ff8..407c6c3 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -715,6 +715,7 @@ OBJS-$(CONFIG_H264_VDPAU_HWACCEL) += vdpau_h264.o
OBJS-$(CONFIG_H264_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
OBJS-$(CONFIG_HEVC_D3D11VA_HWACCEL) += dxva2_hevc.o
OBJS-$(CONFIG_HEVC_DXVA2_HWACCEL) += dxva2_hevc.o
+OBJS-$(CONFIG_HEVC_VAAPI_HWACCEL) += vaapi_hevc.o
OBJS-$(CONFIG_HEVC_VDPAU_HWACCEL) += vdpau_hevc.o
OBJS-$(CONFIG_MPEG1_VDPAU_HWACCEL) += vdpau_mpeg12.o
OBJS-$(CONFIG_MPEG1_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index d4ae497..dbf3927 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -89,6 +89,7 @@ void avcodec_register_all(void)
REGISTER_HWACCEL(HEVC_D3D11VA, hevc_d3d11va);
REGISTER_HWACCEL(HEVC_DXVA2, hevc_dxva2);
REGISTER_HWACCEL(HEVC_QSV, hevc_qsv);
+ REGISTER_HWACCEL(HEVC_VAAPI, hevc_vaapi);
REGISTER_HWACCEL(HEVC_VDPAU, hevc_vdpau);
REGISTER_HWACCEL(MPEG1_XVMC, mpeg1_xvmc);
REGISTER_HWACCEL(MPEG1_VDPAU, mpeg1_vdpau);
diff --git a/libavcodec/hevc.c b/libavcodec/hevc.c
index 3f1a2bb..e7ac50e 100644
--- a/libavcodec/hevc.c
+++ b/libavcodec/hevc.c
@@ -328,7 +328,7 @@ static void export_stream_params(AVCodecContext *avctx, const HEVCParamSets *ps,
static int set_sps(HEVCContext *s, const HEVCSPS *sps, enum AVPixelFormat pix_fmt)
{
- #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + CONFIG_HEVC_D3D11VA_HWACCEL + CONFIG_HEVC_VDPAU_HWACCEL)
+ #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + CONFIG_HEVC_D3D11VA_HWACCEL + CONFIG_HEVC_VAAPI_HWACCEL + CONFIG_HEVC_VDPAU_HWACCEL)
enum AVPixelFormat pix_fmts[HWACCEL_MAX + 2], *fmt = pix_fmts;
int ret, i;
@@ -352,6 +352,9 @@ static int set_sps(HEVCContext *s, const HEVCSPS *sps, enum AVPixelFormat pix_fm
#if CONFIG_HEVC_D3D11VA_HWACCEL
*fmt++ = AV_PIX_FMT_D3D11VA_VLD;
#endif
+#if CONFIG_HEVC_VAAPI_HWACCEL
+ *fmt++ = AV_PIX_FMT_VAAPI;
+#endif
#if CONFIG_HEVC_VDPAU_HWACCEL
*fmt++ = AV_PIX_FMT_VDPAU;
#endif
diff --git a/libavcodec/vaapi_hevc.c b/libavcodec/vaapi_hevc.c
new file mode 100644
index 0000000..762511f
--- /dev/null
+++ b/libavcodec/vaapi_hevc.c
@@ -0,0 +1,490 @@
+/*
+ * HEVC HW decode acceleration through VA API
+ *
+ * Copyright (C) 2015 Timo Rothenpieler <timo(a)rothenpieler.org>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "vaapi_internal.h"
+#include "hevc.h"
+#include "mpegutils.h"
+
+/**
+ * @file
+ * This file implements the glue code between FFmpeg's and VA API's
+ * structures for HEVC decoding.
+ */
+
+typedef struct vaapi_hevc_frame_data {
+ VAPictureParameterBufferHEVC *pic_param;
+ VASliceParameterBufferHEVC *last_slice_param;
+} vaapi_hevc_frame_data;
+
+/**
+ * Initialize an empty VA API picture.
+ *
+ * VA API requires a fixed-size reference picture array.
+ */
+static void init_vaapi_pic(VAPictureHEVC *va_pic)
+{
+ va_pic->picture_id = VA_INVALID_ID;
+ va_pic->flags = VA_PICTURE_HEVC_INVALID;
+ va_pic->pic_order_cnt = 0;
+}
+
+static void fill_vaapi_pic(VAPictureHEVC *va_pic, const HEVCFrame *pic, int rps_type)
+{
+ va_pic->picture_id = ff_vaapi_get_surface_id(pic->frame);
+ va_pic->pic_order_cnt = pic->poc;
+ va_pic->flags = rps_type;
+
+ if (pic->flags & HEVC_FRAME_FLAG_LONG_REF)
+ va_pic->flags |= VA_PICTURE_HEVC_LONG_TERM_REFERENCE;
+
+ if (pic->frame->interlaced_frame) {
+ va_pic->flags |= VA_PICTURE_HEVC_FIELD_PIC;
+
+ if (!pic->frame->top_field_first) {
+ va_pic->flags |= VA_PICTURE_HEVC_BOTTOM_FIELD;
+ }
+ }
+}
+
+static int find_frame_rps_type(const HEVCContext *h, const HEVCFrame *pic)
+{
+ VASurfaceID pic_surf = ff_vaapi_get_surface_id(pic->frame);
+ int i;
+
+ for (i = 0; i < h->rps[ST_CURR_BEF].nb_refs; ++i) {
+ if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_BEF].ref[i]->frame))
+ return VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE;
+ }
+
+ for (i = 0; i < h->rps[ST_CURR_AFT].nb_refs; ++i) {
+ if (pic_surf == ff_vaapi_get_surface_id(h->rps[ST_CURR_AFT].ref[i]->frame))
+ return VA_PICTURE_HEVC_RPS_ST_CURR_AFTER;
+ }
+
+ for (i = 0; i < h->rps[LT_CURR].nb_refs; ++i) {
+ if (pic_surf == ff_vaapi_get_surface_id(h->rps[LT_CURR].ref[i]->frame))
+ return VA_PICTURE_HEVC_RPS_LT_CURR;
+ }
+
+ return 0;
+}
+
+static void fill_vaapi_ReferenceFrames(const HEVCContext *h, VAPictureParameterBufferHEVC *pp)
+{
+ const HEVCFrame *current_picture = h->ref;
+ int i, j, rps_type;
+
+ for (i = 0, j = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); i++) {
+ const HEVCFrame *frame = NULL;
+
+ while (!frame && j < FF_ARRAY_ELEMS(h->DPB)) {
+ if (&h->DPB[j] != current_picture && (h->DPB[j].flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF)))
+ frame = &h->DPB[j];
+ j++;
+ }
+
+ init_vaapi_pic(&pp->ReferenceFrames[i]);
+
+ if (frame) {
+ rps_type = find_frame_rps_type(h, frame);
+ fill_vaapi_pic(&pp->ReferenceFrames[i], frame, rps_type);
+ }
+ }
+}
+
+static uint8_t get_ref_pic_index(const HEVCContext *h, const HEVCFrame *frame)
+{
+ vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private;
+ VAPictureParameterBufferHEVC *pp = frame_data->pic_param;
+ uint8_t i;
+
+ if (!frame)
+ return 0xff;
+
+ for (i = 0; i < FF_ARRAY_ELEMS(pp->ReferenceFrames); ++i) {
+ VASurfaceID pid = pp->ReferenceFrames[i].picture_id;
+ int poc = pp->ReferenceFrames[i].pic_order_cnt;
+ if (pid != VA_INVALID_ID && pid == ff_vaapi_get_surface_id(frame->frame) && poc == frame->poc)
+ return i;
+ }
+
+ return 0xff;
+}
+
+static void fill_picture_parameters(const HEVCContext *h, VAPictureParameterBufferHEVC *pp)
+{
+ int i;
+
+ pp->pic_fields.value = 0;
+ pp->slice_parsing_fields.value = 0;
+
+ fill_vaapi_pic(&pp->CurrPic, h->ref, 0);
+ fill_vaapi_ReferenceFrames(h, pp);
+
+ pp->pic_width_in_luma_samples = h->ps.sps->width;
+ pp->pic_height_in_luma_samples = h->ps.sps->height;
+
+ pp->log2_min_luma_coding_block_size_minus3 = h->ps.sps->log2_min_cb_size - 3;
+
+ pp->pic_fields.bits.chroma_format_idc = h->ps.sps->chroma_format_idc;
+
+ pp->sps_max_dec_pic_buffering_minus1 = h->ps.sps->temporal_layer[h->ps.sps->max_sub_layers - 1].max_dec_pic_buffering - 1;
+ pp->log2_diff_max_min_luma_coding_block_size = h->ps.sps->log2_diff_max_min_coding_block_size;
+ pp->log2_min_transform_block_size_minus2 = h->ps.sps->log2_min_tb_size - 2;
+ pp->log2_diff_max_min_transform_block_size = h->ps.sps->log2_max_trafo_size - h->ps.sps->log2_min_tb_size;
+ pp->max_transform_hierarchy_depth_inter = h->ps.sps->max_transform_hierarchy_depth_inter;
+ pp->max_transform_hierarchy_depth_intra = h->ps.sps->max_transform_hierarchy_depth_intra;
+ pp->num_short_term_ref_pic_sets = h->ps.sps->nb_st_rps;
+ pp->num_long_term_ref_pic_sps = h->ps.sps->num_long_term_ref_pics_sps;
+
+ pp->num_ref_idx_l0_default_active_minus1 = h->ps.pps->num_ref_idx_l0_default_active - 1;
+ pp->num_ref_idx_l1_default_active_minus1 = h->ps.pps->num_ref_idx_l1_default_active - 1;
+ pp->init_qp_minus26 = h->ps.pps->pic_init_qp_minus26;
+
+ pp->pps_cb_qp_offset = h->ps.pps->cb_qp_offset;
+ pp->pps_cr_qp_offset = h->ps.pps->cr_qp_offset;
+
+ pp->pic_fields.bits.tiles_enabled_flag = h->ps.pps->tiles_enabled_flag;
+ pp->pic_fields.bits.separate_colour_plane_flag = h->ps.sps->separate_colour_plane_flag;
+ pp->pic_fields.bits.pcm_enabled_flag = h->ps.sps->pcm_enabled_flag;
+ pp->pic_fields.bits.scaling_list_enabled_flag = h->ps.sps->scaling_list_enable_flag;
+ pp->pic_fields.bits.transform_skip_enabled_flag = h->ps.pps->transform_skip_enabled_flag;
+ pp->pic_fields.bits.amp_enabled_flag = h->ps.sps->amp_enabled_flag;
+ pp->pic_fields.bits.strong_intra_smoothing_enabled_flag = h->ps.sps->sps_strong_intra_smoothing_enable_flag;
+ pp->pic_fields.bits.sign_data_hiding_enabled_flag = h->ps.pps->sign_data_hiding_flag;
+ pp->pic_fields.bits.constrained_intra_pred_flag = h->ps.pps->constrained_intra_pred_flag;
+ pp->pic_fields.bits.cu_qp_delta_enabled_flag = h->ps.pps->cu_qp_delta_enabled_flag;
+ pp->pic_fields.bits.weighted_pred_flag = h->ps.pps->weighted_pred_flag;
+ pp->pic_fields.bits.weighted_bipred_flag = h->ps.pps->weighted_bipred_flag;
+ pp->pic_fields.bits.transquant_bypass_enabled_flag = h->ps.pps->transquant_bypass_enable_flag;
+ pp->pic_fields.bits.entropy_coding_sync_enabled_flag = h->ps.pps->entropy_coding_sync_enabled_flag;
+ pp->pic_fields.bits.pps_loop_filter_across_slices_enabled_flag = h->ps.pps->seq_loop_filter_across_slices_enabled_flag;
+ pp->pic_fields.bits.loop_filter_across_tiles_enabled_flag = h->ps.pps->loop_filter_across_tiles_enabled_flag;
+
+ pp->pic_fields.bits.pcm_loop_filter_disabled_flag = h->ps.sps->pcm.loop_filter_disable_flag;
+ pp->pcm_sample_bit_depth_luma_minus1 = h->ps.sps->pcm.bit_depth - 1;
+ pp->pcm_sample_bit_depth_chroma_minus1 = h->ps.sps->pcm.bit_depth_chroma - 1;
+ pp->log2_min_pcm_luma_coding_block_size_minus3 = h->ps.sps->pcm.log2_min_pcm_cb_size - 3;
+ pp->log2_diff_max_min_pcm_luma_coding_block_size = h->ps.sps->pcm.log2_max_pcm_cb_size - h->ps.sps->pcm.log2_min_pcm_cb_size;
+
+ memset(pp->column_width_minus1, 0, sizeof(pp->column_width_minus1));
+ memset(pp->row_height_minus1, 0, sizeof(pp->row_height_minus1));
+
+ if (h->ps.pps->tiles_enabled_flag) {
+ pp->num_tile_columns_minus1 = h->ps.pps->num_tile_columns - 1;
+ pp->num_tile_rows_minus1 = h->ps.pps->num_tile_rows - 1;
+
+ for (i = 0; i < h->ps.pps->num_tile_columns; i++)
+ pp->column_width_minus1[i] = h->ps.pps->column_width[i] - 1;
+
+ for (i = 0; i < h->ps.pps->num_tile_rows; i++)
+ pp->row_height_minus1[i] = h->ps.pps->row_height[i] - 1;
+ }
+
+ pp->diff_cu_qp_delta_depth = h->ps.pps->diff_cu_qp_delta_depth;
+ pp->pps_beta_offset_div2 = h->ps.pps->beta_offset / 2;
+ pp->pps_tc_offset_div2 = h->ps.pps->tc_offset / 2;
+ pp->log2_parallel_merge_level_minus2 = h->ps.pps->log2_parallel_merge_level - 2;
+
+ /* Diffrent chroma/luma bit depths are currently not supported by ffmpeg. */
+ pp->bit_depth_luma_minus8 = h->ps.sps->bit_depth - 8;
+ pp->bit_depth_chroma_minus8 = h->ps.sps->bit_depth - 8;
+
+ pp->slice_parsing_fields.bits.lists_modification_present_flag = h->ps.pps->lists_modification_present_flag;
+ pp->slice_parsing_fields.bits.long_term_ref_pics_present_flag = h->ps.sps->long_term_ref_pics_present_flag;
+ pp->slice_parsing_fields.bits.sps_temporal_mvp_enabled_flag = h->ps.sps->sps_temporal_mvp_enabled_flag;
+ pp->slice_parsing_fields.bits.cabac_init_present_flag = h->ps.pps->cabac_init_present_flag;
+ pp->slice_parsing_fields.bits.output_flag_present_flag = h->ps.pps->output_flag_present_flag;
+ pp->slice_parsing_fields.bits.dependent_slice_segments_enabled_flag = h->ps.pps->dependent_slice_segments_enabled_flag;
+ pp->slice_parsing_fields.bits.pps_slice_chroma_qp_offsets_present_flag = h->ps.pps->pic_slice_level_chroma_qp_offsets_present_flag;
+ pp->slice_parsing_fields.bits.sample_adaptive_offset_enabled_flag = h->ps.sps->sao_enabled;
+ pp->slice_parsing_fields.bits.deblocking_filter_override_enabled_flag = h->ps.pps->deblocking_filter_override_enabled_flag;
+ pp->slice_parsing_fields.bits.pps_disable_deblocking_filter_flag = h->ps.pps->disable_dbf;
+ pp->slice_parsing_fields.bits.slice_segment_header_extension_present_flag = h->ps.pps->slice_header_extension_present_flag;
+
+ pp->log2_max_pic_order_cnt_lsb_minus4 = h->ps.sps->log2_max_poc_lsb - 4;
+ pp->num_extra_slice_header_bits = h->ps.pps->num_extra_slice_header_bits;
+
+ if (h->nal_unit_type >= NAL_BLA_W_LP && h->nal_unit_type <= NAL_CRA_NUT) {
+ pp->slice_parsing_fields.bits.RapPicFlag = 1;
+ } else {
+ pp->slice_parsing_fields.bits.RapPicFlag = 0;
+ }
+
+ if (IS_IDR(h)) {
+ pp->slice_parsing_fields.bits.IdrPicFlag = 1;
+ } else {
+ pp->slice_parsing_fields.bits.IdrPicFlag = 0;
+ }
+
+ if (IS_IRAP(h)) {
+ pp->slice_parsing_fields.bits.IntraPicFlag = 1;
+ } else {
+ pp->slice_parsing_fields.bits.IntraPicFlag = 0;
+ }
+
+ if (h->sh.short_term_ref_pic_set_sps_flag == 0 && h->sh.short_term_rps) {
+ pp->st_rps_bits = h->sh.short_term_ref_pic_set_size;
+ } else {
+ pp->st_rps_bits = 0;
+ }
+
+ /* TODO */
+ pp->pic_fields.bits.NoPicReorderingFlag = 0;
+ pp->pic_fields.bits.NoBiPredFlag = 0;
+}
+
+
+/** Initialize and start decoding a frame with VA API. */
+static int vaapi_hevc_start_frame(AVCodecContext *avctx,
+ av_unused const uint8_t *buffer,
+ av_unused uint32_t size)
+{
+ HEVCContext * const h = avctx->priv_data;
+ FFVAContext * const vactx = ff_vaapi_get_context(avctx);
+ vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private;
+ VAPictureParameterBufferHEVC *pic_param;
+ VAIQMatrixBufferHEVC *iq_matrix;
+ ScalingList const * scaling_list;
+ int i, j, pos;
+
+ ff_dlog(avctx, "vaapi_hevc_start_frame()\n");
+
+ vactx->slice_param_size = sizeof(VASliceParameterBufferHEVC);
+
+ /* Fill in VAPictureParameterBufferHEVC. */
+ pic_param = ff_vaapi_alloc_pic_param(vactx, sizeof(VAPictureParameterBufferHEVC));
+ if (!pic_param)
+ return -1;
+ fill_picture_parameters(h, pic_param);
+ frame_data->pic_param = pic_param;
+
+ /* Fill in VAIQMatrixBufferHEVC. */
+ if (h->ps.pps->scaling_list_data_present_flag) {
+ scaling_list = &h->ps.pps->scaling_list;
+ } else if (h->ps.sps->scaling_list_enable_flag) {
+ scaling_list = &h->ps.sps->scaling_list;
+ } else {
+ return 0;
+ }
+
+ iq_matrix = ff_vaapi_alloc_iq_matrix(vactx, sizeof(VAIQMatrixBufferHEVC));
+ if (!iq_matrix)
+ return -1;
+
+ for (i = 0; i < 6; ++i) {
+ for (j = 0; j < 16; ++j) {
+ pos = 4 * ff_hevc_diag_scan4x4_y[j] + ff_hevc_diag_scan4x4_x[j];
+ iq_matrix->ScalingList4x4[i][j] = scaling_list->sl[0][i][pos];
+ }
+ for (j = 0; j < 64; ++j) {
+ pos = 8 * ff_hevc_diag_scan8x8_y[j] + ff_hevc_diag_scan8x8_x[j];
+ iq_matrix->ScalingList8x8[i][j] = scaling_list->sl[1][i][pos];
+ iq_matrix->ScalingList16x16[i][j] = scaling_list->sl[2][i][pos];
+ if (i < 2) {
+ iq_matrix->ScalingList32x32[i][j] = scaling_list->sl[3][i * 3][pos];
+ }
+ }
+ iq_matrix->ScalingListDC16x16[i] = scaling_list->sl_dc[0][i];
+ if (i < 2) {
+ iq_matrix->ScalingListDC32x32[i] = scaling_list->sl_dc[1][i * 3];
+ }
+ }
+
+ return 0;
+}
+
+/** End a hardware decoding based frame. */
+static int vaapi_hevc_end_frame(AVCodecContext *avctx)
+{
+ FFVAContext * const vactx = ff_vaapi_get_context(avctx);
+ HEVCContext * const h = avctx->priv_data;
+ vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private;
+ int ret;
+
+ ff_dlog(avctx, "vaapi_hevc_end_frame()\n");
+
+ frame_data->last_slice_param->LongSliceFlags.fields.LastSliceOfPic = 1;
+
+ ret = ff_vaapi_commit_slices(vactx);
+ if (ret < 0)
+ goto finish;
+
+ ret = ff_vaapi_render_picture(vactx, ff_vaapi_get_surface_id(h->ref->frame));
+ if (ret < 0)
+ goto finish;
+
+finish:
+ ff_vaapi_common_end_frame(avctx);
+ return ret;
+}
+
+static int fill_pred_weight_table(HEVCContext * const h,
+ VASliceParameterBufferHEVC *slice_param,
+ SliceHeader * const sh)
+{
+ int i;
+
+ memset(slice_param->delta_luma_weight_l0, 0, sizeof(slice_param->delta_luma_weight_l0));
+ memset(slice_param->delta_luma_weight_l1, 0, sizeof(slice_param->delta_luma_weight_l1));
+ memset(slice_param->luma_offset_l0, 0, sizeof(slice_param->luma_offset_l0));
+ memset(slice_param->luma_offset_l1, 0, sizeof(slice_param->luma_offset_l1));
+ memset(slice_param->delta_chroma_weight_l0, 0, sizeof(slice_param->delta_chroma_weight_l0));
+ memset(slice_param->delta_chroma_weight_l1, 0, sizeof(slice_param->delta_chroma_weight_l1));
+ memset(slice_param->ChromaOffsetL0, 0, sizeof(slice_param->ChromaOffsetL0));
+ memset(slice_param->ChromaOffsetL1, 0, sizeof(slice_param->ChromaOffsetL1));
+
+ slice_param->delta_chroma_log2_weight_denom = 0;
+ slice_param->luma_log2_weight_denom = 0;
+
+ if ( sh->slice_type == I_SLICE
+ || (sh->slice_type == P_SLICE && !h->ps.pps->weighted_pred_flag)
+ || (sh->slice_type == B_SLICE && !h->ps.pps->weighted_bipred_flag)) {
+ return 0;
+ }
+
+ slice_param->luma_log2_weight_denom = sh->luma_log2_weight_denom;
+
+ if (h->ps.sps->chroma_format_idc) {
+ slice_param->delta_chroma_log2_weight_denom = sh->chroma_log2_weight_denom - sh->luma_log2_weight_denom;
+ }
+
+ for (i = 0; i < 15 && i < sh->nb_refs[L0]; ++i) {
+ slice_param->delta_luma_weight_l0[i] = sh->luma_weight_l0[i] - (1 << sh->luma_log2_weight_denom);
+ slice_param->luma_offset_l0[i] = sh->luma_offset_l0[i];
+ slice_param->delta_chroma_weight_l0[i][0] = sh->chroma_weight_l0[i][0] - (1 << sh->chroma_log2_weight_denom);
+ slice_param->delta_chroma_weight_l0[i][1] = sh->chroma_weight_l0[i][1] - (1 << sh->chroma_log2_weight_denom);
+ slice_param->ChromaOffsetL0[i][0] = sh->chroma_offset_l0[i][0];
+ slice_param->ChromaOffsetL0[i][1] = sh->chroma_offset_l0[i][1];
+ }
+
+ if (sh->slice_type == B_SLICE) {
+ for (i = 0; i < 15 && i < sh->nb_refs[L1]; ++i) {
+ slice_param->delta_luma_weight_l1[i] = sh->luma_weight_l1[i] - (1 << sh->luma_log2_weight_denom);
+ slice_param->luma_offset_l1[i] = sh->luma_offset_l1[i];
+ slice_param->delta_chroma_weight_l1[i][0] = sh->chroma_weight_l1[i][0] - (1 << sh->chroma_log2_weight_denom);
+ slice_param->delta_chroma_weight_l1[i][1] = sh->chroma_weight_l1[i][1] - (1 << sh->chroma_log2_weight_denom);
+ slice_param->ChromaOffsetL1[i][0] = sh->chroma_offset_l1[i][0];
+ slice_param->ChromaOffsetL1[i][1] = sh->chroma_offset_l1[i][1];
+ }
+ }
+
+ return 0;
+}
+
+/** Decode the given hevc slice with VA API. */
+static int vaapi_hevc_decode_slice(AVCodecContext *avctx,
+ const uint8_t *buffer,
+ uint32_t size)
+{
+ FFVAContext * const vactx = ff_vaapi_get_context(avctx);
+ HEVCContext * const h = avctx->priv_data;
+ vaapi_hevc_frame_data *frame_data = h->ref->hwaccel_picture_private;
+ SliceHeader * const sh = &h->sh;
+ VASliceParameterBufferHEVC *slice_param;
+ int i, list_idx;
+ uint8_t nb_list = sh->slice_type == B_SLICE ? 2 : 1;
+
+ if (sh->slice_type == I_SLICE)
+ nb_list = 0;
+
+ ff_dlog(avctx, "vaapi_hevc_decode_slice(): buffer %p, size %d\n", buffer, size);
+
+ /* Fill in VASliceParameterBufferH264. */
+ slice_param = (VASliceParameterBufferHEVC *)ff_vaapi_alloc_slice(vactx, buffer, size);
+ if (!slice_param)
+ return -1;
+
+ frame_data->last_slice_param = slice_param;
+
+ /* The base structure changed, so this has to be re-set in order to be valid on every byte order. */
+ slice_param->slice_data_flag = VA_SLICE_DATA_FLAG_ALL;
+
+ /* Add 1 to the bits count here to account for the byte_alignment bit, which allways is at least one bit and not accounted for otherwise. */
+ slice_param->slice_data_byte_offset = (get_bits_count(&h->HEVClc->gb) + 1 + 7) / 8;
+
+ slice_param->slice_segment_address = sh->slice_segment_addr;
+
+ slice_param->LongSliceFlags.value = 0;
+ slice_param->LongSliceFlags.fields.dependent_slice_segment_flag = sh->dependent_slice_segment_flag;
+ slice_param->LongSliceFlags.fields.slice_type = sh->slice_type;
+ slice_param->LongSliceFlags.fields.color_plane_id = sh->colour_plane_id;
+ slice_param->LongSliceFlags.fields.mvd_l1_zero_flag = sh->mvd_l1_zero_flag;
+ slice_param->LongSliceFlags.fields.cabac_init_flag = sh->cabac_init_flag;
+ slice_param->LongSliceFlags.fields.slice_temporal_mvp_enabled_flag = sh->slice_temporal_mvp_enabled_flag;
+ slice_param->LongSliceFlags.fields.slice_deblocking_filter_disabled_flag = sh->disable_deblocking_filter_flag;
+ slice_param->LongSliceFlags.fields.collocated_from_l0_flag = sh->collocated_list == L0 ? 1 : 0;
+ slice_param->LongSliceFlags.fields.slice_loop_filter_across_slices_enabled_flag = sh->slice_loop_filter_across_slices_enabled_flag;
+
+ slice_param->LongSliceFlags.fields.slice_sao_luma_flag = sh->slice_sample_adaptive_offset_flag[0];
+ if (h->ps.sps->chroma_format_idc) {
+ slice_param->LongSliceFlags.fields.slice_sao_chroma_flag = sh->slice_sample_adaptive_offset_flag[1];
+ }
+
+ if (sh->slice_temporal_mvp_enabled_flag) {
+ slice_param->collocated_ref_idx = sh->collocated_ref_idx;
+ } else {
+ slice_param->collocated_ref_idx = 0xFF;
+ }
+
+ slice_param->slice_qp_delta = sh->slice_qp_delta;
+ slice_param->slice_cb_qp_offset = sh->slice_cb_qp_offset;
+ slice_param->slice_cr_qp_offset = sh->slice_cr_qp_offset;
+ slice_param->slice_beta_offset_div2 = sh->beta_offset / 2;
+ slice_param->slice_tc_offset_div2 = sh->tc_offset / 2;
+
+ if (sh->slice_type == I_SLICE) {
+ slice_param->five_minus_max_num_merge_cand = 0;
+ } else {
+ slice_param->five_minus_max_num_merge_cand = 5 - sh->max_num_merge_cand;
+ }
+
+ slice_param->num_ref_idx_l0_active_minus1 = sh->nb_refs[L0] ? sh->nb_refs[L0] - 1 : 0;
+ slice_param->num_ref_idx_l1_active_minus1 = sh->nb_refs[L1] ? sh->nb_refs[L1] - 1 : 0;
+
+ memset(slice_param->RefPicList, 0xFF, sizeof(slice_param->RefPicList));
+
+ /* h->ref->refPicList is updated befor calling each slice */
+ for (list_idx = 0; list_idx < nb_list; ++list_idx) {
+ RefPicList *rpl = &h->ref->refPicList[list_idx];
+
+ for (i = 0; i < rpl->nb_refs; ++i) {
+ slice_param->RefPicList[list_idx][i] = get_ref_pic_index(h, rpl->ref[i]);
+ }
+ }
+
+ return fill_pred_weight_table(h, slice_param, sh);
+}
+
+AVHWAccel ff_hevc_vaapi_hwaccel = {
+ .name = "hevc_vaapi",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .id = AV_CODEC_ID_HEVC,
+ .pix_fmt = AV_PIX_FMT_VAAPI,
+ .start_frame = vaapi_hevc_start_frame,
+ .end_frame = vaapi_hevc_end_frame,
+ .decode_slice = vaapi_hevc_decode_slice,
+ .init = ff_vaapi_context_init,
+ .uninit = ff_vaapi_context_fini,
+ .priv_data_size = sizeof(FFVAContext),
+ .frame_priv_data_size = sizeof(vaapi_hevc_frame_data),
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index f0fa7f0..e098dde 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
#include "libavutil/version.h"
#define LIBAVCODEC_VERSION_MAJOR 56
-#define LIBAVCODEC_VERSION_MINOR 58
+#define LIBAVCODEC_VERSION_MINOR 59
#define LIBAVCODEC_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
1
0