Ticket #2581: aspectRatio-0.21-fixes.patch

File aspectRatio-0.21-fixes.patch, 11.3 KB (added by patrick@…, 16 years ago)

patch against 0.21-fixes, revision #18812

  • libs/libmythtv/recordingprofile.cpp

     
    634634    };
    635635};
    636636
     637class MPEG4AspectRatio: public ComboBoxSetting, public CodecParamStorage {
     638public:
     639   MPEG4AspectRatio(const RecordingProfile& parent):
     640        ComboBoxSetting(this),
     641        CodecParamStorage(this, parent, "mpeg4aspectratio") {
     642        setLabel(QObject::tr("Aspect Ratio"));
     643        addSelection("Leave", "Leave");
     644        addSelection("4:3", "4:3");
     645        addSelection("16:9", "16:9");
     646        setValue(0);
     647        setHelpText(QObject::tr("Sets the aspect ratio of transcoded files"));
     648    };
     649};
     650
    637651class EncodingThreadCount : public SliderSetting, public CodecParamStorage
    638652{
    639653  public:
     
    800814        params->addChild(new MPEG4MinQuality(parent));
    801815        params->addChild(new MPEG4QualDiff(parent));
    802816        params->addChild(new ScaleBitrate(parent));
     817        params->addChild(new MPEG4AspectRatio(parent));
    803818
    804819        HorizontalConfigurationGroup *hq;
    805820        hq = new HorizontalConfigurationGroup(false, false);
  • programs/mythtranscode/transcode.cpp

     
    472472    int video_width = buf_size.width();
    473473    int video_height = buf_size.height();
    474474     
     475    float video_aspect = nvp->GetVideoAspect();
     476    float video_frame_rate = nvp->GetFrameRate();
     477    int newWidth = video_width;
     478    int newHeight = video_height;
     479
     480   
    475481    if (video_height == 1088) {
    476482       VERBOSE(VB_IMPORTANT, "Found video height of 1088.  This is unusual and "
    477483               "more than likely the video is actually 1080 so mythtranscode "
    478484               "will treat it as such.");
    479485    }
    480 
    481     float video_aspect = nvp->GetVideoAspect();
    482     float video_frame_rate = nvp->GetFrameRate();
    483     int newWidth = video_width;
    484     int newHeight = video_height;
    485 
     486    VERBOSE(VB_IMPORTANT, QString("Transcode: current aspect ratio: %1").arg(video_aspect));
     487   
     488    /* this allows transcoders to have fixed aspect ratios */
     489    float newAspect = video_aspect;
     490    /* these elements control the on screen cropping */
     491    int crop_top, crop_bottom, crop_left, crop_right;
     492    crop_top = crop_bottom = crop_left = crop_right = 0;
     493   
    486494    kfa_table = new QPtrList<struct kfatable_entry>;
    487 
    488495    if (fifodir == NULL)
    489496    {
    490497        if (!GetProfile(profileName, encodingType, video_height,
     
    532539                    newHeight = (int)(1.0 * 640 * actualHeight / video_width);
    533540                }
    534541            }
     542            /* hack by Patrick Wagstrom to ensure that aspect ratios are enforced
     543             * if this is set to 43 then we crop the picture to fit the aspect ratio */
     544            if (vidsetting == "MPEG-4")
     545            {
     546               const Setting* setting = profile.byName("mpeg4aspectratio");
     547               if (setting) {
     548                    if (setting->getValue() == "4:3") {
     549                        VERBOSE(VB_IMPORTANT, QString("Transcoder calls for a 4:3 aspect ratio"));
     550                        newAspect = 1.333333333333;
     551                    } else if (setting->getValue() == "16:9") {
     552                        VERBOSE(VB_IMPORTANT, QString("Transcoder calls for a 16:9 aspect ratio"));
     553                        newAspect = 1.777777777777;
     554                    } else if (setting->getValue() == "Leave") {
     555                        VERBOSE(VB_IMPORTANT, QString("Transcoder leaves aspect ratio unchanged"));
     556                    } else {
     557                        VERBOSE(VB_IMPORTANT, QString("Transcode: unable to get aspect ratio"));
     558                    }
     559                    /* support for cropping... */
     560                    if (abs(newAspect - video_aspect) > 0.01) {
     561                        /* calculate the actual pixel ratio for videos, needed
     562                         * when encoded without square pixels, such as 720x480
     563                         * NTSC captures */
     564                        float pixelRatio = float(video_width)/float(video_height)/video_aspect;
     565                        /* scale the aspect ratio by the pixel ratio for the new sizes */
     566                        float newAspectScaling = newAspect * pixelRatio;
     567                        if (abs(video_aspect - 1.77777) < 0.01) {
     568                            /* working with 16:9 source -- assume the height is correct */
     569                            newWidth = (int)(newHeight * newAspectScaling);
     570                            crop_left = (int)(video_width - video_height*newAspectScaling)/2;
     571                            crop_right = crop_left;
     572                            /* while we're at it, kill the noise on the top of th screen too */
     573                            crop_top = crop_top + 8;
     574                            VERBOSE(VB_IMPORTANT, QString("Transcode: removing top 8 pixels to get rid of garbage from Comcast"));
     575                        } else if (abs(video_aspect - 1.33333) < 0.01) {
     576                            /* working with 4:3 source -- assume the width is correct */
     577                            newHeight = (int)(newWidth / newAspectScaling);
     578                            crop_top = (int)(video_height - video_width/newAspectScaling)/2;
     579                            crop_bottom = crop_top;
     580                        } else {
     581                            VERBOSE(VB_IMPORTANT, QString("Transcode: got a video with unknown aspect ratio %1").arg(video_aspect));
     582                        }
     583                    }
     584                } else {
     585                    VERBOSE(VB_IMPORTANT, QString("Transcode: unable to get aspect ratio"));
     586                }
     587            }
    535588
    536589            if (encodingType.left(4).lower() == "mpeg")
    537590            {
     
    543596            VERBOSE(VB_IMPORTANT, QString("Resizing from %1x%2 to %3x%4")
    544597                    .arg(video_width).arg(video_height)
    545598                    .arg(newWidth).arg(newHeight));
     599            VERBOSE(VB_IMPORTANT, QString("Crop Settings %1 %2 %3 %4")
     600                    .arg(crop_left).arg(crop_right)
     601                    .arg(crop_top).arg(crop_bottom));
    546602        }
    547603        else  // lossy and no resize
    548604            nvp->SetVideoFilters(vidfilters);
     
    557613        nvr->SetOption("vbiformat", gContext->GetSetting("VbiFormat"));
    558614
    559615        nvr->SetFrameRate(video_frame_rate);
    560         nvr->SetVideoAspect(video_aspect);
     616        /* update the aspect to the new video aspect */
     617        VERBOSE(VB_IMPORTANT, QString("Container Aspect Ratio Set to: %1").arg(newAspect));
     618 
     619        nvr->SetVideoAspect(newAspect);
    561620        nvr->SetTranscoding(true);
    562621
    563622        if (vidsetting == "MPEG-4")
     
    859918            if (! nvp->WriteStoredData(outRingBuffer, (did_ff == 0),
    860919                                       timecodeOffset))
    861920            {
     921                // TODO: check to make sure this doesn't blow out the new aspect ratio
    862922                if (video_aspect != nvp->GetVideoAspect())
    863923                {
    864924                    video_aspect = nvp->GetVideoAspect();
     925
     926                    VERBOSE(VB_IMPORTANT, QString("Overriding container aspect from %1 to %2").arg(newAspect).arg(video_aspect));
    865927                    nvr->SetNewVideoParams(video_aspect);
    866928                }
    867929
     
    897959                                   video_width, video_height);
    898960                    avpicture_fill(&imageOut, frame.buf, PIX_FMT_YUV420P,
    899961                                   newWidth, newHeight);
    900                     if (video_height != 1088) {
    901                         scontext = img_resample_init(newWidth, newHeight,
    902                                                      video_width, video_height);
    903                     } else {
    904                         scontext = img_resample_full_init(newWidth, newHeight,
    905                                                      video_width, video_height,
    906                                                      0,8,0,0,0,0,0,0);
     962                    if (video_height == 1088 && crop_bottom < 8) {
     963                        crop_bottom = 8;
    907964                    }
     965                    // FIXME: Debugging code
     966                    VERBOSE(VB_IMPORTANT, QString("B Crop Settings %1 %2 %3 %4")
     967                             .arg(crop_left).arg(crop_right)
     968                             .arg(crop_top).arg(crop_bottom));
     969 
     970                    scontext = img_resample_full_init(newWidth, newHeight,
     971                                                      video_width, video_height,
     972                                                      crop_top, crop_bottom,
     973                                                      crop_left, crop_right,
     974                                                      0,0,0,0);
    908975                    img_resample(scontext, &imageOut, &imageIn);
    909976                    img_resample_close(scontext);
    910977                }
     
    923990                    (frame.timecode - lasttimecode - (int)vidFrameTime);
    924991            }
    925992
    926             if (video_aspect != nvp->GetVideoAspect())
     993            if (newAspect != nvp->GetVideoAspect())
    927994            {
    928                 video_aspect = nvp->GetVideoAspect();
    929                 nvr->SetNewVideoParams(video_aspect);
     995                // used to check video aspect
     996                // video_aspect = nvp->GetVideoAspect();
     997                nvr->SetNewVideoParams(newAspect);
    930998            }
    931999
    9321000
     
    9551023                avpicture_fill(&imageOut, frame.buf, PIX_FMT_YUV420P,
    9561024                               newWidth, newHeight);
    9571025                if (video_height != 1088) {
    958                     scontext = img_resample_init(newWidth, newHeight,
    959                                                  video_width, video_height);
     1026                    // FIXME: Debugging code
     1027                    /* VERBOSE(VB_IMPORTANT, QString("XY: Crop Settings %1 %2 %3 %4")
     1028                             .arg(crop_left).arg(crop_right)
     1029                             .arg(crop_top).arg(crop_bottom)); */
     1030                    scontext = img_resample_full_init(newWidth, newHeight,
     1031                                                      video_width, video_height,
     1032                                                      crop_top, crop_bottom,
     1033                                                      crop_left, crop_right,
     1034                                                      0,0,0,0);
    9601035                }
    9611036                else
    9621037                {
     1038                    // FIXME: Debugging code
     1039                    /* VERBOSE(VB_IMPORTANT, QString("XX: Crop Settings %1 %2 %3 %4")
     1040                             .arg(crop_left).arg(crop_right)
     1041                             .arg(crop_top).arg(crop_bottom)); */
     1042                    /* scontext = img_resample_full_init(newWidth, newHeight,
     1043                                                      video_width, video_height,
     1044                                                      crop_top, crop_bottom,
     1045                                                      crop_left, crop_right,
     1046                                                      0,0,0,0); */
    9631047                    scontext = img_resample_full_init(newWidth, newHeight,
    9641048                                                      video_width, video_height,
    9651049                                                      0,8,0,0,0,0,0,0);