Wednesday, February 20, 2008

x264 troubles..ends well

Anyone who reads my blog knows that although I am a loyal Penguin, I'm all about getting the work done expeditiously and distributing my videos to the widest array of formats possible. One of those formats is Apple's Quicktime. And for me, a rendered Quicktime usually ends up as an MPEG4 video in iTunes. With Apple announcing that the new Apple TV 2.0 displays web content as well as iTunes podcasts, higher definition content on iTunes is poised for much growth.

Therefore, it is in that light that I have tasked myself to encode and share some of my band videos to iTunes in glorious (or in the case of my band, un-glorious) DVD resolution. And while I'm at it, I should use tasty H.264 compression for maximum quality at a minimum file size. But of course, this seemingly simple task was much easier said than done.

For me, the rub is that encoding to x264 in Cinelerra is broken. I was getting this error when encoding to H.264:
x264 [error]: no ratecontrol method specified

I had seen this error a few weeks back and emailed the Cinelerra CVS board, but no one had a clue. But tonight, I was going to figure out what the hell was going on. So my first stop was to try encoding to h264 with my old mainstay, ffmpeg. Alas, my friend through thick and thin segfaulted:
[root@ogre 20080206]# ffmpeg -i StormPigs20080206.mpg -an -pass 1 -vcodec h264 -b 1500 -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -me epzs -subq 1 -trellis 0 -refs 1 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 1500 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 output.mp4
FFmpeg version SVN-r8876,
Copyright (c) 2000-2007 Fabrice Bellard, et al.
configuration: --prefix=/usr --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --shlibdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --extra-cflags=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic --enable-libmp3lame --enable-libogg --enable-libtheora --enable-libvorbis --enable-libfaad --enable-libfaac --enable-libgsm --enable-xvid --enable-x264 --enable-liba52 --enable-liba52bin --enable-libdts --enable-pp --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-opts --disable-strip
libavutil version: 49.4.0
libavcodec version: 51.40.4
libavformat version: 51.12.1
built on May 3 2007 12:41:19, gcc: 4.1.2 20070424 (Red Hat 4.1.2-11)
Input #0, mpeg, from 'StormPigs20080206.mpg':
Duration: 01:50:37.0, start: 0.500000, bitrate: 4677 kb/s
Stream #0.0[0x1e0]: Video: mpeg2video, yuv420p, 720x480, 9000 kb/s, 29.97 fps(r)
Stream #0.1[0x80]: Audio: ac3, 48000 Hz, stereo, 448 kb/s
Output #0, mp4, to 'output.mp4':
Stream #0.0: Video: h264, yuv420p, 720x480, q=10-51, pass 1, 1 kb/s, 29.97 fps(c)
Stream mapping:
Stream #0.0 -> #0.0
Segmentation fault


FFmpeg had crapped out, so I gave avidemux2 a shot. When I tried to encode using either command line x264 or x264 encoding through avidemux2, I would get this error:
x264 [error]: width or height not divisible by 2 (480x1)

Update 2009/02/12
Here's a more recent entry regarding proper divisors for x264 compression:
http://crazedmuleproductions.blogspot.com/2009/01/high-quality-h264-output.html
*** end update ***

Well..the video is 720x480. Certainly those two numbers are divisible by two, so what gives with the logic of that error?

After Googling and trying various tweaks for about an hour, it occurred to me that there seemed to be no rhyme or reason to the error I was seeing. With that thought in mind, I decided to remove the x264 package entirely. I had installed the package from freshrpms:

[root@ogre 20080206]# yum remove x264*
Loading "installonlyn" plugin

Setting up
Remove Process

Resolving Dependencies
-->
Running transaction check

---> Package x264.x86_64 0:0.0.0-0.3.20070529.fc7 set to be erased

---> Package x264-devel.x86_64 0:0.0.0-0.3.20070529.fc7 set to be erased


Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
============================================================================= Removing:
x264 x86_64 0.0.0-0.3.20070529.fc7 installed 1.0 M

x264-devel x86_64 0.0.0-0.3.20070529.fc7 installed 712 k


Hmmm. Looking at the version number, it seemed like the x264 from FreshRPMs was a VERY early version number. Let me see if another repository has a different version. Luckily, Livna had some love waiting for me:
[root@ogre 20080206]# yum install x264* --disablerepo=freshrpms
Loading "installonlyn" plugin

Setting up Install Process

Parsing package install arguments

Resolving Dependencies
-->
Running transaction check

---> Package x264.x86_64 0:0-0.8.20061028.lvn7 set to be updated
---> Package x264-devel.x86_64 0:0-0.8.20061028.lvn7 set to be updated

Dependencies Resolved =============================================================================
Package Arch Version Repository Size
============================================================================= Installing:
x264 x86_64 0-0.8.20061028.lvn7 livna 227 k

x264-devel x86_64 0-0.8.20061028.lvn7 livna 15 k


Now that version looks a little more recent, yes? With the new Livna repository x264 installed, I tried my render using the shortest path possible: avidemux2. Lo and behold, x264 no longer complained about my height and width not being divisible by 2! Better yet, I was able to render a complete file using avidemux2's two pass defaults without any errors! Some folks might appreciate seeing the verbose output:
**saving:**
Output format:7
AVI family
Enc X264, using /mnt/videos/20070912/StormPigs20070912.mp4.stat as logfile

X264 Encoder ready , w: 720 h:480 mode:3X264 pass 1
x264 codec using /mnt/videos/20070912/StormPigs20070912.mp4.stat as statfile
Opening X264 for 720 x 480
vui.i_sar_width = 1
vui.i_sar_height = 1
rc.f_qcompress = 0.60
analyse.i_direct_mv_pred = 1
rc.i_qp_min = 10
rc.i_qp_max = 51
rc.i_qp_step = 4
i_frame_reference = 1
i_scenecut_threshold = 40
i_keyint_min = 25
i_keyint_max = 250
i_bframe = 2
i_bframe_bias = 0
b_bframe_pyramid = 0
analyse. b_bidir_me = 0
b_bframe_adaptive = 1
analyse.b_weighted_bipred = 0
b_cabac = 1
analyse.i_trellis = 0
analyse.i_subpel_refine = 2
analyse.b_chroma_me = 1
b_deblocking_filter = 1
i_deblocking_filter_alphac0 = 0
i_deblocking_filter_beta = 0
analyse.i_me_method = 0
analyse.i_me_range = 16
analyse.b_transform_8x8 = 0
analyse.b_mixed_references = 0
analyse.i_noise_reduction = 0
x264 [info]: using SAR=1/1
x264 [info]: using cpu capabilities MMX MMXEXT SSE SSE2
X264 init ok (atom mode : 1)
X264 using 4 threads
Nb nal :3
?? type :6 in nal 0
X264 :Unknown image type:0
X264 :Unknown image type:0

Starting pass 2 (720x480)
[x264] Size 700, average bitrate 1007301 kb/s
x264 [info]: slice I:703 Avg QP: 0.00 size:153296 PSNR Mean Y:70.14 U:71.46 V:71.57 Avg:70.54 Global:67.35
x264 [info]: slice P:124246 Avg QP: 2.00 size: 98655 PSNR Mean Y:59.56 U:60.40 V:60.59 Avg:59.82 Global:57.88
x264 [info]: slice B:49758 Avg QP: 4.00 size: 59273 PSNR Mean Y:66.82 U:67.47 V:67.73 Avg:67.04 Global:57.02
x264 [info]: mb I I16..4: 19.0% 0.0% 81.0%
x264 [info]: mb P I16..4: 9.0% 0.0% 0.0% P16..4: 85.4% 0.0% 0.0% 0.0% 0.0% skip: 5.6%
x264 [info]: mb B I16..4: 4.3% 0.0% 0.0% B16..8: 39.5% 0.0% 0.0% direct:30.0% skip:26.3%
x264 [info]: SSIM Mean Y:0.9992809
x264 [info]: PSNR Mean Y:61.673 U:62.456 V:62.668 Avg:61.920 Global:57.635 kb/s:21017.02

X264 dual size: 700 MB
X264 pass 2, using bitrate of 1007
x264 codec using /mnt/videos/20070912/StormPigs20070912.mp4.stat as statfile
Opening X264 for 720 x 480
vui.i_sar_width = 1
vui.i_sar_height = 1
rc.f_qcompress = 0.60
analyse.i_direct_mv_pred = 1
rc.i_qp_min = 10
rc.i_qp_max = 51
rc.i_qp_step = 4
i_frame_reference = 1
i_scenecut_threshold = 40
i_keyint_min = 25
i_keyint_max = 250
i_bframe = 2
i_bframe_bias = 0
b_bframe_pyramid = 0
analyse. b_bidir_me = 0
b_bframe_adaptive = 1
analyse.b_weighted_bipred = 1
b_cabac = 1
analyse.i_trellis = 2
analyse.i_subpel_refine = 5
analyse.b_chroma_me = 1
b_deblocking_filter = 1
i_deblocking_filter_alphac0 = 0
i_deblocking_filter_beta = 0
analyse.i_me_method = 1
analyse.i_me_range = 16
analyse.b_transform_8x8 = 1
analyse.b_mixed_references = 0
analyse.i_noise_reduction = 0
_8x8P is on
_8x8B is on
_8x8I is on
_4x4I is on
x264 [info]: using SAR=1/1
x264 [info]: using cpu capabilities MMX MMXEXT SSE SSE2
X264 init ok (atom mode : 1)
X264 using 4 threads
Nb nal :3
?? type :6 in nal 0
x264 has 39 extra bytes
We have extradata for video in copy mode (39)
X264 :Unknown image type:0
X264 :Unknown image type:0
[Bridge] Going to time 0

Syncing on 8192
Sync found at offset 0
A52 sync found at 0 + 0
[Bridge] Starting with time 0, shift 0
[Bridge] Ending with time 0, sample 0
[Bridge] Going to time 0

Syncing on 8192
Sync found at offset 0
A52 sync found at 0 + 0
[Bridge] Going to time 0

Syncing on 8192
Sync found at offset 0
A52 sync found at 0 + 0
[FAAC] : Sample input:2048, max byte output1536
[Faac] Initialized :
[Faac]Version : 1.25
[Faac]Bitrate : 192000
[Faac]Mpeg2 (1)/4(0) : 0
[Faac]Use lfe ) : 0
[Faac]Sample output : 1024
[Faac]Bitrate : 384000
[LavFormat] Bitrate 384
Output #0, mp4, to '/mnt/videos/20070912/StormPigs20070912.mp4':
Stream #0.0, 29.97 fps(c): Video: 0x0000, 720x480, q=2-31, 2000 kb/s
Stream #0.1: Audio: 0x0000, 48000 Hz, stereo, 384 kb/s
lavformat mpeg muxer initialized
PacketQueue MP4 audioQ created
[AudioQueueThread] Starting
DMX_audio Going out of bound (position : 326445000 asked 5000 end326446848)
DMX_audio Going out of bound (position : 326446848 asked 5000 end326446848)
**PKTZ:READ ERROR
**END OF AUDIO STREAM
EditorPacket:Read failed; retrying (were at seg 0sample 279810048
/ 279814208)
EditorPacket : End of *last* stream
read failed, end of stream ?
EditorPacket:Read failed; retrying (were at seg 0sample 279810048
/ 279814208)
EditorPacket : End of *last* stream
read failed, end of stream ?
[Bridge] End of stream
EditorPacket:Read failed; retrying (were at seg 0sample 279810048
/ 279814208)
EditorPacket : End of *last* stream
read failed, end of stream ?
[Bridge] End of stream
EditorPacket:Read failed; retrying (were at seg 0sample 279810048
/ 279814208)
EditorPacket : End of *last* stream
read failed, end of stream ?
[Bridge] End of stream
[bridge] No data in 0 max 24448 out 0
[AudioQueueThread] Exiting
[AudioThread] Target 4294901760, got 279810048, 0.065149 %
LavMuxer:Audio DTS is too low 5829354666 / 5829362696!
x264 [error]: 2nd pass has more frames than 1st pass (174709)
x264 [error]: continuing anyway, at constant QP=22
x264 [error]: disabling adaptive B-frames
LavMuxer:Audio DTS is too low 5829354666 / 5829396062!
x264 [info]: slice I:703 Avg QP:18.35 size: 28210 PSNR Mean Y:52.78 U:53.43 V:54.33 Avg:53.09 Global:47.62
x264 [info]: slice P:124248 Avg QP:21.42 size: 5232 PSNR Mean Y:47.14 U:47.26 V:48.82 Avg:47.39 Global:44.70
x264 [info]: slice B:49758 Avg QP:21.06 size: 1286 PSNR Mean Y:58.14 U:58.09 V:59.69 Avg:58.30 Global:45.20
x264 [info]: mb I I16..4: 17.5% 42.4% 40.1%
x264 [info]: mb P I16..4: 2.4% 9.0% 0.2% P16..4: 39.2% 12.0% 2.3% 0.0% 0.0% skip:34.8%
x264 [info]: mb B I16..4: 0.1% 1.2% 0.0% B16..8: 7.7% 0.4% 0.8% direct:14.5% skip:75.3%
x264 [info]: 8x8 transform intra:76.4% inter:65.0%
x264 [info]: SSIM Mean Y:0.9828109
x264 [info]: PSNR Mean Y:50.297 U:50.371 V:51.938 Avg:50.519 Global:44.843 kb/s:1007.06
[Bridge] Destroying bridge
[FAAC] Deleting faac
yv12close called
ba010000 -> ba010000


And best yet, the rendered x264 file loads into iTunes! Hooray!

Of course, I haven't tried actually bringing it to an iPod, but that will be the next thing to do. I'll let you know.
cheers,
da mule

Reference
Here are Apple's specifications for making a podcast:
http://www.apple.com/itunes/store/podcaststechspecs.html

No comments: