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
13 13
14API changes, most recent first:14API changes, most recent first:
15 15
162014-06-xx - xxxxxxx - lavu 53.17.0 - imgutils.h
17 Add av_image_check_sar().
18
162014-06-xx - xxxxxxx - lavc 55.55.0 - avcodec.h192014-06-xx - xxxxxxx - lavc 55.55.0 - avcodec.h
17 Add av_packet_rescale_ts() to simplify timestamp conversion.20 Add av_packet_rescale_ts() to simplify timestamp conversion.
18 21
libavcodec/dirac.c+2−0
@@ -316,6 +316,8 @@ int avpriv_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb, · 315 unmodified lines
316 if (ret < 0)316 if (ret < 0)
317 return ret;317 return ret;
318 318
319 ff_set_sar(avctx, avctx->sample_aspect_ratio);
320
319 /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames321 /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
320 * currently only used to signal field coding */322 * currently only used to signal field coding */
321 picture_coding_mode = svq3_get_ue_golomb(gb);323 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
150 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)150 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
151 return ret;151 return ret;
152 152
153 ff_set_sar(avctx, avctx->sample_aspect_ratio);
154
153 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {155 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
154 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");156 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
155 return ret;157 return ret;
libavcodec/dvdec.c+9−8
@@ -36,6 +36,7 @@ · 35 unmodified lines
36 */36 */
37 37
38#include "libavutil/internal.h"38#include "libavutil/internal.h"
39#include "libavutil/imgutils.h"
39#include "libavutil/pixdesc.h"40#include "libavutil/pixdesc.h"
40#include "avcodec.h"41#include "avcodec.h"
41#include "internal.h"42#include "internal.h"
@@ -347,6 +348,14 @@ static int dvvideo_decode_frame(AVCodecContext *avctx, · 305 unmodified lines
347 if (ret < 0)348 if (ret < 0)
348 return ret;349 return ret;
349 350
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
350 if (ff_get_buffer(avctx, s->frame, 0) < 0) {359 if (ff_get_buffer(avctx, s->frame, 0) < 0) {
351 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");360 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
352 return -1;361 return -1;
@@ -363,14 +372,6 @@ static int dvvideo_decode_frame(AVCodecContext *avctx, · 10 unmodified lines
363 /* return image */372 /* return image */
364 *got_frame = 1;373 *got_frame = 1;
365 374
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
374 return s->sys->frame_size;375 return s->sys->frame_size;
375}376}
376 377
libavcodec/exr.c+2−2
@@ -1107,8 +1107,8 @@ static int decode_header(EXRContext *s) · 1106 unmodified lines
1107 if (!var_size)1107 if (!var_size)
1108 return AVERROR_INVALIDDATA;1108 return AVERROR_INVALIDDATA;
1109 1109
1110 s->avctx->sample_aspect_ratio =1110 ff_set_sar(s->avctx,
1111 av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255);1111 av_d2q(av_int2float(bytestream2_get_le32(&s->gb)), 255));
1112 1112
1113 continue;1113 continue;
1114 } else if ((var_size = check_header_variable(s, "compression",1114 } 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
328 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);328 f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
329 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);329 f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
330 330
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
331 return 0;339 return 0;
332}340}
333 341
libavcodec/h263dec.c+2−0
@@ -497,6 +497,8 @@ int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, · 496 unmodified lines
497 if (ret < 0)497 if (ret < 0)
498 return ret;498 return ret;
499 499
500 ff_set_sar(avctx, avctx->sample_aspect_ratio);
501
500 if ((ret = ff_MPV_common_frame_size_change(s)))502 if ((ret = ff_MPV_common_frame_size_change(s)))
501 return ret;503 return ret;
502 }504 }
libavcodec/h264_slice.c+1−2
@@ -1067,8 +1067,7 @@ static int h264_slice_header_init(H264Context *h, int reinit) · 1066 unmodified lines
1067 h->avctx->thread_count : 1;1067 h->avctx->thread_count : 1;
1068 int i, ret;1068 int i, ret;
1069 1069
1070 h->avctx->sample_aspect_ratio = h->sps.sar;1070 ff_set_sar(h->avctx, h->sps.sar);
1071 av_assert0(h->avctx->sample_aspect_ratio.den);
1072 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,1071 av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
1073 &h->chroma_x_shift, &h->chroma_y_shift);1072 &h->chroma_x_shift, &h->chroma_y_shift);
1074 1073
libavcodec/hevc.c+2−1
@@ -400,9 +400,10 @@ static int set_sps(HEVCContext *s, const HEVCSPS *sps) · 399 unmodified lines
400 s->avctx->width = sps->output_width;400 s->avctx->width = sps->output_width;
401 s->avctx->height = sps->output_height;401 s->avctx->height = sps->output_height;
402 s->avctx->pix_fmt = sps->pix_fmt;402 s->avctx->pix_fmt = sps->pix_fmt;
403 s->avctx->sample_aspect_ratio = sps->vui.sar;
404 s->avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;403 s->avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
405 404
405 ff_set_sar(s->avctx, sps->vui.sar);
406
406 if (sps->vui.video_signal_type_present_flag)407 if (sps->vui.video_signal_type_present_flag)
407 s->avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG408 s->avctx->color_range = sps->vui.video_full_range_flag ? AVCOL_RANGE_JPEG
408 : AVCOL_RANGE_MPEG;409 : 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
176 */176 */
177int ff_set_dimensions(AVCodecContext *s, int width, int height);177int ff_set_dimensions(AVCodecContext *s, int width, int height);
178 178
179/**
180 * Check that the provided sample aspect ratio is valid and set it on the codec
181 * context.
182 */
183int ff_set_sar(AVCodecContext *avctx, AVRational sar);
184
179/**185/**
180 * Add or update AV_FRAME_DATA_MATRIXENCODING side data.186 * Add or update AV_FRAME_DATA_MATRIXENCODING side data.
181 */187 */
libavcodec/mjpegdec.c+1−0
@@ -1222,6 +1222,7 @@ static int mjpeg_decode_app(MJpegDecodeContext *s) · 1221 unmodified lines
1222 1222
1223 s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);1223 s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1224 s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);1224 s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1225 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1225 1226
1226 if (s->avctx->debug & FF_DEBUG_PICT_INFO)1227 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1227 av_log(s->avctx, AV_LOG_INFO,1228 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
1294 }1294 }
1295 } // MPEG-21295 } // MPEG-2
1296 1296
1297 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1298
1297 avctx->pix_fmt = mpeg_get_pixelformat(avctx);1299 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1298 // until then pix_fmt may be changed right after codec init1300 // until then pix_fmt may be changed right after codec init
1299#if FF_API_XVMC1301#if FF_API_XVMC
libavcodec/truemotion1.c+2−0
@@ -412,6 +412,8 @@ static int truemotion1_decode_header(TrueMotion1Context *s) · 411 unmodified lines
412 if ((ret = ff_set_dimensions(s->avctx, s->w, s->h)) < 0)412 if ((ret = ff_set_dimensions(s->avctx, s->w, s->h)) < 0)
413 return ret;413 return ret;
414 414
415 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
416
415 av_fast_malloc(&s->vert_pred, &s->vert_pred_size, s->avctx->width * sizeof(unsigned int));417 av_fast_malloc(&s->vert_pred, &s->vert_pred_size, s->avctx->width * sizeof(unsigned int));
416 }418 }
417 419
libavcodec/utils.c+33−0
@@ -153,6 +153,21 @@ int ff_set_dimensions(AVCodecContext *s, int width, int height) · 152 unmodified lines
153 return ret;153 return ret;
154}154}
155 155
156int 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
156int ff_side_data_update_matrix_encoding(AVFrame *frame,171int ff_side_data_update_matrix_encoding(AVFrame *frame,
157 enum AVMatrixEncoding matrix_encoding)172 enum AVMatrixEncoding matrix_encoding)
158{173{
@@ -636,6 +651,14 @@ int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags) · 477 unmodified lines
636 if (!frame->sample_aspect_ratio.num)651 if (!frame->sample_aspect_ratio.num)
637 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;652 frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
638 653
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
639 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)662 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
640 return ret;663 return ret;
641 break;664 break;
@@ -1071,6 +1094,16 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code · 429 unmodified lines
1071 ff_set_dimensions(avctx, 0, 0);1094 ff_set_dimensions(avctx, 0, 0);
1072 }1095 }
1073 1096
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
1074 /* if the decoder init function was already called previously,1107 /* if the decoder init function was already called previously,
1075 * free the already allocated subtitle_header before overwriting it */1108 * free the already allocated subtitle_header before overwriting it */
1076 if (av_codec_is_decoder(codec))1109 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
478 v->s.avctx->width * h,478 v->s.avctx->width * h,
479 1 << 30);479 1 << 30);
480 }480 }
481 ff_set_sar(v->s.avctx, v->s.avctx->sample_aspect_ratio);
481 av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n",482 av_log(v->s.avctx, AV_LOG_DEBUG, "Aspect: %i:%i\n",
482 v->s.avctx->sample_aspect_ratio.num,483 v->s.avctx->sample_aspect_ratio.num,
483 v->s.avctx->sample_aspect_ratio.den);484 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
2243 av_reduce(&avctx->sample_aspect_ratio.num,2243 av_reduce(&avctx->sample_aspect_ratio.num,
2244 &avctx->sample_aspect_ratio.den,2244 &avctx->sample_aspect_ratio.den,
2245 aspect.num, aspect.den, 1 << 30);2245 aspect.num, aspect.den, 1 << 30);
2246 ff_set_sar(avctx, avctx->sample_aspect_ratio);
2246 }2247 }
2247 2248
2248 if (s->theora < 0x030200)2249 if (s->theora < 0x030200)
libavutil/imgutils.c+23−0
@@ -25,7 +25,9 @@ · 24 unmodified lines
25#include "imgutils.h"25#include "imgutils.h"
26#include "internal.h"26#include "internal.h"
27#include "log.h"27#include "log.h"
28#include "mathematics.h"
28#include "pixdesc.h"29#include "pixdesc.h"
30#include "rational.h"
29 31
30void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],32void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
31 const AVPixFmtDescriptor *pixdesc)33 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
228 return AVERROR(EINVAL);230 return AVERROR(EINVAL);
229}231}
230 232
233int 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
231void av_image_copy_plane(uint8_t *dst, int dst_linesize,254void av_image_copy_plane(uint8_t *dst, int dst_linesize,
232 const uint8_t *src, int src_linesize,255 const uint8_t *src, int src_linesize,
233 int bytewidth, int height)256 int bytewidth, int height)
libavutil/imgutils.h+15−0
@@ -29,6 +29,7 @@ · 28 unmodified lines
29 29
30#include "avutil.h"30#include "avutil.h"
31#include "pixdesc.h"31#include "pixdesc.h"
32#include "rational.h"
32 33
33/**34/**
34 * Compute the max pixel step for each plane of an image with a35 * 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
128 */129 */
129int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx);130int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx);
130 131
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 */
144int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar);
145
131int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt);146int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt);
132 147
133/**148/**
libavutil/version.h+1−1
@@ -54,7 +54,7 @@ · 53 unmodified lines
54 */54 */
55 55
56#define LIBAVUTIL_VERSION_MAJOR 5356#define LIBAVUTIL_VERSION_MAJOR 53
57#define LIBAVUTIL_VERSION_MINOR 1657#define LIBAVUTIL_VERSION_MINOR 17
58#define LIBAVUTIL_VERSION_MICRO 058#define LIBAVUTIL_VERSION_MICRO 0
59 59
60#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \60#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