Index: libs/libmythtv/recordingprofile.cpp
===================================================================
--- libs/libmythtv/recordingprofile.cpp	(revision 18813)
+++ libs/libmythtv/recordingprofile.cpp	(working copy)
@@ -634,6 +634,20 @@
     };
 };
 
+class MPEG4AspectRatio: public ComboBoxSetting, public CodecParamStorage {
+public:
+   MPEG4AspectRatio(const RecordingProfile& parent):
+        ComboBoxSetting(this),
+        CodecParamStorage(this, parent, "mpeg4aspectratio") {
+        setLabel(QObject::tr("Aspect Ratio"));
+        addSelection("Leave", "Leave");
+        addSelection("4:3", "4:3");
+        addSelection("16:9", "16:9");
+        setValue(0);
+        setHelpText(QObject::tr("Sets the aspect ratio of transcoded files"));
+    };
+};
+
 class EncodingThreadCount : public SliderSetting, public CodecParamStorage
 {
   public:
@@ -800,6 +814,7 @@
         params->addChild(new MPEG4MinQuality(parent));
         params->addChild(new MPEG4QualDiff(parent));
         params->addChild(new ScaleBitrate(parent));
+        params->addChild(new MPEG4AspectRatio(parent));
 
         HorizontalConfigurationGroup *hq;
         hq = new HorizontalConfigurationGroup(false, false);
Index: programs/mythtranscode/transcode.cpp
===================================================================
--- programs/mythtranscode/transcode.cpp	(revision 18813)
+++ programs/mythtranscode/transcode.cpp	(working copy)
@@ -472,19 +472,26 @@
     int video_width = buf_size.width();
     int video_height = buf_size.height();
      
+    float video_aspect = nvp->GetVideoAspect();
+    float video_frame_rate = nvp->GetFrameRate();
+    int newWidth = video_width;
+    int newHeight = video_height;
+
+    
     if (video_height == 1088) {
        VERBOSE(VB_IMPORTANT, "Found video height of 1088.  This is unusual and "
                "more than likely the video is actually 1080 so mythtranscode "
                "will treat it as such.");
     }
-
-    float video_aspect = nvp->GetVideoAspect();
-    float video_frame_rate = nvp->GetFrameRate();
-    int newWidth = video_width;
-    int newHeight = video_height;
-
+    VERBOSE(VB_IMPORTANT, QString("Transcode: current aspect ratio: %1").arg(video_aspect));
+    
+    /* this allows transcoders to have fixed aspect ratios */
+    float newAspect = video_aspect;
+    /* these elements control the on screen cropping */
+    int crop_top, crop_bottom, crop_left, crop_right;
+    crop_top = crop_bottom = crop_left = crop_right = 0;
+    
     kfa_table = new QPtrList<struct kfatable_entry>;
-
     if (fifodir == NULL)
     {
         if (!GetProfile(profileName, encodingType, video_height,
@@ -532,6 +539,52 @@
                     newHeight = (int)(1.0 * 640 * actualHeight / video_width);
                 }
             }
+            /* hack by Patrick Wagstrom to ensure that aspect ratios are enforced
+             * if this is set to 43 then we crop the picture to fit the aspect ratio */
+            if (vidsetting == "MPEG-4")
+            {
+               const Setting* setting = profile.byName("mpeg4aspectratio");
+               if (setting) {
+                    if (setting->getValue() == "4:3") {
+                        VERBOSE(VB_IMPORTANT, QString("Transcoder calls for a 4:3 aspect ratio"));
+                        newAspect = 1.333333333333;
+                    } else if (setting->getValue() == "16:9") {
+                        VERBOSE(VB_IMPORTANT, QString("Transcoder calls for a 16:9 aspect ratio"));
+                        newAspect = 1.777777777777;
+                    } else if (setting->getValue() == "Leave") {
+                        VERBOSE(VB_IMPORTANT, QString("Transcoder leaves aspect ratio unchanged"));
+                    } else {
+                        VERBOSE(VB_IMPORTANT, QString("Transcode: unable to get aspect ratio"));
+                    }
+                    /* support for cropping... */
+                    if (abs(newAspect - video_aspect) > 0.01) {
+                        /* calculate the actual pixel ratio for videos, needed
+                         * when encoded without square pixels, such as 720x480
+                         * NTSC captures */
+                        float pixelRatio = float(video_width)/float(video_height)/video_aspect;
+                        /* scale the aspect ratio by the pixel ratio for the new sizes */
+                        float newAspectScaling = newAspect * pixelRatio;
+                        if (abs(video_aspect - 1.77777) < 0.01) {
+                            /* working with 16:9 source -- assume the height is correct */
+                            newWidth = (int)(newHeight * newAspectScaling);
+                            crop_left = (int)(video_width - video_height*newAspectScaling)/2;
+                            crop_right = crop_left;
+                            /* while we're at it, kill the noise on the top of th screen too */
+                            crop_top = crop_top + 8;
+                            VERBOSE(VB_IMPORTANT, QString("Transcode: removing top 8 pixels to get rid of garbage from Comcast"));
+                        } else if (abs(video_aspect - 1.33333) < 0.01) {
+                            /* working with 4:3 source -- assume the width is correct */
+                            newHeight = (int)(newWidth / newAspectScaling);
+                            crop_top = (int)(video_height - video_width/newAspectScaling)/2;
+                            crop_bottom = crop_top;
+                        } else {
+                            VERBOSE(VB_IMPORTANT, QString("Transcode: got a video with unknown aspect ratio %1").arg(video_aspect));
+                        }
+                    }
+                } else {
+                    VERBOSE(VB_IMPORTANT, QString("Transcode: unable to get aspect ratio"));
+                }
+            }
 
             if (encodingType.left(4).lower() == "mpeg")
             {
@@ -543,6 +596,9 @@
             VERBOSE(VB_IMPORTANT, QString("Resizing from %1x%2 to %3x%4")
                     .arg(video_width).arg(video_height)
                     .arg(newWidth).arg(newHeight));
+            VERBOSE(VB_IMPORTANT, QString("Crop Settings %1 %2 %3 %4")
+                    .arg(crop_left).arg(crop_right)
+                    .arg(crop_top).arg(crop_bottom));
         }
         else  // lossy and no resize
             nvp->SetVideoFilters(vidfilters);
@@ -557,7 +613,10 @@
         nvr->SetOption("vbiformat", gContext->GetSetting("VbiFormat"));
 
         nvr->SetFrameRate(video_frame_rate);
-        nvr->SetVideoAspect(video_aspect);
+        /* update the aspect to the new video aspect */
+        VERBOSE(VB_IMPORTANT, QString("Container Aspect Ratio Set to: %1").arg(newAspect));
+ 
+        nvr->SetVideoAspect(newAspect);
         nvr->SetTranscoding(true);
 
         if (vidsetting == "MPEG-4")
@@ -859,9 +918,12 @@
             if (! nvp->WriteStoredData(outRingBuffer, (did_ff == 0),
                                        timecodeOffset))
             {
+                // TODO: check to make sure this doesn't blow out the new aspect ratio
                 if (video_aspect != nvp->GetVideoAspect())
                 {
                     video_aspect = nvp->GetVideoAspect();
+
+                    VERBOSE(VB_IMPORTANT, QString("Overriding container aspect from %1 to %2").arg(newAspect).arg(video_aspect));
                     nvr->SetNewVideoParams(video_aspect);
                 }
 
@@ -897,14 +959,19 @@
                                    video_width, video_height);
                     avpicture_fill(&imageOut, frame.buf, PIX_FMT_YUV420P,
                                    newWidth, newHeight);
-                    if (video_height != 1088) {
-                        scontext = img_resample_init(newWidth, newHeight,
-                                                     video_width, video_height);
-                    } else {
-                        scontext = img_resample_full_init(newWidth, newHeight,
-                                                     video_width, video_height,
-                                                     0,8,0,0,0,0,0,0);
+                    if (video_height == 1088 && crop_bottom < 8) {
+                        crop_bottom = 8;
                     }
+                    // FIXME: Debugging code
+                    VERBOSE(VB_IMPORTANT, QString("B Crop Settings %1 %2 %3 %4")
+                             .arg(crop_left).arg(crop_right)
+                             .arg(crop_top).arg(crop_bottom));
+ 
+                    scontext = img_resample_full_init(newWidth, newHeight,
+                                                      video_width, video_height,
+                                                      crop_top, crop_bottom,
+                                                      crop_left, crop_right,
+                                                      0,0,0,0);
                     img_resample(scontext, &imageOut, &imageIn);
                     img_resample_close(scontext);
                 }
@@ -923,10 +990,11 @@
                     (frame.timecode - lasttimecode - (int)vidFrameTime);
             }
 
-            if (video_aspect != nvp->GetVideoAspect())
+            if (newAspect != nvp->GetVideoAspect())
             {
-                video_aspect = nvp->GetVideoAspect();
-                nvr->SetNewVideoParams(video_aspect);
+                // used to check video aspect
+                // video_aspect = nvp->GetVideoAspect();
+                nvr->SetNewVideoParams(newAspect);
             }
 
 
@@ -955,11 +1023,27 @@
                 avpicture_fill(&imageOut, frame.buf, PIX_FMT_YUV420P,
                                newWidth, newHeight);
                 if (video_height != 1088) {
-                    scontext = img_resample_init(newWidth, newHeight,
-                                                 video_width, video_height);
+                    // FIXME: Debugging code
+                    /* VERBOSE(VB_IMPORTANT, QString("XY: Crop Settings %1 %2 %3 %4")
+                             .arg(crop_left).arg(crop_right)
+                             .arg(crop_top).arg(crop_bottom)); */
+                    scontext = img_resample_full_init(newWidth, newHeight,
+                                                      video_width, video_height,
+                                                      crop_top, crop_bottom,
+                                                      crop_left, crop_right,
+                                                      0,0,0,0);
                 }
                 else
                 {
+                    // FIXME: Debugging code
+                    /* VERBOSE(VB_IMPORTANT, QString("XX: Crop Settings %1 %2 %3 %4")
+                             .arg(crop_left).arg(crop_right)
+                             .arg(crop_top).arg(crop_bottom)); */
+                    /* scontext = img_resample_full_init(newWidth, newHeight,
+                                                      video_width, video_height,
+                                                      crop_top, crop_bottom,
+                                                      crop_left, crop_right,
+                                                      0,0,0,0); */
                     scontext = img_resample_full_init(newWidth, newHeight,
                                                       video_width, video_height,
                                                       0,8,0,0,0,0,0,0);

