I'm still mostly thinking along the lines of poor G-sensor performance, because beyond that it could be anyone's guess as to what is going on with that handset hardware/software wise...?
____________
Photos and videos both effected? Or Videos only?
Android Forums | Samsung Galaxy S 3 - Vertical photos rotating to horizontal orientation
You could look into the exif/meta data of your photos/video file and see if there is an overriding property causing a conflict?
Some phones can have a "bad"/failing G-Sensor from the get go. A few One-X'ers, including a mod, El Presidente, had to get a new handset because of a bollocksed-up G-Sensor.
You could try a G-Sensor calibration in Safe Mode. Worth a try maybe?
I noticed in the camera's panoramic mode you can "see" when the orientation flips a little easier. The 'level line' will jump axis for you.
You could try a diagnostic application to show you current orientation values off the G-sensor. You could dial *#*#3424#*#* to use HTC's diagnostic application and give the G-Sensor test a try. It will show X Y Z values that can maybe be compared to a known normally working handset. There is other diagnostic applications to try on the market if you'd rather.
It could maybe even have something to do with your storage? Anything strange going on with your microsd card, on board storage, or file structure? Are you carrying over files or folder structure from a previous handset on your microsd card? - you could try changing the default save location to see if it acts different on one or the other?
Is the final video, when transferred to a computer, still turned around sideways? Maybe just the phone's Gallery application has an issue?