{"id":346,"date":"2016-07-04T00:29:17","date_gmt":"2016-07-04T00:29:17","guid":{"rendered":"http:\/\/blog.arrozcru.org\/?p=346"},"modified":"2016-07-04T00:31:27","modified_gmt":"2016-07-04T00:31:27","slug":"ffglitch-jpeg-dc-coefficients","status":"publish","type":"post","link":"http:\/\/blog.arrozcru.org\/?p=346","title":{"rendered":"ffglitch jpeg dc coefficients"},"content":{"rendered":"<p>Glitching images with hex editors is fun. We touch a few bytes and the whole image gets screwed up.<\/p>\n<p>But what if we don&#8217;t want to screw up the bitstream? I want the resulting bitstream to valid, and just tweak a few values.<\/p>\n<p>I had an idea on how to do that, and I&#8217;ve spent a few hours this weekend working on a proof of concept for FFmpeg. The source code is\u00a0available here:<\/p>\n<p><a href=\"https:\/\/github.com\/ramiropolla\/ffmpeg\/tree\/gbm\">https:\/\/github.com\/ramiropolla\/ffmpeg\/tree\/gbm<\/a><\/p>\n<p>Basically it&#8217;s a three-step process. We first run FFmpeg and ask it to dump the value we&#8217;re interested in editing (only DC coefficients for jpeg images is currently implemented). We then tweak the values that were dumped in a text file. And then we re-run FFmpeg and specify the altered dump file as input for those specific values.<\/p>\n<p>The code is a huge hack. I&#8217;ve modified the jpeg decoder to dump the DC coefficients on the first run. Then I&#8217;ve modified the\u00a0bitstream reader to duplicate the data it&#8217;s reading as it goes along. When we get to the altered DC coefficients, the new values are read from the text file and written to the output bitstream.<\/p>\n<pre class=\"lang:default decode:true \">$ .\/configure --disable-everything --disable-external-libs --disable-doc --disable-programs --enable-ffmpeg --enable-decoder=mjpeg --enable-encoder=mjpeg --enable-encoder=mjpeg_glitch --enable-demuxer=image2 --enable-muxer=image2\r\n$ make\r\n$ # generate DC coefficients file\r\n$ .\/ffmpeg_g -i lena.jpg -f image2 -vcodec mjpeg_glitch -dump_mjpeg_dc dc_in.txt -y temp.jpg\r\n$ # now edit dc_in.txt (for example, using the python script provided with the source code)\r\n$ python glitch\/reverse_dc.py dc_in.txt &gt; dc_out.txt\r\n$ # rewrite the bitstream with the new DC coefficients\r\n$ .\/ffmpeg_g -i lena.jpg -f image2 -vcodec mjpeg_glitch -read_mjpeg_dc dc_out.txt -y lena_out.jpg\r\n<\/pre>\n<p>Input:<\/p>\n<p><a href=\"http:\/\/blog.arrozcru.org\/wp-content\/uploads\/2016\/07\/lena.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-348\" src=\"http:\/\/blog.arrozcru.org\/wp-content\/uploads\/2016\/07\/lena.jpg\" alt=\"lena\" width=\"512\" height=\"512\" srcset=\"http:\/\/blog.arrozcru.org\/wp-content\/uploads\/2016\/07\/lena.jpg 512w, http:\/\/blog.arrozcru.org\/wp-content\/uploads\/2016\/07\/lena-150x150.jpg 150w, http:\/\/blog.arrozcru.org\/wp-content\/uploads\/2016\/07\/lena-300x300.jpg 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/p>\n<p>Output:<\/p>\n<p><a href=\"http:\/\/blog.arrozcru.org\/wp-content\/uploads\/2016\/07\/lena_out.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-347\" src=\"http:\/\/blog.arrozcru.org\/wp-content\/uploads\/2016\/07\/lena_out.jpg\" alt=\"lena_out\" width=\"512\" height=\"512\" srcset=\"http:\/\/blog.arrozcru.org\/wp-content\/uploads\/2016\/07\/lena_out.jpg 512w, http:\/\/blog.arrozcru.org\/wp-content\/uploads\/2016\/07\/lena_out-150x150.jpg 150w, http:\/\/blog.arrozcru.org\/wp-content\/uploads\/2016\/07\/lena_out-300x300.jpg 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Glitching images with hex editors is fun. We touch a few bytes and the whole image gets screwed up. But what if we don&#8217;t want to screw up the bitstream? I want the resulting bitstream to valid, and just tweak a few values. I had an idea on how to do that, and I&#8217;ve spent &hellip; <a href=\"http:\/\/blog.arrozcru.org\/?p=346\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ffglitch jpeg dc coefficients<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-346","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/blog.arrozcru.org\/index.php?rest_route=\/wp\/v2\/posts\/346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.arrozcru.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.arrozcru.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.arrozcru.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.arrozcru.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=346"}],"version-history":[{"count":2,"href":"http:\/\/blog.arrozcru.org\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions"}],"predecessor-version":[{"id":350,"href":"http:\/\/blog.arrozcru.org\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions\/350"}],"wp:attachment":[{"href":"http:\/\/blog.arrozcru.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.arrozcru.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.arrozcru.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}