From 5ff85f1d8b5721a9e7f0ca6e03f61f5d3a4c3664 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Mon, 20 Oct 2003 20:23:46 +0000 Subject: [PATCH] AVRational sample_aspect_ratio aspect ratio in JPEG JFIF is SAR not DAR ! removed nonsense SAR guessing code various related cleanups bugs? Originally committed as revision 2403 to svn://svn.ffmpeg.org/ffmpeg/trunk --- ffmpeg.c | 4 +-- ffplay.c | 3 +- libavcodec/Makefile | 2 +- libavcodec/avcodec.h | 7 +++-- libavcodec/common.c | 26 ------------------ libavcodec/common.h | 3 -- libavcodec/h263.c | 44 ++++++++++++----------------- libavcodec/h263dec.c | 9 +----- libavcodec/h264.c | 25 ++++++----------- libavcodec/h264data.h | 4 +-- libavcodec/mjpeg.c | 51 ++++------------------------------ libavcodec/mpeg12.c | 29 +++++++++++++------- libavcodec/mpeg12data.h | 23 ++++++++++++---- libavcodec/mpeg4data.h | 24 ++++++++-------- libavcodec/mpegvideo.h | 2 -- libavcodec/rational.c | 61 +++++++++++++++++++++++++++++++++++++++++ libavcodec/rational.h | 53 +++++++++++++++++++++++++++++++++++ libavcodec/utils.c | 1 + libavformat/dv.c | 8 ++++-- libavformat/yuv4mpeg.c | 19 +++---------- 20 files changed, 215 insertions(+), 183 deletions(-) create mode 100644 libavcodec/rational.c create mode 100644 libavcodec/rational.h diff --git a/ffmpeg.c b/ffmpeg.c index a160e8abd7..a700b266f9 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -2078,7 +2078,7 @@ static void opt_input_file(const char *filename) case CODEC_TYPE_VIDEO: frame_height = enc->height; frame_width = enc->width; - frame_aspect_ratio = enc->aspect_ratio; + frame_aspect_ratio = av_q2d(enc->sample_aspect_ratio) * enc->width / enc->height; frame_pix_fmt = enc->pix_fmt; rfps = ic->streams[i]->r_frame_rate; rfps_base = ic->streams[i]->r_frame_rate_base; @@ -2239,7 +2239,7 @@ static void opt_output_file(const char *filename) video_enc->width = frame_width; video_enc->height = frame_height; - video_enc->aspect_ratio = frame_aspect_ratio; + video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 30000); video_enc->pix_fmt = frame_pix_fmt; if (!intra_only) diff --git a/ffplay.c b/ffplay.c index 13a96e4112..5105ad1d48 100644 --- a/ffplay.c +++ b/ffplay.c @@ -330,7 +330,8 @@ static void video_image_display(VideoState *is) vp = &is->pictq[is->pictq_rindex]; if (vp->bmp) { /* XXX: use variable in the frame */ - aspect_ratio = is->video_st->codec.aspect_ratio; + aspect_ratio = av_q2d(is->video_st->codec.sample_aspect_ratio) + * is->video_st->codec.width / is->video_st->codec.height;; if (aspect_ratio <= 0.0) aspect_ratio = (float)is->video_st->codec.width / (float)is->video_st->codec.height; diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 4ae6c3a663..00e7c09402 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -18,7 +18,7 @@ OBJS= common.o utils.o mem.o allcodecs.o \ fft.o mdct.o mace.o huffyuv.o cyuv.o opts.o raw.o h264.o golomb.o \ vp3.o asv1.o 4xm.o cabac.o ffv1.o ra144.o ra288.o vcr1.o cljr.o \ roqvideo.o dpcm.o interplayvideo.o xan.o rpza.o cinepak.o msrle.o \ - msvideo1.o vqavideo.o idcinvideo.o adx.o + msvideo1.o vqavideo.o idcinvideo.o adx.o rational.o ifeq ($(AMR_NB),yes) ifeq ($(AMR_NB_FIXED),yes) diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index f6c052a04b..47ab4c0950 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -12,10 +12,11 @@ extern "C" { #endif #include "common.h" +#include "rational.h" #define FFMPEG_VERSION_INT 0x000408 #define FFMPEG_VERSION "0.4.8" -#define LIBAVCODEC_BUILD 4686 +#define LIBAVCODEC_BUILD 4687 #define LIBAVCODEC_VERSION_INT FFMPEG_VERSION_INT #define LIBAVCODEC_VERSION FFMPEG_VERSION @@ -1034,11 +1035,11 @@ typedef struct AVCodecContext { #define FF_PRED_MEDIAN 2 /** - * aspect ratio (0 if unknown). + * sample aspect ratio (0 if unknown). * - encoding: set by user. * - decoding: set by lavc. */ - float aspect_ratio; + AVRational sample_aspect_ratio; /** * the picture in the bitstream. diff --git a/libavcodec/common.c b/libavcodec/common.c index 58d625f099..f37c2c9eba 100644 --- a/libavcodec/common.c +++ b/libavcodec/common.c @@ -386,29 +386,3 @@ int64_t ff_gcd(int64_t a, int64_t b){ if(b) return ff_gcd(b, a%b); else return a; } - -void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max){ - double best_diff=1E10, diff; - int best_denom=1, best_nom=1; - int nom, denom, gcd; - - //brute force here, perhaps we should try continued fractions if we need large max ... - for(denom=1; denom<=max; denom++){ - nom= (int)(f*denom + 0.5); - if(nom<=0 || nom>max) continue; - - diff= ABS( f - (double)nom / (double)denom ); - if(diff < best_diff){ - best_diff= diff; - best_nom= nom; - best_denom= denom; - } - } - - gcd= ff_gcd(best_nom, best_denom); - best_nom /= gcd; - best_denom /= gcd; - - *nom_arg= best_nom; - *denom_arg= best_denom; -} diff --git a/libavcodec/common.h b/libavcodec/common.h index 7149ef8085..9740acb586 100644 --- a/libavcodec/common.h +++ b/libavcodec/common.h @@ -1056,9 +1056,6 @@ static inline int ff_get_fourcc(const char *s){ #define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24)) -void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max); - - #ifdef ARCH_X86 #define MASK_ABS(mask, level)\ asm volatile(\ diff --git a/libavcodec/h263.c b/libavcodec/h263.c index 5d03ba611e..0b09dc7311 100644 --- a/libavcodec/h263.c +++ b/libavcodec/h263.c @@ -131,19 +131,13 @@ int h263_get_picture_format(int width, int height) #ifdef CONFIG_ENCODERS -static void float_aspect_to_info(MpegEncContext * s, float aspect){ +static void aspect_to_info(MpegEncContext * s, AVRational aspect){ int i; - aspect*= s->height/(double)s->width; -//printf("%f\n", aspect); - - if(aspect==0) aspect= 1.0; + if(aspect.num==0) aspect= (AVRational){1,1}; - ff_float2fraction(&s->aspected_width, &s->aspected_height, aspect, 255); - -//printf("%d %d\n", s->aspected_width, s->aspected_height); for(i=1; i<6; i++){ - if(s->aspected_width == pixel_aspect[i][0] && s->aspected_height== pixel_aspect[i][1]){ + if(av_cmp_q(pixel_aspect[i], aspect) == 0){ s->aspect_ratio_info=i; return; } @@ -270,16 +264,15 @@ void h263_encode_picture_header(MpegEncContext * s, int picture_number) if (format == 7) { /* Custom Picture Format (CPFMT) */ - float_aspect_to_info(s, s->avctx->aspect_ratio); + aspect_to_info(s, s->avctx->sample_aspect_ratio); put_bits(&s->pb,4,s->aspect_ratio_info); put_bits(&s->pb,9,(s->width >> 2) - 1); put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */ put_bits(&s->pb,9,(s->height >> 2)); - if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) - { - put_bits(&s->pb, 8, s->aspected_width); - put_bits(&s->pb, 8, s->aspected_height); + if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){ + put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num); + put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den); } } @@ -1949,13 +1942,12 @@ static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_n put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */ put_bits(&s->pb, 3, 1); /* is obj layer priority */ - float_aspect_to_info(s, s->avctx->aspect_ratio); + aspect_to_info(s, s->avctx->sample_aspect_ratio); put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */ - if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) - { - put_bits(&s->pb, 8, s->aspected_width); - put_bits(&s->pb, 8, s->aspected_height); + if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){ + put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num); + put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den); } if(s->low_delay){ @@ -4341,11 +4333,10 @@ int h263_decode_picture_header(MpegEncContext *s) dprintf("\nH.263+ Custom picture: %dx%d\n",width,height); if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) { /* aspected dimensions */ - s->aspected_width = get_bits(&s->gb, 8); - s->aspected_height = get_bits(&s->gb, 8); + s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8); + s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8); }else{ - s->aspected_width = pixel_aspect[s->aspect_ratio_info][0]; - s->aspected_height= pixel_aspect[s->aspect_ratio_info][1]; + s->avctx->sample_aspect_ratio= pixel_aspect[s->aspect_ratio_info]; } } else { width = h263_format[format][0]; @@ -4632,11 +4623,10 @@ static int decode_vol_header(MpegEncContext *s, GetBitContext *gb){ //printf("vo type:%d\n",s->vo_type); s->aspect_ratio_info= get_bits(gb, 4); if(s->aspect_ratio_info == FF_ASPECT_EXTENDED){ - s->aspected_width = get_bits(gb, 8); // par_width - s->aspected_height = get_bits(gb, 8); // par_height + s->avctx->sample_aspect_ratio.num= get_bits(gb, 8); // par_width + s->avctx->sample_aspect_ratio.den= get_bits(gb, 8); // par_height }else{ - s->aspected_width = pixel_aspect[s->aspect_ratio_info][0]; - s->aspected_height= pixel_aspect[s->aspect_ratio_info][1]; + s->avctx->sample_aspect_ratio= pixel_aspect[s->aspect_ratio_info]; } if ((s->vol_control_parameters=get_bits1(gb))) { /* vol control parameter */ diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c index f4f0c29490..f83ff54420 100644 --- a/libavcodec/h263dec.c +++ b/libavcodec/h263dec.c @@ -393,7 +393,6 @@ int ff_h263_decode_frame(AVCodecContext *avctx, MpegEncContext *s = avctx->priv_data; int ret; AVFrame *pict = data; - float new_aspect; #ifdef PRINT_FRAME_TIME uint64_t time= rdtsc(); @@ -602,13 +601,8 @@ retry: /* and other parameters. So then we could init the picture */ /* FIXME: By the way H263 decoder is evolving it should have */ /* an H263EncContext */ - if(s->aspected_height) - new_aspect= s->aspected_width*s->width / (float)(s->height*s->aspected_height); - else - new_aspect=0; - if ( s->width != avctx->width || s->height != avctx->height - || ABS(new_aspect - avctx->aspect_ratio) > 0.001) { + if ( s->width != avctx->width || s->height != avctx->height) { /* H.263 could change picture size any time */ ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat s->parse_context.buffer=0; @@ -618,7 +612,6 @@ retry: if (!s->context_initialized) { avctx->width = s->width; avctx->height = s->height; - avctx->aspect_ratio= new_aspect; goto retry; } diff --git a/libavcodec/h264.c b/libavcodec/h264.c index f186fc26b2..97589ce827 100644 --- a/libavcodec/h264.c +++ b/libavcodec/h264.c @@ -79,8 +79,7 @@ typedef struct SPS{ int crop_top; ///< frame_cropping_rect_top_offset int crop_bottom; ///< frame_cropping_rect_bottom_offset int vui_parameters_present_flag; - int sar_width; - int sar_height; + AVRational sar; short offset_for_ref_frame[256]; //FIXME dyn aloc? }SPS; @@ -2829,7 +2828,6 @@ static int decode_slice_header(H264Context *h){ int first_mb_in_slice, pps_id; int num_ref_idx_active_override_flag; static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE}; - float new_aspect; s->current_picture.reference= h->nal_ref_idc != 0; @@ -2881,14 +2879,8 @@ static int decode_slice_header(H264Context *h){ else s->height= 16*s->mb_height - 4*(h->sps.crop_top + h->sps.crop_bottom); //FIXME recheck - if(s->aspected_height) //FIXME emms at end of slice ? - new_aspect= h->sps.sar_width*s->width / (float)(s->height*h->sps.sar_height); - else - new_aspect=0; - if (s->context_initialized - && ( s->width != s->avctx->width || s->height != s->avctx->height - || ABS(new_aspect - s->avctx->aspect_ratio) > 0.001)) { + && ( s->width != s->avctx->width || s->height != s->avctx->height)) { free_tables(h); MPV_common_end(s); } @@ -2900,7 +2892,7 @@ static int decode_slice_header(H264Context *h){ s->avctx->width = s->width; s->avctx->height = s->height; - s->avctx->aspect_ratio= new_aspect; + s->avctx->sample_aspect_ratio= h->sps.sar; } if(first_mb_in_slice == 0){ @@ -3736,18 +3728,17 @@ static inline int decode_vui_parameters(H264Context *h, SPS *sps){ if( aspect_ratio_info_present_flag ) { aspect_ratio_idc= get_bits(&s->gb, 8); if( aspect_ratio_idc == EXTENDED_SAR ) { - sps->sar_width= get_bits(&s->gb, 16); - sps->sar_height= get_bits(&s->gb, 16); + sps->sar.num= get_bits(&s->gb, 16); + sps->sar.den= get_bits(&s->gb, 16); }else if(aspect_ratio_idc < 16){ - sps->sar_width= pixel_aspect[aspect_ratio_idc][0]; - sps->sar_height= pixel_aspect[aspect_ratio_idc][1]; + sps->sar= pixel_aspect[aspect_ratio_idc]; }else{ fprintf(stderr, "illegal aspect ratio\n"); return -1; } }else{ - sps->sar_width= - sps->sar_height= 0; + sps->sar.num= + sps->sar.den= 0; } // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height); #if 0 diff --git a/libavcodec/h264data.h b/libavcodec/h264data.h index 0a7ad50148..40a2522532 100644 --- a/libavcodec/h264data.h +++ b/libavcodec/h264data.h @@ -51,8 +51,8 @@ #define EXTENDED_SAR 255 -static const uint16_t pixel_aspect[16][2]={ - {0, 0}, +static const AVRational pixel_aspect[14]={ + {0, 1}, {1, 1}, {12, 11}, {10, 11}, diff --git a/libavcodec/mjpeg.c b/libavcodec/mjpeg.c index f67f280a88..f2267faf8f 100644 --- a/libavcodec/mjpeg.c +++ b/libavcodec/mjpeg.c @@ -381,28 +381,8 @@ static void jpeg_put_comments(MpegEncContext *s) put_string(p, "JFIF"); /* this puts the trailing zero-byte too */ put_bits(p, 16, 0x0201); /* v 1.02 */ put_bits(p, 8, 0); /* units type: 0 - aspect ratio */ - switch(s->aspect_ratio_info) - { - case FF_ASPECT_4_3_625: - case FF_ASPECT_4_3_525: - put_bits(p, 16, 4); - put_bits(p, 16, 3); - break; - case FF_ASPECT_16_9_625: - case FF_ASPECT_16_9_525: - put_bits(p, 16, 16); - put_bits(p, 16, 9); - break; - case FF_ASPECT_EXTENDED: - put_bits(p, 16, s->aspected_width); - put_bits(p, 16, s->aspected_height); - break; - case FF_ASPECT_SQUARE: - default: - put_bits(p, 16, 1); /* aspect: 1:1 */ - put_bits(p, 16, 1); - break; - } + put_bits(p, 16, s->avctx->sample_aspect_ratio.num); + put_bits(p, 16, s->avctx->sample_aspect_ratio.den); put_bits(p, 8, 0); /* thumbnail width */ put_bits(p, 8, 0); /* thumbnail height */ } @@ -1547,29 +1527,10 @@ static int mjpeg_decode_app(MJpegDecodeContext *s) skip_bits(&s->gb, 8); /* the trailing zero-byte */ printf("mjpeg: JFIF header found (version: %x.%x)\n", get_bits(&s->gb, 8), get_bits(&s->gb, 8)); - if (get_bits(&s->gb, 8) == 0) - { - int x_density, y_density; - x_density = get_bits(&s->gb, 16); - y_density = get_bits(&s->gb, 16); + skip_bits(&s->gb, 8); - dprintf("x/y density: %d (%f), %d (%f)\n", x_density, - (float)x_density, y_density, (float)y_density); -#if 0 - //MN: needs to be checked - if(x_density) -// s->avctx->aspect_ratio= s->width*y_density/((float)s->height*x_density); - s->avctx->aspect_ratio = (float)x_density/y_density; - /* it's better, but every JFIF I have seen stores 1:1 */ - else - s->avctx->aspect_ratio= 0.0; -#endif - } - else - { - skip_bits(&s->gb, 16); - skip_bits(&s->gb, 16); - } + s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16); + s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16); t_w = get_bits(&s->gb, 8); t_h = get_bits(&s->gb, 8); @@ -2085,7 +2046,7 @@ static int sp5x_decode_frame(AVCodecContext *avctx, memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos)); j += sizeof(sp5x_data_sos); - for (i = 14; i < buf_size, j < buf_size+1024-2; i++) + for (i = 14; i < buf_size && j < buf_size+1024-2; i++) { recoded[j++] = buf[i]; if (buf[i] == 0xff) diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c index 015033bf36..f054e00d0d 100644 --- a/libavcodec/mpeg12.c +++ b/libavcodec/mpeg12.c @@ -190,10 +190,10 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s) int n, i; uint64_t time_code; float best_aspect_error= 1E10; - float aspect_ratio= s->avctx->aspect_ratio; + float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio); int constraint_parameter_flag; - if(aspect_ratio==0.0) aspect_ratio= s->width / (float)s->height; //pixel aspect 1:1 (VGA) + if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA) if (s->current_picture.key_frame) { /* mpeg1 header repeated every gop */ @@ -219,7 +219,7 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s) put_bits(&s->pb, 12, s->height); for(i=1; i<15; i++){ - float error= mpeg1_aspect[i] - s->width/(s->height*aspect_ratio); + float error= mpeg1_aspect[i] - aspect_ratio; error= ABS(error); if(error < best_aspect_error){ @@ -1742,7 +1742,6 @@ static void mpeg_decode_sequence_extension(MpegEncContext *s) int bit_rate_ext, vbv_buf_ext; int frame_rate_ext_n, frame_rate_ext_d; int level, profile; - float aspect; skip_bits(&s->gb, 1); /* profil and level esc*/ profile= get_bits(&s->gb, 3); @@ -1774,9 +1773,15 @@ static void mpeg_decode_sequence_extension(MpegEncContext *s) s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO; s->avctx->sub_id = 2; /* indicates mpeg2 found */ - aspect= mpeg2_aspect[s->aspect_ratio_info]; - if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height); - else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect; + if(s->aspect_ratio_info <= 1) + s->avctx->sample_aspect_ratio= mpeg2_aspect[s->aspect_ratio_info]; + else{ + s->avctx->sample_aspect_ratio= + av_div_q( + mpeg2_aspect[s->aspect_ratio_info], + (AVRational){s->width, s->height} + ); + } if(s->avctx->debug & FF_DEBUG_PICT_INFO) printf("profile: %d, level: %d \n", profile, level); @@ -1802,8 +1807,12 @@ static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1) s1->pan_scan.width= 16*w; s1->pan_scan.height=16*h; - if(mpeg2_aspect[s->aspect_ratio_info] < 0.0) - s->avctx->aspect_ratio*= (s->width * h)/(float)(s->height * w); + if(s->aspect_ratio_info > 1) + s->avctx->sample_aspect_ratio= + av_div_q( + mpeg2_aspect[s->aspect_ratio_info], + (AVRational){w, h} + ); if(s->avctx->debug & FF_DEBUG_PICT_INFO) printf("sde w:%d, h:%d\n", w, h); @@ -2243,7 +2252,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx, s->aspect_ratio_info= get_bits(&s->gb, 4); if(s->codec_id == CODEC_ID_MPEG1VIDEO){ aspect= mpeg1_aspect[s->aspect_ratio_info]; - if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height); + if(aspect!=0.0) avctx->sample_aspect_ratio= av_d2q(aspect, 30000); } s->frame_rate_index = get_bits(&s->gb, 4); diff --git a/libavcodec/mpeg12data.h b/libavcodec/mpeg12data.h index 938897528c..dd94bb572b 100644 --- a/libavcodec/mpeg12data.h +++ b/libavcodec/mpeg12data.h @@ -416,11 +416,22 @@ static const float mpeg1_aspect[16]={ 1.2015, }; -static const float mpeg2_aspect[16]={ - 0, - 1.0, - -3.0/4.0, - -9.0/16.0, - -1.0/2.21, +static const AVRational mpeg2_aspect[16]={ + {0,1}, + {1,1}, + {4,3}, + {16,9}, + {221,100}, + {0,1}, + {0,1}, + {0,1}, + {0,1}, + {0,1}, + {0,1}, + {0,1}, + {0,1}, + {0,1}, + {0,1}, + {0,1}, }; diff --git a/libavcodec/mpeg4data.h b/libavcodec/mpeg4data.h index d48e874687..0092a9f5e0 100644 --- a/libavcodec/mpeg4data.h +++ b/libavcodec/mpeg4data.h @@ -341,23 +341,23 @@ static const uint8_t mb_type_b_tab[4][2] = { {1, 1}, {1, 2}, {1, 3}, {1, 4}, }; -static const uint16_t pixel_aspect[16][2]={ - {0, 0}, +static const AVRational pixel_aspect[16]={ + {0, 1}, {1, 1}, {12, 11}, {10, 11}, {16, 11}, {40, 33}, - {0, 0}, - {0, 0}, - {0, 0}, - {0, 0}, - {0, 0}, - {0, 0}, - {0, 0}, - {0, 0}, - {0, 0}, - {0, 0}, + {0, 1}, + {0, 1}, + {0, 1}, + {0, 1}, + {0, 1}, + {0, 1}, + {0, 1}, + {0, 1}, + {0, 1}, + {0, 1}, }; /* these matrixes will be permuted for the idct */ diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h index 74fa23d3b2..30432c1731 100644 --- a/libavcodec/mpegvideo.h +++ b/libavcodec/mpegvideo.h @@ -554,8 +554,6 @@ typedef struct MpegEncContext { int new_pred; int reduced_res_vop; int aspect_ratio_info; //FIXME remove - int aspected_width; //FIXME remove - int aspected_height; //FIXME remove int sprite_warping_accuracy; int low_latency_sprite; int data_partitioning; ///< data partitioning flag from header diff --git a/libavcodec/rational.c b/libavcodec/rational.c new file mode 100644 index 0000000000..f2a579abca --- /dev/null +++ b/libavcodec/rational.c @@ -0,0 +1,61 @@ +/* + * Rational numbers + * Copyright (c) 2003 Michael Niedermayer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/** + * @file rational.c + * Rational numbers + * @author Michael Niedermayer + */ + +//#include +#include + +#include "common.h" +#include "avcodec.h" +#include "rational.h" + +AVRational av_mul_q(AVRational b, AVRational c){ + av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX); + return b; +} + +AVRational av_div_q(AVRational b, AVRational c){ + av_reduce(&b.num, &b.den, b.num * (int64_t)c.den, b.den * (int64_t)c.num, INT_MAX); + return b; +} + +AVRational av_add_q(AVRational b, AVRational c){ + av_reduce(&b.num, &b.den, b.num * (int64_t)c.den + c.num * (int64_t)b.den, b.den * (int64_t)c.den, INT_MAX); + return b; +} + +AVRational av_sub_q(AVRational b, AVRational c){ + av_reduce(&b.num, &b.den, b.num * (int64_t)c.den - c.num * (int64_t)b.den, b.den * (int64_t)c.den, INT_MAX); + return b; +} + +AVRational av_d2q(double d, int max){ + AVRational a; + int exponent= FFMAX( (int)log2(ABS(d) + 1e-20), 0); + int64_t den= 1LL << (61 - exponent); + av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max); + + return a; +} diff --git a/libavcodec/rational.h b/libavcodec/rational.h new file mode 100644 index 0000000000..d5fc77f1a6 --- /dev/null +++ b/libavcodec/rational.h @@ -0,0 +1,53 @@ +/* + * Rational numbers + * Copyright (c) 2003 Michael Niedermayer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/** + * @file rational.h + * Rational numbers. + * @author Michael Niedermayer + */ + +#ifndef RATIONAL_H +#define RATIONAL_H + +typedef struct AVRational{ + int num; + int den; +} AVRational; + +static inline int av_cmp_q(AVRational a, AVRational b){ + const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den; + + if (tmp < 0) return -1; + else if(tmp == 0) return 0; + else return 1; +} + +static inline double av_q2d(AVRational a){ + return a.num / (double) a.den; +} + +AVRational av_mul_q(AVRational b, AVRational c); +AVRational av_div_q(AVRational b, AVRational c); +AVRational av_add_q(AVRational b, AVRational c); +AVRational av_sub_q(AVRational b, AVRational c); +AVRational av_d2q(double d, int max); + +#endif // RATIONAL_H diff --git a/libavcodec/utils.c b/libavcodec/utils.c index ac4a12fb16..f9ed4f5ab6 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -314,6 +314,7 @@ void avcodec_get_context_defaults(AVCodecContext *s){ s->me_subpel_quality=8; s->lmin= FF_QP2LAMBDA * s->qmin; s->lmax= FF_QP2LAMBDA * s->qmax; + s->sample_aspect_ratio= (AVRational){0,1}; s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS; s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS; diff --git a/libavformat/dv.c b/libavformat/dv.c index 62992fa8fc..56e3160162 100644 --- a/libavformat/dv.c +++ b/libavformat/dv.c @@ -556,10 +556,12 @@ static int dv_extract_video_info(uint8_t* frame, AVCodecContext* avctx) avctx->height = sys->height; avctx->pix_fmt = sys->pix_fmt; vsc_pack = dv_extract_pack(frame, dv_video_control); + /* MN: someone please fill in the correct SAR for dv, i dont have the standard doc so i cant, allthough its very likely the same as MPEG4 (12:11 10:11 16:11 40:33) */ if (vsc_pack && (vsc_pack[2] & 0x07) == (apt?0x02:0x07)) - avctx->aspect_ratio = 16.0 / 9.0; + avctx->sample_aspect_ratio = (AVRational){16,9}; else - avctx->aspect_ratio = 4.0 / 3.0; + avctx->sample_aspect_ratio = (AVRational){4,3}; + avctx->sample_aspect_ratio = av_div_q(avctx->sample_aspect_ratio, (AVRational){sys->width,sys->height}); size = sys->frame_size; } return size; @@ -660,7 +662,7 @@ DVMuxContext* dv_init_mux(AVFormatContext* s) c->has_audio = c->has_video = 0; c->start_time = time(NULL); c->aspect = 0; /* 4:3 is the default */ - if ((int)(vst->codec.aspect_ratio * 10) == 17) /* 16:9 */ + if ((int)(av_q2d(vst->codec.sample_aspect_ratio) * vst->codec.width / vst->codec.height * 10) == 17) /* 16:9 */ c->aspect = 0x07; if (fifo_init(&c->audio_data, 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) diff --git a/libavformat/yuv4mpeg.c b/libavformat/yuv4mpeg.c index e9f08c8343..25eef070fe 100644 --- a/libavformat/yuv4mpeg.c +++ b/libavformat/yuv4mpeg.c @@ -24,18 +24,11 @@ #ifdef CONFIG_ENCODERS - -static struct { int n; int d;} SAR[] = {{10, 11}, /* 4:3 NTSC */ - {59, 54}, /* 4:3 PAL */ - {40, 33}, /* 16:9 NTSC */ - {118, 81}, /* 16:9 PAL */ - { 1, 1}};/* should always be the last */ - static int yuv4_generate_header(AVFormatContext *s, char* buf) { AVStream *st; int width, height; - int raten, rated, aspectn, aspectd, n, i; + int raten, rated, aspectn, aspectd, n; char inter; st = s->streams[0]; @@ -44,13 +37,8 @@ static int yuv4_generate_header(AVFormatContext *s, char* buf) av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1); - for (i=0; icodec.aspect_ratio - - (float)SAR[i].n/SAR[i].d * (float)width/height) < 0.05) - break; - } - aspectn = SAR[i].n; - aspectd = SAR[i].d; + aspectn = st->codec.sample_aspect_ratio.num; + aspectd = st->codec.sample_aspect_ratio.den; inter = 'p'; /* progressive is the default */ if (st->codec.coded_frame && st->codec.coded_frame->interlaced_frame) { @@ -200,6 +188,7 @@ static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap) st->codec.pix_fmt = PIX_FMT_YUV420P; st->codec.codec_type = CODEC_TYPE_VIDEO; st->codec.codec_id = CODEC_ID_RAWVIDEO; + st->codec.sample_aspect_ratio= (AVRational){aspectn, aspectd}; return 0; }