History

avcodec: Add max_pixels options

Michael Niedermayer committed 9 years ago

Commit 2f078301 parent f542b156 files · +23 −6Browse files

Unified · Split

6 files
  1. doc/codecs.texi +4 −0
  2. libavcodec/avcodec.h +8 −0
  3. libavcodec/options_table.h +1 −0
  4. libavcodec/utils.c +6 −6
  5. tests/ref/fate/api-mjpeg-codec-param +2 −0
  6. tests/ref/fate/api-png-codec-param +2 −0
doc/codecs.texi+4−0
@@ -1272,6 +1272,10 @@ ffprobe -dump_separator " · 1271 unmodified lines
12721272 " -i ~/videos/matrixbench_mpeg2.mpg
12731273 @end example
12741274
1275+@item max_pixels @var{integer} (@emph{decoding/encoding,video})
1276+Maximum number of pixels per image. This value can be used to avoid out of
1277+memory failures due to large images.
1278+
12751279 @end table
12761280
12771281 @c man end CODEC OPTIONS
libavcodec/avcodec.h+8−0
@@ -3570,6 +3570,14 @@ typedef struct AVCodecContext { · 3569 unmodified lines
35703570 */
35713571 int trailing_padding;
35723572
3573+ /**
3574+ * The number of pixels per image to maximally accept.
3575+ *
3576+ * - decoding: set by user
3577+ * - encoding: set by user
3578+ */
3579+ int64_t max_pixels;
3580+
35733581 } AVCodecContext;
35743582
35753583 AVRational av_codec_get_pkt_timebase (const AVCodecContext *avctx);
libavcodec/options_table.h+1−0
@@ -570,6 +570,7 @@ static const AVOption avcodec_options[] = { · 569 unmodified lines
570570 {"codec_whitelist", "List of decoders that are allowed to be used", OFFSET(codec_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, A|V|S|D },
571571 {"pixel_format", "set pixel format", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64=AV_PIX_FMT_NONE}, -1, INT_MAX, 0 },
572572 {"video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str=NULL}, 0, INT_MAX, 0 },
573+{"max_pixels", "Maximum number of pixels", OFFSET(max_pixels), AV_OPT_TYPE_INT64, {.i64 = INT_MAX }, 0, INT_MAX, A|V|S|D|E },
573574 {NULL},
574575 };
575576
libavcodec/utils.c+6−6
@@ -209,7 +209,7 @@ void avcodec_set_dimensions(AVCodecContext *s, int width, int height) · 208 unmodified lines
209209
210210 int ff_set_dimensions(AVCodecContext *s, int width, int height)
211211 {
212− int ret = av_image_check_size(width, height, 0, s);
212+ int ret = av_image_check_size2(width, height, s->max_pixels, AV_PIX_FMT_NONE, 0, s);
213213
214214 if (ret < 0)
215215 width = height = 0;
@@ -904,7 +904,7 @@ static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags) · 688 unmodified lines
904904 int ret;
905905
906906 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
907− if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
907+ if ((ret = av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) {
908908 av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
909909 return AVERROR(EINVAL);
910910 }
@@ -1338,8 +1338,8 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code · 427 unmodified lines
13381338 }
13391339
13401340 if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
1341− && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
1342− || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
1341+ && ( av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0
1342+ || av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)) {
13431343 av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
13441344 ff_set_dimensions(avctx, 0, 0);
13451345 }
@@ -1982,7 +1982,7 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx, · 636 unmodified lines
19821982 return 0;
19831983 }
19841984
1985− if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1985+ if (av_image_check_size2(avctx->width, avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
19861986 return AVERROR(EINVAL);
19871987
19881988 if (frame && frame->format == AV_PIX_FMT_NONE)
@@ -2233,7 +2233,7 @@ int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *pi · 244 unmodified lines
22332233 }
22342234
22352235 *got_picture_ptr = 0;
2236− if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
2236+ if ((avctx->coded_width || avctx->coded_height) && av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx))
22372237 return AVERROR(EINVAL);
22382238
22392239 avctx->internal->pkt = avpkt;
tests/ref/fate/api-mjpeg-codec-param+2−0
@@ -155,6 +155,7 @@ stream=0, decode=0 · 154 unmodified lines
155155 codec_whitelist=
156156 pixel_format=yuvj422p
157157 video_size=400x225
158+ max_pixels=2147483647
158159 stream=0, decode=1
159160 b=0
160161 ab=0
@@ -312,3 +313,4 @@ stream=0, decode=1 · 151 unmodified lines
312313 codec_whitelist=
313314 pixel_format=yuvj422p
314315 video_size=400x225
316+ max_pixels=2147483647
tests/ref/fate/api-png-codec-param+2−0
@@ -155,6 +155,7 @@ stream=0, decode=0 · 154 unmodified lines
155155 codec_whitelist=
156156 pixel_format=rgba
157157 video_size=128x128
158+ max_pixels=2147483647
158159 stream=0, decode=1
159160 b=0
160161 ab=0
@@ -312,3 +313,4 @@ stream=0, decode=1 · 151 unmodified lines
312313 codec_whitelist=
313314 pixel_format=rgba
314315 video_size=128x128
316+ max_pixels=2147483647

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t