From da2e774fd6841da7cede8c8ef30337449329727c Mon Sep 17 00:00:00 2001 From: Laurentiu Ion Date: Tue, 10 Jan 2012 03:21:17 +0200 Subject: [PATCH 01/22] kmvc: Use bytestream2 functions to prevent buffer overreads. Signed-off-by: Ronald S. Bultje --- libavcodec/kmvc.c | 150 ++++++++++++++++++---------------------------- 1 file changed, 58 insertions(+), 92 deletions(-) diff --git a/libavcodec/kmvc.c b/libavcodec/kmvc.c index 6c55863e7d..2b54b84e99 100644 --- a/libavcodec/kmvc.c +++ b/libavcodec/kmvc.c @@ -46,6 +46,7 @@ typedef struct KmvcContext { uint32_t pal[256]; uint8_t *cur, *prev; uint8_t *frm0, *frm1; + GetByteContext g; } KmvcContext; typedef struct BitBuf { @@ -55,23 +56,19 @@ typedef struct BitBuf { #define BLK(data, x, y) data[(x) + (y) * 320] -#define kmvc_init_getbits(bb, src) bb.bits = 7; bb.bitbuf = *src++; +#define kmvc_init_getbits(bb, g) bb.bits = 7; bb.bitbuf = bytestream2_get_byte(g); -#define kmvc_getbit(bb, src, src_end, res) {\ +#define kmvc_getbit(bb, g, res) {\ res = 0; \ if (bb.bitbuf & (1 << bb.bits)) res = 1; \ bb.bits--; \ if(bb.bits == -1) { \ - if (src >= src_end) { \ - av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); \ - return AVERROR_INVALIDDATA; \ - } \ - bb.bitbuf = *src++; \ + bb.bitbuf = bytestream2_get_byte(g); \ bb.bits = 7; \ } \ } -static int kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int src_size, int w, int h) +static int kmvc_decode_intra_8x8(KmvcContext * ctx, int w, int h) { BitBuf bb; int res, val; @@ -79,42 +76,33 @@ static int kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int src int bx, by; int l0x, l1x, l0y, l1y; int mx, my; - const uint8_t *src_end = src + src_size; - kmvc_init_getbits(bb, src); + kmvc_init_getbits(bb, &ctx->g); for (by = 0; by < h; by += 8) for (bx = 0; bx < w; bx += 8) { - kmvc_getbit(bb, src, src_end, res); + if (!bytestream2_get_bytes_left(&ctx->g)) { + av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); + return AVERROR_INVALIDDATA; + } + kmvc_getbit(bb, &ctx->g, res); if (!res) { // fill whole 8x8 block - if (src >= src_end) { - av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); - return AVERROR_INVALIDDATA; - } - val = *src++; + val = bytestream2_get_byte(&ctx->g); for (i = 0; i < 64; i++) BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val; } else { // handle four 4x4 subblocks for (i = 0; i < 4; i++) { l0x = bx + (i & 1) * 4; l0y = by + (i & 2) * 2; - kmvc_getbit(bb, src, src_end, res); + kmvc_getbit(bb, &ctx->g, res); if (!res) { - kmvc_getbit(bb, src, src_end, res); + kmvc_getbit(bb, &ctx->g, res); if (!res) { // fill whole 4x4 block - if (src >= src_end) { - av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); - return AVERROR_INVALIDDATA; - } - val = *src++; + val = bytestream2_get_byte(&ctx->g); for (j = 0; j < 16; j++) BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val; } else { // copy block from already decoded place - if (src >= src_end) { - av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); - return AVERROR_INVALIDDATA; - } - val = *src++; + val = bytestream2_get_byte(&ctx->g); mx = val & 0xF; my = val >> 4; for (j = 0; j < 16; j++) @@ -125,25 +113,17 @@ static int kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int src for (j = 0; j < 4; j++) { l1x = l0x + (j & 1) * 2; l1y = l0y + (j & 2); - kmvc_getbit(bb, src, src_end, res); + kmvc_getbit(bb, &ctx->g, res); if (!res) { - kmvc_getbit(bb, src, src_end, res); + kmvc_getbit(bb, &ctx->g, res); if (!res) { // fill whole 2x2 block - if (src >= src_end) { - av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); - return AVERROR_INVALIDDATA; - } - val = *src++; + val = bytestream2_get_byte(&ctx->g); BLK(ctx->cur, l1x, l1y) = val; BLK(ctx->cur, l1x + 1, l1y) = val; BLK(ctx->cur, l1x, l1y + 1) = val; BLK(ctx->cur, l1x + 1, l1y + 1) = val; } else { // copy block from already decoded place - if (src >= src_end) { - av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); - return AVERROR_INVALIDDATA; - } - val = *src++; + val = bytestream2_get_byte(&ctx->g); mx = val & 0xF; my = val >> 4; BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my); @@ -155,10 +135,10 @@ static int kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int src BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my); } } else { // read values for block - BLK(ctx->cur, l1x, l1y) = *src++; - BLK(ctx->cur, l1x + 1, l1y) = *src++; - BLK(ctx->cur, l1x, l1y + 1) = *src++; - BLK(ctx->cur, l1x + 1, l1y + 1) = *src++; + BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g); + BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g); + BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g); + BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g); } } } @@ -169,7 +149,7 @@ static int kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int src return 0; } -static int kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int src_size, int w, int h) +static int kmvc_decode_inter_8x8(KmvcContext * ctx, int w, int h) { BitBuf bb; int res, val; @@ -177,21 +157,20 @@ static int kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int src int bx, by; int l0x, l1x, l0y, l1y; int mx, my; - const uint8_t *src_end = src + src_size; - kmvc_init_getbits(bb, src); + kmvc_init_getbits(bb, &ctx->g); for (by = 0; by < h; by += 8) for (bx = 0; bx < w; bx += 8) { - kmvc_getbit(bb, src, src_end, res); + kmvc_getbit(bb, &ctx->g, res); if (!res) { - kmvc_getbit(bb, src, src_end, res); + kmvc_getbit(bb, &ctx->g, res); if (!res) { // fill whole 8x8 block - if (src >= src_end) { + if (!bytestream2_get_bytes_left(&ctx->g)) { av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); return AVERROR_INVALIDDATA; } - val = *src++; + val = bytestream2_get_byte(&ctx->g); for (i = 0; i < 64; i++) BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val; } else { // copy block from previous frame @@ -200,26 +179,22 @@ static int kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int src BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3)); } } else { // handle four 4x4 subblocks + if (!bytestream2_get_bytes_left(&ctx->g)) { + av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); + return AVERROR_INVALIDDATA; + } for (i = 0; i < 4; i++) { l0x = bx + (i & 1) * 4; l0y = by + (i & 2) * 2; - kmvc_getbit(bb, src, src_end, res); + kmvc_getbit(bb, &ctx->g, res); if (!res) { - kmvc_getbit(bb, src, src_end, res); + kmvc_getbit(bb, &ctx->g, res); if (!res) { // fill whole 4x4 block - if (src >= src_end) { - av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); - return AVERROR_INVALIDDATA; - } - val = *src++; + val = bytestream2_get_byte(&ctx->g); for (j = 0; j < 16; j++) BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val; } else { // copy block - if (src >= src_end) { - av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); - return AVERROR_INVALIDDATA; - } - val = *src++; + val = bytestream2_get_byte(&ctx->g); mx = (val & 0xF) - 8; my = (val >> 4) - 8; for (j = 0; j < 16; j++) @@ -230,25 +205,17 @@ static int kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int src for (j = 0; j < 4; j++) { l1x = l0x + (j & 1) * 2; l1y = l0y + (j & 2); - kmvc_getbit(bb, src, src_end, res); + kmvc_getbit(bb, &ctx->g, res); if (!res) { - kmvc_getbit(bb, src, src_end, res); + kmvc_getbit(bb, &ctx->g, res); if (!res) { // fill whole 2x2 block - if (src >= src_end) { - av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); - return AVERROR_INVALIDDATA; - } - val = *src++; + val = bytestream2_get_byte(&ctx->g); BLK(ctx->cur, l1x, l1y) = val; BLK(ctx->cur, l1x + 1, l1y) = val; BLK(ctx->cur, l1x, l1y + 1) = val; BLK(ctx->cur, l1x + 1, l1y + 1) = val; } else { // copy block - if (src >= src_end) { - av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n"); - return AVERROR_INVALIDDATA; - } - val = *src++; + val = bytestream2_get_byte(&ctx->g); mx = (val & 0xF) - 8; my = (val >> 4) - 8; BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my); @@ -260,10 +227,10 @@ static int kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int src BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my); } } else { // read values for block - BLK(ctx->cur, l1x, l1y) = *src++; - BLK(ctx->cur, l1x + 1, l1y) = *src++; - BLK(ctx->cur, l1x, l1y + 1) = *src++; - BLK(ctx->cur, l1x + 1, l1y + 1) = *src++; + BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g); + BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g); + BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g); + BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g); } } } @@ -276,8 +243,6 @@ static int kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int src static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPacket *avpkt) { - const uint8_t *buf = avpkt->data; - int buf_size = avpkt->size; KmvcContext *const ctx = avctx->priv_data; uint8_t *out, *src; int i; @@ -285,6 +250,7 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPa int blocksize; const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL); + bytestream2_init(&ctx->g, avpkt->data, avpkt->size); if (ctx->pic.data[0]) avctx->release_buffer(avctx, &ctx->pic); @@ -295,16 +261,16 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPa return -1; } - header = *buf++; + header = bytestream2_get_byte(&ctx->g); /* blocksize 127 is really palette change event */ - if (buf[0] == 127) { - buf += 3; + if (bytestream2_peek_byte(&ctx->g) == 127) { + bytestream2_skip(&ctx->g, 3); for (i = 0; i < 127; i++) { - ctx->pal[i + (header & 0x81)] = AV_RB24(buf); - buf += 4; + ctx->pal[i + (header & 0x81)] = bytestream2_get_be24(&ctx->g); + bytestream2_skip(&ctx->g, 1); } - buf -= 127 * 4 + 3; + bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR); } if (header & KMVC_KEYFRAME) { @@ -319,7 +285,7 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPa ctx->pic.palette_has_changed = 1; // palette starts from index 1 and has 127 entries for (i = 1; i <= ctx->palsize; i++) { - ctx->pal[i] = bytestream_get_be24(&buf); + ctx->pal[i] = bytestream2_get_be24(&ctx->g); } } @@ -336,7 +302,7 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPa /* make the palette available on the way out */ memcpy(ctx->pic.data[1], ctx->pal, 1024); - blocksize = *buf++; + blocksize = bytestream2_get_byte(&ctx->g); if (blocksize != 8 && blocksize != 127) { av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize); @@ -349,10 +315,10 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPa memcpy(ctx->cur, ctx->prev, 320 * 200); break; case 3: - kmvc_decode_intra_8x8(ctx, buf, buf_size, avctx->width, avctx->height); + kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height); break; case 4: - kmvc_decode_inter_8x8(ctx, buf, buf_size, avctx->width, avctx->height); + kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height); break; default: av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD); @@ -380,7 +346,7 @@ static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPa *(AVFrame *) data = ctx->pic; /* always report that the buffer was completely consumed */ - return buf_size; + return avpkt->size; } From b8c310cb0a071d998b90e9b166ed29062524de8b Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Wed, 4 Jan 2012 15:06:10 +0100 Subject: [PATCH 02/22] v4l2: support compressed formats Let pass the codec name to -pixel_format and introduce -input_format. --- libavdevice/v4l2.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c index 71e5b1159a..9c5da39e6a 100644 --- a/libavdevice/v4l2.c +++ b/libavdevice/v4l2.c @@ -708,11 +708,15 @@ static int v4l2_read_header(AVFormatContext *s1, AVFormatParameters *ap) } if (s->pixel_format) { + AVCodec *codec = avcodec_find_decoder_by_name(s->pixel_format); + + if (codec) + s1->video_codec_id = codec->id; pix_fmt = av_get_pix_fmt(s->pixel_format); - if (pix_fmt == PIX_FMT_NONE) { - av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", + if (pix_fmt == PIX_FMT_NONE && !codec) { + av_log(s1, AV_LOG_ERROR, "No such input format: %s.\n", s->pixel_format); res = AVERROR(EINVAL); @@ -818,7 +822,8 @@ static const AVOption options[] = { { "standard", "TV standard, used only by analog frame grabber", OFFSET(standard), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC }, { "channel", "TV channel, used only by frame grabber", OFFSET(channel), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, DEC }, { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, - { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, + { "pixel_format", "Preferred pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, + { "input_format", "Preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, { "list_formats", "List available formats and exit", OFFSET(list_format), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, DEC, "list_formats" }, { "all", "Show all available formats", OFFSET(list_format), AV_OPT_TYPE_CONST, {.dbl = V4L_ALLFORMATS }, 0, INT_MAX, DEC, "list_formats" }, From 0efd48dfd15273a5ac85f2fd42f19d419fafe90d Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Thu, 5 Jan 2012 12:17:45 +0100 Subject: [PATCH 03/22] v4l2: poll the file descriptor Instead of busy waiting use poll(); --- libavdevice/v4l2.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c index 9c5da39e6a..2da98728c9 100644 --- a/libavdevice/v4l2.c +++ b/libavdevice/v4l2.c @@ -36,6 +36,7 @@ #include #include #include +#include #if HAVE_SYS_VIDEOIO_H #include #else @@ -48,6 +49,7 @@ #include "libavutil/parseutils.h" #include "libavutil/pixdesc.h" #include "libavutil/avstring.h" +#include "libavutil/mathematics.h" static const int desired_video_buffers = 256; @@ -61,6 +63,7 @@ struct video_data { int frame_format; /* V4L2_PIX_FMT_* */ int width, height; int frame_size; + int timeout; int interlaced; int top_field_first; @@ -436,12 +439,20 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) struct video_data *s = ctx->priv_data; struct v4l2_buffer buf; struct buff_data *buf_descriptor; + struct pollfd p = { .fd = s->fd, .events = POLLIN }; int res; memset(&buf, 0, sizeof(struct v4l2_buffer)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; + res = poll(&p, 1, s->timeout); + if (res < 0) + return AVERROR(errno); + + if (!(p.revents & (POLLIN | POLLERR | POLLHUP))) + return AVERROR(EAGAIN); + /* FIXME: Some special treatment might be needed in case of loss of signal... */ while ((res = ioctl(s->fd, VIDIOC_DQBUF, &buf)) < 0 && (errno == EINTR)); if (res < 0) { @@ -635,6 +646,10 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap) s1->streams[0]->codec->time_base.den = tpf->denominator; s1->streams[0]->codec->time_base.num = tpf->numerator; + s->timeout = 100 + + av_rescale_q(1, s1->streams[0]->codec->time_base, + (AVRational){1, 1000}); + return 0; } From b6db385922b79939b0dc124d53ddb4824afac040 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Sat, 7 Jan 2012 10:59:35 +0100 Subject: [PATCH 04/22] v4l2: use C99 struct initializer Remove some unneeded memsets. --- libavdevice/v4l2.c | 58 ++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c index 2da98728c9..191decde2a 100644 --- a/libavdevice/v4l2.c +++ b/libavdevice/v4l2.c @@ -169,14 +169,11 @@ static int device_init(AVFormatContext *ctx, int *width, int *height, { struct video_data *s = ctx->priv_data; int fd = s->fd; - struct v4l2_format fmt; + struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE }; struct v4l2_pix_format *pix = &fmt.fmt.pix; int res; - memset(&fmt, 0, sizeof(struct v4l2_format)); - - fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; pix->width = *width; pix->height = *height; pix->pixelformat = pix_fmt; @@ -334,14 +331,14 @@ static void list_formats(AVFormatContext *ctx, int fd, int type) static int mmap_init(AVFormatContext *ctx) { - struct video_data *s = ctx->priv_data; - struct v4l2_requestbuffers req; int i, res; + struct video_data *s = ctx->priv_data; + struct v4l2_requestbuffers req = { + .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, + .count = desired_video_buffers, + .memory = V4L2_MEMORY_MMAP + }; - memset(&req, 0, sizeof(struct v4l2_requestbuffers)); - req.count = desired_video_buffers; - req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - req.memory = V4L2_MEMORY_MMAP; res = ioctl(s->fd, VIDIOC_REQBUFS, &req); if (res < 0) { if (errno == EINVAL) { @@ -374,12 +371,12 @@ static int mmap_init(AVFormatContext *ctx) } for (i = 0; i < req.count; i++) { - struct v4l2_buffer buf; + struct v4l2_buffer buf = { + .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, + .index = i, + .memory = V4L2_MEMORY_MMAP + }; - memset(&buf, 0, sizeof(struct v4l2_buffer)); - buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - buf.memory = V4L2_MEMORY_MMAP; - buf.index = i; res = ioctl(s->fd, VIDIOC_QUERYBUF, &buf); if (res < 0) { av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF)\n"); @@ -411,14 +408,13 @@ static int mmap_init(AVFormatContext *ctx) static void mmap_release_buffer(AVPacket *pkt) { - struct v4l2_buffer buf; + struct v4l2_buffer buf = { 0 }; int res, fd; struct buff_data *buf_descriptor = pkt->priv; if (pkt->data == NULL) return; - memset(&buf, 0, sizeof(struct v4l2_buffer)); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = buf_descriptor->index; @@ -437,15 +433,14 @@ static void mmap_release_buffer(AVPacket *pkt) static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt) { struct video_data *s = ctx->priv_data; - struct v4l2_buffer buf; + struct v4l2_buffer buf = { + .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, + .memory = V4L2_MEMORY_MMAP + }; struct buff_data *buf_descriptor; struct pollfd p = { .fd = s->fd, .events = POLLIN }; int res; - memset(&buf, 0, sizeof(struct v4l2_buffer)); - buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - buf.memory = V4L2_MEMORY_MMAP; - res = poll(&p, 1, s->timeout); if (res < 0) return AVERROR(errno); @@ -504,12 +499,11 @@ static int mmap_start(AVFormatContext *ctx) int i, res; for (i = 0; i < s->buffers; i++) { - struct v4l2_buffer buf; - - memset(&buf, 0, sizeof(struct v4l2_buffer)); - buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - buf.memory = V4L2_MEMORY_MMAP; - buf.index = i; + struct v4l2_buffer buf = { + .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, + .index = i, + .memory = V4L2_MEMORY_MMAP + }; res = ioctl(s->fd, VIDIOC_QBUF, &buf); if (res < 0) { @@ -552,12 +546,12 @@ static void mmap_close(struct video_data *s) static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap) { struct video_data *s = s1->priv_data; - struct v4l2_input input; - struct v4l2_standard standard; + struct v4l2_input input = { 0 }; + struct v4l2_standard standard = { 0 }; struct v4l2_streamparm streamparm = { 0 }; struct v4l2_fract *tpf = &streamparm.parm.capture.timeperframe; + AVRational framerate_q = { 0 }; int i, ret; - AVRational framerate_q; streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; @@ -569,7 +563,6 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap) } /* set tv video input */ - memset (&input, 0, sizeof (input)); input.index = s->channel; if (ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) { av_log(s1, AV_LOG_ERROR, "The V4L2 driver ioctl enum input failed:\n"); @@ -589,7 +582,6 @@ static int v4l2_set_parameters(AVFormatContext *s1, AVFormatParameters *ap) av_log(s1, AV_LOG_DEBUG, "The V4L2 driver set standard: %s\n", s->standard); /* set tv standard */ - memset (&standard, 0, sizeof (standard)); for(i=0;;i++) { standard.index = i; if (ioctl(s->fd, VIDIOC_ENUMSTD, &standard) < 0) { From 0c50edb789c6489aad477d19bab5b06b01a36d91 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Sun, 8 Jan 2012 17:49:43 +0100 Subject: [PATCH 05/22] doc: fix stray reference to FFmpeg --- doc/filters.texi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/filters.texi b/doc/filters.texi index e022b46531..4e7ede23a4 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -470,7 +470,7 @@ drawbox=10:20:200:60:red@@0.5" Draw text string or text from specified file on top of video using the libfreetype library. -To enable compilation of this filter you need to configure FFmpeg with +To enable compilation of this filter you need to configure Libav with @code{--enable-libfreetype}. The filter also recognizes strftime() sequences in the provided text From 0b8b3387a977dcdb6fb9e53bcc9966d34b2e4cec Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Tue, 10 Jan 2012 02:54:34 +0000 Subject: [PATCH 06/22] rgb2rgb: allow conversion for <15 bpp Signed-off-by: Ronald S. Bultje --- libswscale/swscale_unscaled.c | 62 +++++++++++++++++------------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c index 7c339b6c05..57695483ea 100644 --- a/libswscale/swscale_unscaled.c +++ b/libswscale/swscale_unscaled.c @@ -360,8 +360,8 @@ static int rgbToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[], const enum PixelFormat dstFormat = c->dstFormat; const int srcBpp = (c->srcFormatBpp + 7) >> 3; const int dstBpp = (c->dstFormatBpp + 7) >> 3; - const int srcId = c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */ - const int dstId = c->dstFormatBpp >> 2; + const int srcId = c->srcFormatBpp; + const int dstId = c->dstFormatBpp; void (*conv)(const uint8_t *src, uint8_t *dst, int src_size) = NULL; #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst) @@ -383,38 +383,38 @@ static int rgbToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[], /* BGR -> BGR */ if ((isBGRinInt(srcFormat) && isBGRinInt(dstFormat)) || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) { - switch (srcId | (dstId << 4)) { - case 0x34: conv = rgb16to15; break; - case 0x36: conv = rgb24to15; break; - case 0x38: conv = rgb32to15; break; - case 0x43: conv = rgb15to16; break; - case 0x46: conv = rgb24to16; break; - case 0x48: conv = rgb32to16; break; - case 0x63: conv = rgb15to24; break; - case 0x64: conv = rgb16to24; break; - case 0x68: conv = rgb32to24; break; - case 0x83: conv = rgb15to32; break; - case 0x84: conv = rgb16to32; break; - case 0x86: conv = rgb24to32; break; + switch (srcId | (dstId << 16)) { + case 0x000F0010: conv = rgb16to15; break; + case 0x000F0018: conv = rgb24to15; break; + case 0x000F0020: conv = rgb32to15; break; + case 0x0010000F: conv = rgb15to16; break; + case 0x00100018: conv = rgb24to16; break; + case 0x00100020: conv = rgb32to16; break; + case 0x0018000F: conv = rgb15to24; break; + case 0x00180010: conv = rgb16to24; break; + case 0x00180020: conv = rgb32to24; break; + case 0x0020000F: conv = rgb15to32; break; + case 0x00200010: conv = rgb16to32; break; + case 0x00200018: conv = rgb24to32; break; } } else if ((isBGRinInt(srcFormat) && isRGBinInt(dstFormat)) || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) { - switch (srcId | (dstId << 4)) { - case 0x33: conv = rgb15tobgr15; break; - case 0x34: conv = rgb16tobgr15; break; - case 0x36: conv = rgb24tobgr15; break; - case 0x38: conv = rgb32tobgr15; break; - case 0x43: conv = rgb15tobgr16; break; - case 0x44: conv = rgb16tobgr16; break; - case 0x46: conv = rgb24tobgr16; break; - case 0x48: conv = rgb32tobgr16; break; - case 0x63: conv = rgb15tobgr24; break; - case 0x64: conv = rgb16tobgr24; break; - case 0x66: conv = rgb24tobgr24; break; - case 0x68: conv = rgb32tobgr24; break; - case 0x83: conv = rgb15tobgr32; break; - case 0x84: conv = rgb16tobgr32; break; - case 0x86: conv = rgb24tobgr32; break; + switch (srcId | (dstId << 16)) { + case 0x000F000F: conv = rgb15tobgr15; break; + case 0x000F0010: conv = rgb16tobgr15; break; + case 0x000F0018: conv = rgb24tobgr15; break; + case 0x000F0020: conv = rgb32tobgr15; break; + case 0x0010000F: conv = rgb15tobgr16; break; + case 0x00100010: conv = rgb16tobgr16; break; + case 0x00100018: conv = rgb24tobgr16; break; + case 0x00100020: conv = rgb32tobgr16; break; + case 0x0018000F: conv = rgb15tobgr24; break; + case 0x00180010: conv = rgb16tobgr24; break; + case 0x00180018: conv = rgb24tobgr24; break; + case 0x00180020: conv = rgb32tobgr24; break; + case 0x0020000F: conv = rgb15tobgr32; break; + case 0x00200010: conv = rgb16tobgr32; break; + case 0x00200018: conv = rgb24tobgr32; break; } } From 353a2d2164c09740e42f33014c4773b93e96a0d2 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Mon, 9 Jan 2012 23:37:24 +0000 Subject: [PATCH 07/22] bmpdec: support for rgb444 with bitfields compression Do not display garbage for invalid/unsupported bitfields values. Signed-off-by: Ronald S. Bultje --- libavcodec/bmp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/libavcodec/bmp.c b/libavcodec/bmp.c index f438d10613..1f725f5369 100644 --- a/libavcodec/bmp.c +++ b/libavcodec/bmp.c @@ -162,8 +162,18 @@ static int bmp_decode_frame(AVCodecContext *avctx, case 16: if(comp == BMP_RGB) avctx->pix_fmt = PIX_FMT_RGB555; - if(comp == BMP_BITFIELDS) - avctx->pix_fmt = rgb[1] == 0x07E0 ? PIX_FMT_RGB565 : PIX_FMT_RGB555; + else if (comp == BMP_BITFIELDS) { + if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F) + avctx->pix_fmt = PIX_FMT_RGB565; + else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F) + avctx->pix_fmt = PIX_FMT_RGB555; + else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F) + avctx->pix_fmt = PIX_FMT_RGB444; + else { + av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]); + return AVERROR(EINVAL); + } + } break; case 8: if(hsize - ihsize - 14 > 0) From a78dbada55d6b467dd560e0780f6bc50f72ba21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 9 Jan 2012 18:35:01 +0200 Subject: [PATCH 08/22] movenc: Don't store a nonzero creation time if nothing was set by the caller MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the creation time is stored in the file as a zero, the mov demuxer skips exporting the creation time. Currently, files muxed without a creation time get demuxed with a Jan 1st 1970 creation timestamp. Signed-off-by: Martin Storsjö --- libavformat/movenc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libavformat/movenc.c b/libavformat/movenc.c index e11eb509d8..85b5667076 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -2317,7 +2317,8 @@ static int mov_write_header(AVFormatContext *s) #endif if (t = av_dict_get(s->metadata, "creation_time", NULL, 0)) mov->time = ff_iso8601_to_unix_time(t->value); - mov->time += 0x7C25B080; //1970 based -> 1904 based + if (mov->time) + mov->time += 0x7C25B080; // 1970 based -> 1904 based if (mov->chapter_track) mov_create_chapter_track(s, mov->chapter_track); From 5c7c9a9f3354164c793baf7796c8ed432a184b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Tue, 10 Jan 2012 16:37:40 +0200 Subject: [PATCH 09/22] fate: Update file checksums after the mov muxer change in a78dbada55d6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Storsjö --- tests/ref/acodec/alac | 2 +- tests/ref/acodec/pcm_s16be | 2 +- tests/ref/acodec/pcm_s24be | 2 +- tests/ref/acodec/pcm_s32be | 2 +- tests/ref/acodec/pcm_s8 | 2 +- tests/ref/lavf/mov | 2 +- tests/ref/vsynth1/dnxhd_1080i | 2 +- tests/ref/vsynth1/mpeg4 | 2 +- tests/ref/vsynth1/qtrle | 2 +- tests/ref/vsynth1/svq1 | 2 +- tests/ref/vsynth2/dnxhd_1080i | 2 +- tests/ref/vsynth2/mpeg4 | 2 +- tests/ref/vsynth2/qtrle | 2 +- tests/ref/vsynth2/svq1 | 2 +- 14 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/ref/acodec/alac b/tests/ref/acodec/alac index 15d1a1fa41..cef12d0744 100644 --- a/tests/ref/acodec/alac +++ b/tests/ref/acodec/alac @@ -1,4 +1,4 @@ -8d9cb7f65c5b17c74e5f9bdc36f32b7d *./tests/data/acodec/alac.m4a +db1806d9ffd85c168c2c71a28e6d9229 *./tests/data/acodec/alac.m4a 389410 ./tests/data/acodec/alac.m4a 64151e4bcc2b717aa5a8454d424d6a1f *./tests/data/alac.acodec.out.wav stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 1058400/ 1058400 diff --git a/tests/ref/acodec/pcm_s16be b/tests/ref/acodec/pcm_s16be index aea4c98544..f7666660fa 100644 --- a/tests/ref/acodec/pcm_s16be +++ b/tests/ref/acodec/pcm_s16be @@ -1,4 +1,4 @@ -dd832e23156643becce8e9d2c24cb31d *./tests/data/acodec/pcm_s16be.mov +53c9eb319c778e7ce137667f62384994 *./tests/data/acodec/pcm_s16be.mov 1060073 ./tests/data/acodec/pcm_s16be.mov 64151e4bcc2b717aa5a8454d424d6a1f *./tests/data/pcm_s16be.acodec.out.wav stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 1058400/ 1058400 diff --git a/tests/ref/acodec/pcm_s24be b/tests/ref/acodec/pcm_s24be index 4407af3efb..b9fada7e76 100644 --- a/tests/ref/acodec/pcm_s24be +++ b/tests/ref/acodec/pcm_s24be @@ -1,4 +1,4 @@ -1b570c296bce03e36e1dfb369190ffb6 *./tests/data/acodec/pcm_s24be.mov +af8acd2f08e4bbebe7f4bea4d6f59dd6 *./tests/data/acodec/pcm_s24be.mov 1589273 ./tests/data/acodec/pcm_s24be.mov 64151e4bcc2b717aa5a8454d424d6a1f *./tests/data/pcm_s24be.acodec.out.wav stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 1058400/ 1058400 diff --git a/tests/ref/acodec/pcm_s32be b/tests/ref/acodec/pcm_s32be index fc8fb5e7d3..d6e5205832 100644 --- a/tests/ref/acodec/pcm_s32be +++ b/tests/ref/acodec/pcm_s32be @@ -1,4 +1,4 @@ -249c2ca88e2d8cdaed345e3d446e5bc3 *./tests/data/acodec/pcm_s32be.mov +63f0e22b4f7c5d61d75047d85f140d52 *./tests/data/acodec/pcm_s32be.mov 2118473 ./tests/data/acodec/pcm_s32be.mov 64151e4bcc2b717aa5a8454d424d6a1f *./tests/data/pcm_s32be.acodec.out.wav stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 1058400/ 1058400 diff --git a/tests/ref/acodec/pcm_s8 b/tests/ref/acodec/pcm_s8 index 01c2240a06..3b550d2916 100644 --- a/tests/ref/acodec/pcm_s8 +++ b/tests/ref/acodec/pcm_s8 @@ -1,4 +1,4 @@ -f467f8899b2bd11c736d0f4e61efb1c4 *./tests/data/acodec/pcm_s8.mov +4b3013a3f3c328ecdb617cd88b3fe836 *./tests/data/acodec/pcm_s8.mov 530873 ./tests/data/acodec/pcm_s8.mov 651d4eb8d98dfcdda96ae6c43d8f156b *./tests/data/pcm_s8.acodec.out.wav stddev: 147.89 PSNR: 52.93 MAXDIFF: 255 bytes: 1058400/ 1058400 diff --git a/tests/ref/lavf/mov b/tests/ref/lavf/mov index f51b42d74a..a4ae2d5532 100644 --- a/tests/ref/lavf/mov +++ b/tests/ref/lavf/mov @@ -1,3 +1,3 @@ -8dc82a08a0abb47c822d03a6e408383b *./tests/data/lavf/lavf.mov +6c5472152b46e070ae6da359838e1f86 *./tests/data/lavf/lavf.mov 357717 ./tests/data/lavf/lavf.mov ./tests/data/lavf/lavf.mov CRC=0x2f6a9b26 diff --git a/tests/ref/vsynth1/dnxhd_1080i b/tests/ref/vsynth1/dnxhd_1080i index 80484b50ae..e989eae2a0 100644 --- a/tests/ref/vsynth1/dnxhd_1080i +++ b/tests/ref/vsynth1/dnxhd_1080i @@ -1,4 +1,4 @@ -34949ea38da2cf6a8406ad600ad95cfa *./tests/data/vsynth1/dnxhd-1080i.mov +3cfbe36a7dd5b48859b8a569d626ef77 *./tests/data/vsynth1/dnxhd-1080i.mov 3031875 ./tests/data/vsynth1/dnxhd-1080i.mov 0c651e840f860592f0d5b66030d9fa32 *./tests/data/dnxhd_1080i.vsynth1.out.yuv stddev: 6.29 PSNR: 32.15 MAXDIFF: 64 bytes: 760320/ 7603200 diff --git a/tests/ref/vsynth1/mpeg4 b/tests/ref/vsynth1/mpeg4 index 133e228ff4..b318e6f4de 100644 --- a/tests/ref/vsynth1/mpeg4 +++ b/tests/ref/vsynth1/mpeg4 @@ -1,4 +1,4 @@ -9251145d12150cb639098016d61fc75e *./tests/data/vsynth1/odivx.mp4 +59a9e2eed314abface66aaf1b45eb8f2 *./tests/data/vsynth1/odivx.mp4 540180 ./tests/data/vsynth1/odivx.mp4 8828a375448dc5c2215163ba70656f89 *./tests/data/mpeg4.vsynth1.out.yuv stddev: 7.97 PSNR: 30.10 MAXDIFF: 105 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth1/qtrle b/tests/ref/vsynth1/qtrle index 9988897b91..002ee49883 100644 --- a/tests/ref/vsynth1/qtrle +++ b/tests/ref/vsynth1/qtrle @@ -1,4 +1,4 @@ -d14041925ce5ec5001dc519276b1a1ab *./tests/data/vsynth1/qtrle.mov +7d75328a17e04796a39fe9be3a322946 *./tests/data/vsynth1/qtrle.mov 15263232 ./tests/data/vsynth1/qtrle.mov 243325fb2cae1a9245efd49aff936327 *./tests/data/qtrle.vsynth1.out.yuv stddev: 3.42 PSNR: 37.43 MAXDIFF: 48 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth1/svq1 b/tests/ref/vsynth1/svq1 index 8c647c7870..3137e3bd9d 100644 --- a/tests/ref/vsynth1/svq1 +++ b/tests/ref/vsynth1/svq1 @@ -1,4 +1,4 @@ -595fc4e38734521356b60e67b813f0fa *./tests/data/vsynth1/svq1.mov +5c9d8734693f3cab57f61e76b5b6da7d *./tests/data/vsynth1/svq1.mov 1334367 ./tests/data/vsynth1/svq1.mov 9cc35c54b2c77d36bd7e308b393c1f81 *./tests/data/svq1.vsynth1.out.yuv stddev: 9.58 PSNR: 28.50 MAXDIFF: 210 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth2/dnxhd_1080i b/tests/ref/vsynth2/dnxhd_1080i index ae988465f6..b9a6faca1b 100644 --- a/tests/ref/vsynth2/dnxhd_1080i +++ b/tests/ref/vsynth2/dnxhd_1080i @@ -1,4 +1,4 @@ -995e433cd076e3c1534fa73181744a84 *./tests/data/vsynth2/dnxhd-1080i.mov +19a91b7da35cecf41e5e3cb322485627 *./tests/data/vsynth2/dnxhd-1080i.mov 3031875 ./tests/data/vsynth2/dnxhd-1080i.mov 3c559af629ae0a8fb1a9a0e4b4da7733 *./tests/data/dnxhd_1080i.vsynth2.out.yuv stddev: 1.31 PSNR: 45.77 MAXDIFF: 23 bytes: 760320/ 7603200 diff --git a/tests/ref/vsynth2/mpeg4 b/tests/ref/vsynth2/mpeg4 index b89a3ec60a..8ccab866d5 100644 --- a/tests/ref/vsynth2/mpeg4 +++ b/tests/ref/vsynth2/mpeg4 @@ -1,4 +1,4 @@ -c2ca709a0ed64833fd38f703b19e5e85 *./tests/data/vsynth2/odivx.mp4 +8c9afbf564008a8ce6719cc3546deae1 *./tests/data/vsynth2/odivx.mp4 119833 ./tests/data/vsynth2/odivx.mp4 90a3577850239083a9042bef33c50e85 *./tests/data/mpeg4.vsynth2.out.yuv stddev: 5.34 PSNR: 33.57 MAXDIFF: 83 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth2/qtrle b/tests/ref/vsynth2/qtrle index 6b2a01168e..5dd0425a6f 100644 --- a/tests/ref/vsynth2/qtrle +++ b/tests/ref/vsynth2/qtrle @@ -1,4 +1,4 @@ -d8c1604dc46d9aa4ec0385e6722c6989 *./tests/data/vsynth2/qtrle.mov +4805f35ca6e03b9279cc18f3f7356366 *./tests/data/vsynth2/qtrle.mov 14798419 ./tests/data/vsynth2/qtrle.mov b2418e0e3a9a8619b31219cbcf24dc82 *./tests/data/qtrle.vsynth2.out.yuv stddev: 1.26 PSNR: 46.06 MAXDIFF: 13 bytes: 7603200/ 7603200 diff --git a/tests/ref/vsynth2/svq1 b/tests/ref/vsynth2/svq1 index 7c54c74ac9..8adca6456d 100644 --- a/tests/ref/vsynth2/svq1 +++ b/tests/ref/vsynth2/svq1 @@ -1,4 +1,4 @@ -7f9fbe4890bc1df67867bf03803dca48 *./tests/data/vsynth2/svq1.mov +138ad38281570f1a3b68d63ed896435d *./tests/data/vsynth2/svq1.mov 766851 ./tests/data/vsynth2/svq1.mov aa03471dac3f49455a33a2b19fda1098 *./tests/data/svq1.vsynth2.out.yuv stddev: 3.23 PSNR: 37.93 MAXDIFF: 61 bytes: 7603200/ 7603200 From 7b9373db89096dea65a206bb4637db61348776f2 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 10 Jan 2012 09:13:32 +0100 Subject: [PATCH 10/22] avconv: fix -copyinkf. This option only applies to streamcopy, but is currently processed only when encoding. --- avconv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/avconv.c b/avconv.c index 16f22f4039..d6860c64f3 100644 --- a/avconv.c +++ b/avconv.c @@ -3618,13 +3618,13 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc) ost->top_field_first = -1; MATCH_PER_STREAM_OPT(top_field_first, i, ost->top_field_first, oc, st); - MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc ,st); - #if CONFIG_AVFILTER MATCH_PER_STREAM_OPT(filters, str, filters, oc, st); if (filters) ost->avfilter = av_strdup(filters); #endif + } else { + MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc ,st); } return ost; From 5d25140f714cbbf72e82e95c61a97353e75e6f90 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 3 Jan 2012 11:18:00 +0100 Subject: [PATCH 11/22] vsrc_buffer: error on changing frame parameters. --- libavfilter/vsrc_buffer.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libavfilter/vsrc_buffer.c b/libavfilter/vsrc_buffer.c index 91465026c2..178b3e68bb 100644 --- a/libavfilter/vsrc_buffer.c +++ b/libavfilter/vsrc_buffer.c @@ -36,6 +36,12 @@ typedef struct { AVRational pixel_aspect; } BufferSourceContext; +#define CHECK_PARAM_CHANGE(s, c, width, height, format)\ + if (c->w != width || c->h != height || c->pix_fmt != format) {\ + av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\ + return AVERROR(EINVAL);\ + } + int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame, int64_t pts, AVRational pixel_aspect) { @@ -49,6 +55,8 @@ int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame, //return -1; } + CHECK_PARAM_CHANGE(buffer_filter, c, frame->width, frame->height, frame->format); + c->buf = avfilter_get_video_buffer(buffer_filter->outputs[0], AV_PERM_WRITE, c->w, c->h); av_image_copy(c->buf->data, c->buf->linesize, frame->data, frame->linesize, @@ -73,6 +81,8 @@ int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf) return AVERROR(EINVAL); } + CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format); + c->buf = buf; return 0; From ac646076826bf5ec36356fdda2826a5663eec303 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Mon, 2 Jan 2012 15:02:46 +0100 Subject: [PATCH 12/22] avconv: reinitialize the filtergraph on resolution change. This is a hopefully temporary solution until lavfi can properly deal with resolution changes. --- avconv.c | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/avconv.c b/avconv.c index d6860c64f3..c96cc73280 100644 --- a/avconv.c +++ b/avconv.c @@ -608,7 +608,6 @@ static int configure_video_filters(InputStream *ist, OutputStream *ost) if ((ret = avfilter_graph_parse(ost->graph, ost->avfilter, inputs, outputs, NULL)) < 0) return ret; - av_freep(&ost->avfilter); } else { if ((ret = avfilter_link(last_filter, 0, ost->output_video_filter, 0)) < 0) return ret; @@ -680,6 +679,10 @@ void exit_program(int ret) bsfc = next; } output_streams[i].bitstream_filters = NULL; + +#if CONFIG_AVFILTER + av_freep(&output_streams[i].avfilter); +#endif } for (i = 0; i < nb_input_files; i++) { avformat_close_input(&input_files[i].ctx); @@ -1252,6 +1255,7 @@ static void do_subtitle_out(AVFormatContext *s, static int bit_buffer_size = 1024 * 256; static uint8_t *bit_buffer = NULL; +#if !CONFIG_AVFILTER static void do_video_resample(OutputStream *ost, InputStream *ist, AVFrame *in_picture, @@ -1275,7 +1279,6 @@ static void do_video_resample(OutputStream *ost, ost->video_resample = 1; } -#if !CONFIG_AVFILTER if (ost->video_resample) { *out_picture = &ost->pict_tmp; if (resample_changed) { @@ -1297,21 +1300,13 @@ static void do_video_resample(OutputStream *ost, sws_scale(ost->img_resample_ctx, in_picture->data, in_picture->linesize, 0, ost->resample_height, (*out_picture)->data, (*out_picture)->linesize); } -#else - if (resample_changed) { - avfilter_graph_free(&ost->graph); - if (configure_video_filters(ist, ost)) { - av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n"); - exit_program(1); - } - } -#endif if (resample_changed) { ost->resample_width = dec->width; ost->resample_height = dec->height; ost->resample_pix_fmt = dec->pix_fmt; } } +#endif static void do_video_out(AVFormatContext *s, @@ -1366,7 +1361,11 @@ static void do_video_out(AVFormatContext *s, if (nb_frames <= 0) return; +#if !CONFIG_AVFILTER do_video_resample(ost, ist, in_picture, &final_picture); +#else + final_picture = in_picture; +#endif /* duplicates frame if needed */ for (i = 0; i < nb_frames; i++) { @@ -1936,12 +1935,33 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = &output_streams[i]; - int frame_size; + int frame_size, resample_changed; if (!check_output_constraints(ist, ost) || !ost->encoding_needed) continue; #if CONFIG_AVFILTER + resample_changed = ost->resample_width != decoded_frame->width || + ost->resample_height != decoded_frame->height || + ost->resample_pix_fmt != decoded_frame->format; + if (resample_changed) { + av_log(NULL, AV_LOG_INFO, + "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n", + ist->file_index, ist->st->index, + ost->resample_width, ost->resample_height, av_get_pix_fmt_name(ost->resample_pix_fmt), + decoded_frame->width, decoded_frame->height, av_get_pix_fmt_name(decoded_frame->format)); + + avfilter_graph_free(&ost->graph); + if (configure_video_filters(ist, ost)) { + av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n"); + exit_program(1); + } + + ost->resample_width = decoded_frame->width; + ost->resample_height = decoded_frame->height; + ost->resample_pix_fmt = decoded_frame->format; + } + if (ist->st->sample_aspect_ratio.num) decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio; if (ist->st->codec->codec->capabilities & CODEC_CAP_DR1) { From e652cc9606068189cb512a36f0335a5cf2ecf287 Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Mon, 9 Jan 2012 02:06:59 +0100 Subject: [PATCH 13/22] rv34: use get_bits_left() It is not necessary to store the bit stream length in a RV34DecContext field. --- libavcodec/rv34.c | 3 +-- libavcodec/rv34.h | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c index 91d678876a..f9f8677385 100644 --- a/libavcodec/rv34.c +++ b/libavcodec/rv34.c @@ -1186,7 +1186,7 @@ static int check_slice_end(RV34DecContext *r, MpegEncContext *s) return 1; if(r->s.mb_skip_run > 1) return 0; - bits = r->bits - get_bits_count(&s->gb); + bits = get_bits_left(&s->gb); if(bits < 0 || (bits < 8 && !show_bits(&s->gb, bits))) return 1; return 0; @@ -1266,7 +1266,6 @@ static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int r->si.end = end; s->qscale = r->si.quant; - r->bits = buf_size*8; s->mb_num_left = r->si.end - r->si.start; r->s.mb_skip_run = 0; diff --git a/libavcodec/rv34.h b/libavcodec/rv34.h index 12607fb806..3f3b879306 100644 --- a/libavcodec/rv34.h +++ b/libavcodec/rv34.h @@ -92,7 +92,6 @@ typedef struct RV34DecContext{ const uint8_t *luma_dc_quant_p;///< luma subblock DC quantizer for interframes RV34VLC *cur_vlcs; ///< VLC set used for current frame decoding - int bits; ///< slice size in bits H264PredContext h; ///< functions for 4x4 and 16x16 intra block prediction SliceInfo si; ///< current slice information From cd44521625d0e2ada0f2dc82eee0707ab758d31e Mon Sep 17 00:00:00 2001 From: Oka Motofumi Date: Tue, 10 Jan 2012 06:39:04 -0800 Subject: [PATCH 14/22] swscale: specify register type. Fixes a compilation failure on win64. Signed-off-by: Ronald S. Bultje --- libswscale/x86/input.asm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libswscale/x86/input.asm b/libswscale/x86/input.asm index 4bdb575765..a23cf05301 100644 --- a/libswscale/x86/input.asm +++ b/libswscale/x86/input.asm @@ -135,7 +135,7 @@ cglobal %2ToY, 3, 3, %1, dst, src, w %macro YUYV_TO_UV_FN 2-3 cglobal %2ToUV, 3, 4, %1, dstU, dstV, src, w %ifdef ARCH_X86_64 - movsxd wq, r4m + movsxd wq, dword r4m %else ; x86-32 mov wq, r4m %endif @@ -190,7 +190,7 @@ cglobal %2ToUV, 3, 4, %1, dstU, dstV, src, w %macro NVXX_TO_UV_FN 2 cglobal %2ToUV, 3, 4, %1, dstU, dstV, src, w %ifdef ARCH_X86_64 - movsxd wq, r4m + movsxd wq, dword r4m %else ; x86-32 mov wq, r4m %endif From b14fa5572c2a3bb1d8cd6327c4687a2eee363bbb Mon Sep 17 00:00:00 2001 From: "Ronald S. Bultje" Date: Sun, 20 Nov 2011 15:54:15 -0800 Subject: [PATCH 15/22] swscale: fix crash in fast_bilinear code when compiled with -mred-zone. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Additional comments from Måns Rullgard have been integrated by Reinhard Tartler. Signed-off-by: Reinhard Tartler --- libswscale/x86/swscale_template.c | 50 ++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/libswscale/x86/swscale_template.c b/libswscale/x86/swscale_template.c index b3d7336588..e38f58b5d0 100644 --- a/libswscale/x86/swscale_template.c +++ b/libswscale/x86/swscale_template.c @@ -1513,12 +1513,24 @@ static void RENAME(hyscale_fast)(SwsContext *c, int16_t *dst, void *mmx2FilterCode= c->lumMmx2FilterCode; int i; #if defined(PIC) - DECLARE_ALIGNED(8, uint64_t, ebxsave); + uint64_t ebxsave; +#endif +#if ARCH_X86_64 + uint64_t retsave; #endif __asm__ volatile( #if defined(PIC) "mov %%"REG_b", %5 \n\t" +#if ARCH_X86_64 + "mov -8(%%rsp), %%"REG_a" \n\t" + "mov %%"REG_a", %6 \n\t" +#endif +#else +#if ARCH_X86_64 + "mov -8(%%rsp), %%"REG_a" \n\t" + "mov %%"REG_a", %5 \n\t" +#endif #endif "pxor %%mm7, %%mm7 \n\t" "mov %0, %%"REG_c" \n\t" @@ -1560,11 +1572,23 @@ static void RENAME(hyscale_fast)(SwsContext *c, int16_t *dst, #if defined(PIC) "mov %5, %%"REG_b" \n\t" +#if ARCH_X86_64 + "mov %6, %%"REG_a" \n\t" + "mov %%"REG_a", -8(%%rsp) \n\t" +#endif +#else +#if ARCH_X86_64 + "mov %5, %%"REG_a" \n\t" + "mov %%"REG_a", -8(%%rsp) \n\t" +#endif #endif :: "m" (src), "m" (dst), "m" (filter), "m" (filterPos), "m" (mmx2FilterCode) #if defined(PIC) ,"m" (ebxsave) +#endif +#if ARCH_X86_64 + ,"m"(retsave) #endif : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D #if !defined(PIC) @@ -1587,10 +1611,22 @@ static void RENAME(hcscale_fast)(SwsContext *c, int16_t *dst1, int16_t *dst2, #if defined(PIC) DECLARE_ALIGNED(8, uint64_t, ebxsave); #endif +#if ARCH_X86_64 + DECLARE_ALIGNED(8, uint64_t, retsave); +#endif __asm__ volatile( #if defined(PIC) "mov %%"REG_b", %7 \n\t" +#if ARCH_X86_64 + "mov -8(%%rsp), %%"REG_a" \n\t" + "mov %%"REG_a", %8 \n\t" +#endif +#else +#if ARCH_X86_64 + "mov -8(%%rsp), %%"REG_a" \n\t" + "mov %%"REG_a", %7 \n\t" +#endif #endif "pxor %%mm7, %%mm7 \n\t" "mov %0, %%"REG_c" \n\t" @@ -1620,11 +1656,23 @@ static void RENAME(hcscale_fast)(SwsContext *c, int16_t *dst1, int16_t *dst2, #if defined(PIC) "mov %7, %%"REG_b" \n\t" +#if ARCH_X86_64 + "mov %8, %%"REG_a" \n\t" + "mov %%"REG_a", -8(%%rsp) \n\t" +#endif +#else +#if ARCH_X86_64 + "mov %7, %%"REG_a" \n\t" + "mov %%"REG_a", -8(%%rsp) \n\t" +#endif #endif :: "m" (src1), "m" (dst1), "m" (filter), "m" (filterPos), "m" (mmx2FilterCode), "m" (src2), "m"(dst2) #if defined(PIC) ,"m" (ebxsave) +#endif +#if ARCH_X86_64 + ,"m"(retsave) #endif : "%"REG_a, "%"REG_c, "%"REG_d, "%"REG_S, "%"REG_D #if !defined(PIC) From 84e5159e25d2fd35bdceef4678c199e27d1b88ce Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Tue, 10 Jan 2012 16:06:02 +0000 Subject: [PATCH 16/22] bmpenc: support for PIX_FMT_RGB444 Signed-off-by: Ronald S. Bultje --- libavcodec/bmpenc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libavcodec/bmpenc.c b/libavcodec/bmpenc.c index 9cd7adb390..ca2951ab37 100644 --- a/libavcodec/bmpenc.c +++ b/libavcodec/bmpenc.c @@ -27,6 +27,7 @@ static const uint32_t monoblack_pal[] = { 0x000000, 0xFFFFFF }; static const uint32_t rgb565_masks[] = { 0xF800, 0x07E0, 0x001F }; +static const uint32_t rgb444_masks[] = { 0x0F00, 0x00F0, 0x000F }; static av_cold int bmp_encode_init(AVCodecContext *avctx){ BMPContext *s = avctx->priv_data; @@ -39,9 +40,8 @@ static av_cold int bmp_encode_init(AVCodecContext *avctx){ avctx->bits_per_coded_sample = 24; break; case PIX_FMT_RGB555: - avctx->bits_per_coded_sample = 16; - break; case PIX_FMT_RGB565: + case PIX_FMT_RGB444: avctx->bits_per_coded_sample = 16; break; case PIX_FMT_RGB8: @@ -77,6 +77,11 @@ static int bmp_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_s p->pict_type= AV_PICTURE_TYPE_I; p->key_frame= 1; switch (avctx->pix_fmt) { + case PIX_FMT_RGB444: + compression = BMP_BITFIELDS; + pal = rgb444_masks; // abuse pal to hold color masks + pal_entries = 3; + break; case PIX_FMT_RGB565: compression = BMP_BITFIELDS; pal = rgb565_masks; // abuse pal to hold color masks @@ -158,7 +163,7 @@ AVCodec ff_bmp_encoder = { .encode = bmp_encode_frame, .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_BGR24, - PIX_FMT_RGB555, PIX_FMT_RGB565, + PIX_FMT_RGB555, PIX_FMT_RGB444, PIX_FMT_RGB565, PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8, PIX_FMT_MONOBLACK, PIX_FMT_NONE}, From 29112db8c0f65886e69cbbd6f4e5c44d2d14d238 Mon Sep 17 00:00:00 2001 From: Aneesh Dogra Date: Tue, 10 Jan 2012 23:38:03 +0530 Subject: [PATCH 17/22] bethsoftvideo: Use bytestream2 functions to prevent buffer overreads. Signed-off-by: Ronald S. Bultje --- libavcodec/bethsoftvideo.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/libavcodec/bethsoftvideo.c b/libavcodec/bethsoftvideo.c index f4020d6ee5..fa0457cc66 100644 --- a/libavcodec/bethsoftvideo.c +++ b/libavcodec/bethsoftvideo.c @@ -34,6 +34,7 @@ typedef struct BethsoftvidContext { AVFrame frame; + GetByteContext g; } BethsoftvidContext; static av_cold int bethsoftvid_decode_init(AVCodecContext *avctx) @@ -46,18 +47,18 @@ static av_cold int bethsoftvid_decode_init(AVCodecContext *avctx) return 0; } -static int set_palette(AVFrame * frame, const uint8_t * palette_buffer, int buf_size) +static int set_palette(BethsoftvidContext *ctx) { - uint32_t * palette = (uint32_t *)frame->data[1]; + uint32_t *palette = (uint32_t *)ctx->frame.data[1]; int a; - if (buf_size < 256*3) + if (bytestream2_get_bytes_left(&ctx->g) < 256*3) return AVERROR_INVALIDDATA; for(a = 0; a < 256; a++){ - palette[a] = AV_RB24(&palette_buffer[a * 3]) * 4; + palette[a] = bytestream2_get_be24u(&ctx->g) * 4; } - frame->palette_has_changed = 1; + ctx->frame.palette_has_changed = 1; return 256*3; } @@ -65,8 +66,6 @@ static int bethsoftvid_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) { - const uint8_t *buf = avpkt->data; - int buf_size = avpkt->size; BethsoftvidContext * vid = avctx->priv_data; char block_type; uint8_t * dst; @@ -80,29 +79,32 @@ static int bethsoftvid_decode_frame(AVCodecContext *avctx, av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); return -1; } + + bytestream2_init(&vid->g, avpkt->data, avpkt->size); dst = vid->frame.data[0]; frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height; - switch(block_type = *buf++){ - case PALETTE_BLOCK: - return set_palette(&vid->frame, buf, buf_size); + switch(block_type = bytestream2_get_byte(&vid->g)){ + case PALETTE_BLOCK: { + return set_palette(vid); + } case VIDEO_YOFF_P_FRAME: - yoffset = bytestream_get_le16(&buf); + yoffset = bytestream2_get_le16(&vid->g); if(yoffset >= avctx->height) return -1; dst += vid->frame.linesize[0] * yoffset; } // main code - while((code = *buf++)){ + while((code = bytestream2_get_byte(&vid->g))){ int length = code & 0x7f; // copy any bytes starting at the current position, and ending at the frame width while(length > remaining){ if(code < 0x80) - bytestream_get_buffer(&buf, dst, remaining); + bytestream2_get_buffer(&vid->g, dst, remaining); else if(block_type == VIDEO_I_FRAME) - memset(dst, buf[0], remaining); + memset(dst, bytestream2_peek_byte(&vid->g), remaining); length -= remaining; // decrement the number of bytes to be copied dst += remaining + wrap_to_next_line; // skip over extra bytes at end of frame remaining = avctx->width; @@ -112,9 +114,9 @@ static int bethsoftvid_decode_frame(AVCodecContext *avctx, // copy any remaining bytes after / if line overflows if(code < 0x80) - bytestream_get_buffer(&buf, dst, length); + bytestream2_get_buffer(&vid->g, dst, length); else if(block_type == VIDEO_I_FRAME) - memset(dst, *buf++, length); + memset(dst, bytestream2_get_byte(&vid->g), length); remaining -= length; dst += length; } @@ -123,7 +125,7 @@ static int bethsoftvid_decode_frame(AVCodecContext *avctx, *data_size = sizeof(AVFrame); *(AVFrame*)data = vid->frame; - return buf_size; + return avpkt->size; } static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx) From e9626eb32e8d1e1ca4ebfb3e3b02cb3f03f5625c Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Tue, 10 Jan 2012 09:24:21 -0500 Subject: [PATCH 18/22] sierravmd: fix audio pts The duration of the first packet was being calculated incorrectly, leading to an incorrect timestamp offset. --- libavformat/sierravmd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/sierravmd.c b/libavformat/sierravmd.c index 1b5f04b393..81ff46fea0 100644 --- a/libavformat/sierravmd.c +++ b/libavformat/sierravmd.c @@ -206,7 +206,7 @@ static int vmd_read_header(AVFormatContext *s, vmd->frame_table[total_frames].pts = current_audio_pts; total_frames++; if(!current_audio_pts) - current_audio_pts += sound_buffers; + current_audio_pts += sound_buffers - 1; else current_audio_pts++; break; From 4556ebfb7d13f08eb8f794ac4fdfc04c89c46ece Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Tue, 10 Jan 2012 08:48:23 -0500 Subject: [PATCH 19/22] ipmovie: fix pts for CODEC_ID_INTERPLAY_DPCM frame sample count calculation was incorrect --- libavformat/ipmovie.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavformat/ipmovie.c b/libavformat/ipmovie.c index b91196b34c..daa44b173f 100644 --- a/libavformat/ipmovie.c +++ b/libavformat/ipmovie.c @@ -144,7 +144,7 @@ static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb, (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8)); else s->audio_frame_count += - (s->audio_chunk_size - 6) / s->audio_channels; + (s->audio_chunk_size - 6 - s->audio_channels) / s->audio_channels; av_dlog(NULL, "sending audio frame with pts %"PRId64" (%d audio frames)\n", pkt->pts, s->audio_frame_count); From 17ce52912f59a74ecc265e062578fb1181456e18 Mon Sep 17 00:00:00 2001 From: Alex Converse Date: Tue, 10 Jan 2012 13:07:09 -0800 Subject: [PATCH 20/22] aacsbr: prevent out of bounds memcpy(). Fixes Libav Bug 195. This doesn't make the code handle sample rate or upsample/downsample change properly but this is still a good sanity check. Based on change by Michael Niedermayer. Signed-off-by: Alex Converse --- libavcodec/aacsbr.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libavcodec/aacsbr.c b/libavcodec/aacsbr.c index 81b0b4c001..0bfcabb06b 100644 --- a/libavcodec/aacsbr.c +++ b/libavcodec/aacsbr.c @@ -1181,14 +1181,15 @@ static void sbr_qmf_synthesis(DSPContext *dsp, FFTContext *mdct, { int i, n; const float *sbr_qmf_window = div ? sbr_qmf_window_ds : sbr_qmf_window_us; + const int step = 128 >> div; float *v; for (i = 0; i < 32; i++) { - if (*v_off == 0) { + if (*v_off < step) { int saved_samples = (1280 - 128) >> div; memcpy(&v0[SBR_SYNTHESIS_BUF_SIZE - saved_samples], v0, saved_samples * sizeof(float)); - *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - (128 >> div); + *v_off = SBR_SYNTHESIS_BUF_SIZE - saved_samples - step; } else { - *v_off -= 128 >> div; + *v_off -= step; } v = v0 + *v_off; if (div) { From 3ab770001817e0f52114a9876819f07fcd8ed93a Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Tue, 10 Jan 2012 15:08:20 +0100 Subject: [PATCH 21/22] mpegvideo: claim ownership of referenced pictures Under certain conditions pictures could be released before they were returned with frame-threading. Broken mv computation in the upcoming rv34 frame-threading patch was caused by this. To prevent contexts from running out of available pictures the loop releasing "unused" pictures has to be run for B frames too. --- libavcodec/mpegvideo.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index e4c45886dc..f711d36aec 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -1170,25 +1170,26 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) s->codec_id == CODEC_ID_SVQ3); /* mark & release old frames */ - if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && - s->last_picture_ptr != s->next_picture_ptr && - s->last_picture_ptr->f.data[0]) { - if (s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3) { + if (s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3) { + if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && + s->last_picture_ptr != s->next_picture_ptr && + s->last_picture_ptr->f.data[0]) { if (s->last_picture_ptr->owner2 == s) free_frame_buffer(s, s->last_picture_ptr); + } - /* release forgotten pictures */ - /* if (mpeg124/h263) */ - if (!s->encoding) { - for (i = 0; i < s->picture_count; i++) { - if (s->picture[i].owner2 == s && s->picture[i].f.data[0] && - &s->picture[i] != s->next_picture_ptr && - s->picture[i].f.reference) { - if (!(avctx->active_thread_type & FF_THREAD_FRAME)) - av_log(avctx, AV_LOG_ERROR, - "releasing zombie picture\n"); - free_frame_buffer(s, &s->picture[i]); - } + /* release forgotten pictures */ + /* if (mpeg124/h263) */ + if (!s->encoding) { + for (i = 0; i < s->picture_count; i++) { + if (s->picture[i].owner2 == s && s->picture[i].f.data[0] && + &s->picture[i] != s->last_picture_ptr && + &s->picture[i] != s->next_picture_ptr && + s->picture[i].f.reference) { + if (!(avctx->active_thread_type & FF_THREAD_FRAME)) + av_log(avctx, AV_LOG_ERROR, + "releasing zombie picture\n"); + free_frame_buffer(s, &s->picture[i]); } } } @@ -1295,6 +1296,14 @@ int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) if (s->next_picture_ptr) ff_copy_picture(&s->next_picture, s->next_picture_ptr); + if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME) && + (s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3)) { + if (s->next_picture_ptr) + s->next_picture_ptr->owner2 = s; + if (s->last_picture_ptr) + s->last_picture_ptr->owner2 = s; + } + assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr && s->last_picture_ptr->f.data[0])); From 9472d37d8e137df1e78e973b8b0e0d5607a799d5 Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Fri, 9 Sep 2011 14:26:32 +0200 Subject: [PATCH 22/22] rv34: frame-level multi-threading Statistics for bourne.rmvb -an -f null 1 thread: 37.12s user 0.03s system 99% cpu 37.174 total 2 threads: 47.63s user 0.24s system 185% cpu 25.807 total 4 threads: 41.21s user 0.30s system 327% cpu 12.674 total --- libavcodec/rv30.c | 4 ++- libavcodec/rv34.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/rv34.h | 2 ++ libavcodec/rv40.c | 4 ++- 4 files changed, 98 insertions(+), 2 deletions(-) diff --git a/libavcodec/rv30.c b/libavcodec/rv30.c index 26708db4fa..4828e982b7 100644 --- a/libavcodec/rv30.c +++ b/libavcodec/rv30.c @@ -275,8 +275,10 @@ AVCodec ff_rv30_decoder = { .init = rv30_decode_init, .close = ff_rv34_decode_end, .decode = ff_rv34_decode_frame, - .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY, + .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_FRAME_THREADS, .flush = ff_mpeg_flush, .long_name = NULL_IF_CONFIG_SMALL("RealVideo 3.0"), .pix_fmts = ff_pixfmt_list_420, + .init_thread_copy = ONLY_IF_THREADS_ENABLED(ff_rv34_decode_init_thread_copy), + .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_rv34_decode_update_thread_context), }; diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c index f9f8677385..ec826fd074 100644 --- a/libavcodec/rv34.c +++ b/libavcodec/rv34.c @@ -24,12 +24,16 @@ * RV30/40 decoder common data */ +#include "libavutil/internal.h" + #include "avcodec.h" #include "dsputil.h" #include "mpegvideo.h" #include "golomb.h" +#include "internal.h" #include "mathops.h" #include "rectangle.h" +#include "thread.h" #include "rv34vlc.h" #include "rv34data.h" @@ -669,6 +673,14 @@ static inline void rv34_mc(RV34DecContext *r, const int block_type, if(uvmx == 6 && uvmy == 6) uvmx = uvmy = 4; } + + if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) { + /* wait for the referenced mb row to be finished */ + int mb_row = FFMIN(s->mb_height - 1, s->mb_y + ((yoff + my + 21) >> 4)); + AVFrame *f = dir ? &s->next_picture_ptr->f : &s->last_picture_ptr->f; + ff_thread_await_progress(f, mb_row, 0); + } + dxy = ly*4 + lx; srcY = dir ? s->next_picture_ptr->f.data[0] : s->last_picture_ptr->f.data[0]; srcU = dir ? s->next_picture_ptr->f.data[1] : s->last_picture_ptr->f.data[1]; @@ -824,6 +836,10 @@ static int rv34_decode_mv(RV34DecContext *r, int block_type) } case RV34_MB_B_DIRECT: //surprisingly, it uses motion scheme from next reference frame + /* wait for the current mb row to be finished */ + if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) + ff_thread_await_progress(&s->next_picture_ptr->f, s->mb_y - 1, 0); + next_bt = s->next_picture_ptr->f.mb_type[s->mb_x + s->mb_y * s->mb_stride]; if(IS_INTRA(next_bt) || IS_SKIP(next_bt)){ ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride); @@ -1255,6 +1271,7 @@ static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int } } s->mb_x = s->mb_y = 0; + ff_thread_finish_setup(s->avctx); } else { int slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I; @@ -1299,6 +1316,11 @@ static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int if(r->loop_filter && s->mb_y >= 2) r->loop_filter(r, s->mb_y - 2); + + if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) + ff_thread_report_progress(&s->current_picture_ptr->f, + s->mb_y - 2, 0); + } if(s->mb_x == s->resync_mb_x) s->first_slice_line=0; @@ -1364,6 +1386,71 @@ av_cold int ff_rv34_decode_init(AVCodecContext *avctx) return 0; } +int ff_rv34_decode_init_thread_copy(AVCodecContext *avctx) +{ + RV34DecContext *r = avctx->priv_data; + + r->s.avctx = avctx; + + if (avctx->internal->is_copy) { + r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height * + sizeof(*r->cbp_chroma)); + r->cbp_luma = av_malloc(r->s.mb_stride * r->s.mb_height * + sizeof(*r->cbp_luma)); + r->deblock_coefs = av_malloc(r->s.mb_stride * r->s.mb_height * + sizeof(*r->deblock_coefs)); + r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 * + sizeof(*r->intra_types_hist)); + r->mb_type = av_malloc(r->s.mb_stride * r->s.mb_height * + sizeof(*r->mb_type)); + + if (!(r->cbp_chroma && r->cbp_luma && r->deblock_coefs && + r->intra_types_hist && r->mb_type)) { + av_freep(&r->cbp_chroma); + av_freep(&r->cbp_luma); + av_freep(&r->deblock_coefs); + av_freep(&r->intra_types_hist); + av_freep(&r->mb_type); + r->intra_types = NULL; + return AVERROR(ENOMEM); + } + + r->intra_types = r->intra_types_hist + r->intra_types_stride * 4; + r->tmp_b_block_base = NULL; + + memset(r->mb_type, 0, r->s.mb_stride * r->s.mb_height * + sizeof(*r->mb_type)); + + MPV_common_init(&r->s); + } + return 0; +} + +int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src) +{ + RV34DecContext *r = dst->priv_data, *r1 = src->priv_data; + MpegEncContext * const s = &r->s, * const s1 = &r1->s; + int err; + + if (dst == src || !s1->context_initialized) + return 0; + + if ((err = ff_mpeg_update_thread_context(dst, src))) + return err; + + r->cur_pts = r1->cur_pts; + r->last_pts = r1->last_pts; + r->next_pts = r1->next_pts; + + memset(&r->si, 0, sizeof(r->si)); + + /* necessary since it is it the condition checked for in decode_slice + * to call MPV_frame_start. cmp. comment at the end of decode_frame */ + s->current_picture_ptr = NULL; + + return 0; +} + static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n) { if(avctx->slice_count) return avctx->slice_offset[n]; @@ -1465,6 +1552,9 @@ int ff_rv34_decode_frame(AVCodecContext *avctx, if(last && s->current_picture_ptr){ if(r->loop_filter) r->loop_filter(r, s->mb_height - 1); + if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) + ff_thread_report_progress(&s->current_picture_ptr->f, + s->mb_height - 1, 0); ff_er_frame_end(s); MPV_frame_end(s); if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) { diff --git a/libavcodec/rv34.h b/libavcodec/rv34.h index 3f3b879306..d86b009705 100644 --- a/libavcodec/rv34.h +++ b/libavcodec/rv34.h @@ -133,5 +133,7 @@ int ff_rv34_get_start_offset(GetBitContext *gb, int blocks); int ff_rv34_decode_init(AVCodecContext *avctx); int ff_rv34_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt); int ff_rv34_decode_end(AVCodecContext *avctx); +int ff_rv34_decode_init_thread_copy(AVCodecContext *avctx); +int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src); #endif /* AVCODEC_RV34_H */ diff --git a/libavcodec/rv40.c b/libavcodec/rv40.c index bde63e19a5..c55a07a7d0 100644 --- a/libavcodec/rv40.c +++ b/libavcodec/rv40.c @@ -563,8 +563,10 @@ AVCodec ff_rv40_decoder = { .init = rv40_decode_init, .close = ff_rv34_decode_end, .decode = ff_rv34_decode_frame, - .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY, + .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_FRAME_THREADS, .flush = ff_mpeg_flush, .long_name = NULL_IF_CONFIG_SMALL("RealVideo 4.0"), .pix_fmts = ff_pixfmt_list_420, + .init_thread_copy = ONLY_IF_THREADS_ENABLED(ff_rv34_decode_init_thread_copy), + .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_rv34_decode_update_thread_context), };