History

Add av_image_check_sar() and use it to validate SAR

Justin Ruggles committed 12 years ago

Commit 9e500ef1 parent d349afb19 files · +116 −14Browse files

Unified · Split

19 files
  1. doc/APIchanges +3 −0
  2. libavcodec/dirac.c +2 −0
  3. libavcodec/dpx.c +2 −0
  4. libavcodec/dvdec.c +9 −8
  5. libavcodec/exr.c +2 −2
  6. libavcodec/ffv1dec.c +8 −0
  7. libavcodec/h263dec.c +2 −0
  8. libavcodec/h264_slice.c +1 −2
  9. libavcodec/hevc.c +2 −1
  10. libavcodec/internal.h +6 −0
  11. libavcodec/mjpegdec.c +1 −0
  12. libavcodec/mpeg12dec.c +2 −0
  13. libavcodec/truemotion1.c +2 −0
  14. libavcodec/utils.c +33 −0
  15. libavcodec/vc1.c +1 −0
  16. libavcodec/vp3.c +1 −0
  17. libavutil/imgutils.c +23 −0
  18. libavutil/imgutils.h +15 −0
  19. libavutil/version.h +1 −1
doc/APIchanges+3−0
@@ -13,6 +13,9 @@ libavutil: 2013-12-xx · 12 unmodified lines
1313
1414 API changes, most recent first:
1515
16+2014-06-xx - xxxxxxx - lavu 53.17.0 - imgutils.h
17+ Add av_image_check_sar().
18+
1619 2014-06-xx - xxxxxxx - lavc 55.55.0 - avcodec.h
1720 Add av_packet_rescale_ts() to simplify timestamp conversion.
1821
libavcodec/dirac.c+2−0
@@ -316,6 +316,8 @@ int avpriv_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb, · 315 unmodified lines
316316 if (ret < 0)
317317 return ret;
318318
319+ ff_set_sar(avctx, avctx->sample_aspect_ratio);
320+
319321 /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
320322 * currently only used to signal field coding */
321323 picture_coding_mode = svq3_get_ue_golomb(gb);
libavcodec/dpx.c+2−0
@@ -150,6 +150,8 @@ static int decode_frame(AVCodecContext *avctx, · 149 unmodified lines
150150 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
151151 return ret;
152152
153+ ff_set_sar(avctx, avctx->sample_aspect_ratio);
154+
153155 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
154156 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
155157 return ret;
libavcodec/dvdec.c+9−8
@@ -36,6 +36,7 @@ · 35 unmodified lines
3636 */
3737
3838 #include "libavutil/internal.h"
39+#include "libavutil/imgutils.h"
3940 #include "libavutil/pixdesc.h"
4041 #include "avcodec.h"
4142 #include "internal.h"
@@ -347,6 +348,14 @@ static int dvvideo_decode_frame(AVCodecContext *avctx, · 305 unmodified lines
347348 if (ret < 0)
348349 return ret;
349350
351+ /* Determine the codec's sample_aspect ratio from the packet */
352+ vsc_pack = buf + 80*5 + 48 + 5;
353+ if ( *vsc_pack == dv_video_control ) {
354+ apt = buf[4] & 0x07;
355+ is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 || (!apt && (vsc_pack[2] & 0x07) == 0x07)));
356+ ff_set_sar(avctx, s->sys->sar[is16_9]);
357+ }
358+
350359 if (ff_get_buffer(avctx, s->frame, 0) < 0) {
351360 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
352361 return -1;
@@ -363,14 +372,6 @@ static int dvvideo_decode_frame(AVCodecContext *avctx, · 10 unmodified lines
363372 /* return image */
364373 *got_frame = 1;
365374
366− /* Determine the codec's sample_aspect ratio from the packet */
367− vsc_pack = buf + 80*5 + 48 + 5;
368− if ( *vsc_pack == dv_video_control ) {
369− apt = buf[4] & 0x07;
370− is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 || (!apt && (vsc_pack[2] & 0x07) == 0x07)));
371− avctx->sample_aspect_ratio = s->sys->sar[is16_9];
372− }
373−
374375 return s->sys->frame_size;
375376 }
376377
libavcodec/exr.c+2−2
@@ -1107,8 +1107,8 @@ static int decode_header(EXRContext *s) · 1106 unmodified lines
11071107 if (!var_size)
11081108 return AVERROR_INVALIDDATA;
11091109
1110− s->avctx->sample_aspect_ratio =
1111− av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255);
1110+ ff_set_sar(s->avctx,
1111+ av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255));
11121112
11131113 continue;
11141114 } else if ((var_size = check_header_variable(s, "compression",
libavcodec/ffv1dec.c+8−0
@@ -328,6 +328,14 @@ static int decode_slice_header(FFV1Context *f, FFV1Context *fs) · 327 unmodified lines
328328 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
329329 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
330330
331+ if (av_image_check_sar(f->width, f->height,
332+ f->cur->sample_aspect_ratio) < 0) {
333+ av_log(f->avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
334+ f->cur->sample_aspect_ratio.num,
335+ f->cur->sample_aspect_ratio.den);
336+ f->cur->sample_aspect_ratio = (AVRational){ 0, 1 };
337+ }
338+
331339 return 0;
332340 }
333341
libavcodec/h263dec.c+2−0
@@ -497,6 +497,8 @@ int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, · 496 unmodified lines
497497 if (ret < 0)
498498 return ret;
499499
500+ ff_set_sar(avctx, avctx->sample_aspect_ratio);
501+
500502 if ((ret = ff_MPV_common_frame_size_change(s)))
501503 return ret;
502504 }
libavcodec/h264_slice.c+1−2
@@ -1067,8 +1067,7 @@ static int h264_slice_header_init(H264Context *h, int reinit) · 1066 unmodified lines
10671067 h->avctx->thread_count : 1;
10681068 int i, ret;
10691069
1070− h->avctx->sample_aspect_ratio = h->sps.sar;
1071− av_assert0(h->avctx->sample_aspect_ratio.den);
1070+ ff_set_sar(h->avctx, h->sps.sar);
10721071 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
10731072 &h->chroma_x_shift, &h->chroma_y_shift);
10741073
libavcodec/hevc.c+2−1
@@ -400,9 +400,10 @@ static int set_sps(HEVCContext *s, const HEVCSPS *sps) · 399 unmodified lines
400400 s->avctx->width = sps->output_width;
401401 s->avctx->height = sps->output_height;
402402 s->avctx->pix_fmt = sps->pix_fmt;
403− s->avctx->sample_aspect_ratio = sps->vui.sar;
404403 s->avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
405404
405+ ff_set_sar(s->avctx, sps->vui.sar);
406+
406407 if (sps->vui.video_signal_type_present_flag)
407408 s->avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
408409 : AVCOL_RANGE_MPEG;
libavcodec/internal.h+6−0
@@ -176,6 +176,12 @@ const uint8_t *avpriv_find_start_code(const uint8_t *restrict p, · 175 unmodified lines
176176 */
177177 int ff_set_dimensions(AVCodecContext *s, int width, int height);
178178
179+/**
180+ * Check that the provided sample aspect ratio is valid and set it on the codec
181+ * context.
182+ */
183+int ff_set_sar(AVCodecContext *avctx, AVRational sar);
184+
179185 /**
180186 * Add or update AV_FRAME_DATA_MATRIXENCODING side data.
181187 */
libavcodec/mjpegdec.c+1−0
@@ -1222,6 +1222,7 @@ static int mjpeg_decode_app(MJpegDecodeContext *s) · 1221 unmodified lines
12221222
12231223 s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
12241224 s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1225+ ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
12251226
12261227 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
12271228 av_log(s->avctx, AV_LOG_INFO,
libavcodec/mpeg12dec.c+2−0
@@ -1294,6 +1294,8 @@ static int mpeg_decode_postinit(AVCodecContext *avctx) · 1293 unmodified lines
12941294 }
12951295 } // MPEG-2
12961296
1297+ ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1298+
12971299 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
12981300 // until then pix_fmt may be changed right after codec init
12991301 #if FF_API_XVMC
libavcodec/truemotion1.c+2−0
@@ -412,6 +412,8 @@ static int truemotion1_decode_header(TrueMotion1Context *s) · 411 unmodified lines
412412 if ((ret = ff_set_dimensions(s->avctx, s->w, s->h)) < 0)
413413 return ret;
414414
415+ ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
416+
415417 av_fast_malloc(&s->vert_pred, &s->vert_pred_size, s->avctx->width * sizeof(unsigned int));
416418 }
417419
libavcodec/utils.c+33−0
@@ -153,6 +153,21 @@ int ff_set_dimensions(AVCodecContext *s, int width, int height) · 152 unmodified lines
153153 return ret;
154154 }
155155
156+int ff_set_sar(AVCodecContext *avctx, AVRational sar)
157+{
158+ int ret = av_image_check_sar(avctx->width, avctx->height, sar);
159+
160+ if (ret < 0) {
161+ av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
162+ sar.num, sar.den);
163+ avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
164+ return ret;
165+ } else {
166+ avctx->sample_aspect_ratio = sar;
167+ }
168+ return 0;
169+}
170+
156171 int ff_side_data_update_matrix_encoding(AVFrame *frame,
157172 enum AVMatrixEncoding matrix_encoding)
158173 {
@@ -636,6 +651,14 @@ int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags) · 477 unmodified lines
636651 if (!frame->sample_aspect_ratio.num)
637652 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
638653
654+ if (av_image_check_sar(frame->width, frame->height,
655+ frame->sample_aspect_ratio) < 0) {
656+ av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
657+ frame->sample_aspect_ratio.num,
658+ frame->sample_aspect_ratio.den);
659+ frame->sample_aspect_ratio = (AVRational){ 0, 1 };
660+ }
661+
639662 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
640663 return ret;
641664 break;
@@ -1071,6 +1094,16 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code · 429 unmodified lines
10711094 ff_set_dimensions(avctx, 0, 0);
10721095 }
10731096
1097+ if (avctx->width > 0 && avctx->height > 0) {
1098+ if (av_image_check_sar(avctx->width, avctx->height,
1099+ avctx->sample_aspect_ratio) < 0) {
1100+ av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1101+ avctx->sample_aspect_ratio.num,
1102+ avctx->sample_aspect_ratio.den);
1103+ avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1104+ }
1105+ }
1106+
10741107 /* if the decoder init function was already called previously,
10751108 * free the already allocated subtitle_header before overwriting it */
10761109 if (av_codec_is_decoder(codec))
libavcodec/vc1.c+1−0
@@ -478,6 +478,7 @@ static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb) · 477 unmodified lines
478478 v->s.avctx->width * h,
479479 1 << 30);
480480 }
481+ ff_set_sar(v->s.avctx, v->s.avctx->sample_aspect_ratio);
481482 av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n",
482483 v->s.avctx->sample_aspect_ratio.num,
483484 v->s.avctx->sample_aspect_ratio.den);
libavcodec/vp3.c+1−0
@@ -2243,6 +2243,7 @@ static int theora_decode_header(AVCodecContext *avctx, GetBitContext *gb) · 2242 unmodified lines
22432243 av_reduce(&avctx->sample_aspect_ratio.num,
22442244 &avctx->sample_aspect_ratio.den,
22452245 aspect.num, aspect.den, 1 << 30);
2246+ ff_set_sar(avctx, avctx->sample_aspect_ratio);
22462247 }
22472248
22482249 if (s->theora < 0x030200)
libavutil/imgutils.c+23−0
@@ -25,7 +25,9 @@ · 24 unmodified lines
2525 #include "imgutils.h"
2626 #include "internal.h"
2727 #include "log.h"
28+#include "mathematics.h"
2829 #include "pixdesc.h"
30+#include "rational.h"
2931
3032 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
3133 const AVPixFmtDescriptor *pixdesc)
@@ -228,6 +230,27 @@ int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *lo · 196 unmodified lines
228230 return AVERROR(EINVAL);
229231 }
230232
233+int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
234+{
235+ int64_t scaled_dim;
236+
237+ if (!sar.den)
238+ return AVERROR(EINVAL);
239+
240+ if (!sar.num || sar.num == sar.den)
241+ return 0;
242+
243+ if (sar.num < sar.den)
244+ scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
245+ else
246+ scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
247+
248+ if (scaled_dim > 0)
249+ return 0;
250+
251+ return AVERROR(EINVAL);
252+}
253+
231254 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
232255 const uint8_t *src, int src_linesize,
233256 int bytewidth, int height)
libavutil/imgutils.h+15−0
@@ -29,6 +29,7 @@ · 28 unmodified lines
2929
3030 #include "avutil.h"
3131 #include "pixdesc.h"
32+#include "rational.h"
3233
3334 /**
3435 * Compute the max pixel step for each plane of an image with a
@@ -128,6 +129,20 @@ void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], · 93 unmodified lines
128129 */
129130 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx);
130131
132+/**
133+ * Check if the given sample aspect ratio of an image is valid.
134+ *
135+ * It is considered invalid if the denominator is 0 or if applying the ratio
136+ * to the image size would make the smaller dimension less than 1. If the
137+ * sar numerator is 0, it is considered unknown and will return as valid.
138+ *
139+ * @param w width of the image
140+ * @param h height of the image
141+ * @param sar sample aspect ratio of the image
142+ * @return 0 if valid, a negative AVERROR code otherwise
143+ */
144+int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar);
145+
131146 int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt);
132147
133148 /**
libavutil/version.h+1−1
@@ -54,7 +54,7 @@ · 53 unmodified lines
5454 */
5555
5656 #define LIBAVUTIL_VERSION_MAJOR 53
57−#define LIBAVUTIL_VERSION_MINOR 16
57+#define LIBAVUTIL_VERSION_MINOR 17
5858 #define LIBAVUTIL_VERSION_MICRO 0
5959
6060 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

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