[Ffmpeg-devel] [PATCH] TIFF encoder (Google SoC qualification task)

Kamil Nowosad k.nowosad
Fri Apr 6 14:17:32 CEST 2007


Hi

On Thu, Apr 05, 2007 at 09:03:59PM +0200, Michael Niedermayer wrote:
> so iam fine and would prefer if we would return to the original suggestion
> from you guys that planar is converted to whatever weird packed format tiff
> requires
> sorry for the bad pix_fmt suggestion i thought tiff would be using one of
> the standard packed yuv variants

Done.
Patch attached.

-- 
Best regards,
Kamil Nowosad
-------------- next part --------------
Index: libavcodec/tiffenc.c
===================================================================
--- libavcodec/tiffenc.c	(wersja 8629)
+++ libavcodec/tiffenc.c	(kopia robocza)
@@ -57,6 +57,7 @@
     uint8_t **buf;                      ///< actual position in buffer
     uint8_t *buf_start;                 ///< pointer to first byte in buffer
     int buf_size;                       ///< buffer size
+    uint16_t subsampling[2];            ///< YUV subsampling factors
 } TiffEncoderContext;
 
 
@@ -173,6 +174,21 @@
     }
 }
 
+static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum)
+{
+    AVFrame *p = (AVFrame*) &s->picture;
+    int i, j, k;
+    int w = (s->width - 1) / s->subsampling[0] + 1;
+    for (i = 0; i < w; i++){
+        for (j = 0; j < s->subsampling[1]; j++)
+            for (k = 0; k < s->subsampling[0]; k++)
+                *dst++ = p->data[0][(lnum + j) * p->linesize[0] +
+                                    i * s->subsampling[0] + k];
+        *dst++ = p->data[1][lnum / s->subsampling[1] * p->linesize[1] + i];
+        *dst++ = p->data[2][lnum / s->subsampling[1] * p->linesize[2] + i];
+    }
+}
+
 static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
                         int buf_size, void *data)
 {
@@ -190,6 +206,9 @@
     uint32_t res[2] = { 72, 1 };        // image resolution (72/1)
     static const uint16_t bpp_tab[] = { 8, 8, 8, 8 };
     int ret = -1;
+    int is_yuv = 0;
+    uint8_t *yuv_line;
+    int shift_h, shift_v;
 
     s->buf_start = buf;
     s->buf = &ptr;
@@ -232,12 +251,28 @@
         s->bpp = 1;
         s->invert = 0;
         break;
+    case PIX_FMT_YUV420P:
+    case PIX_FMT_YUV422P:
+    case PIX_FMT_YUV444P:
+    case PIX_FMT_YUV410P:
+    case PIX_FMT_YUV411P:
+        s->invert = 6;
+        avcodec_get_chroma_sub_sample(avctx->pix_fmt,
+                &shift_h, &shift_v);
+        s->bpp = 8 + (16 >> (shift_h + shift_v));
+        s->subsampling[0] = 1 << shift_h;
+        s->subsampling[1] = 1 << shift_v;
+        s->bpp_tab_size = 3;
+        s->compr = TIFF_RAW;
+        is_yuv = 1;
+        break;
     default:
         av_log(s->avctx, AV_LOG_ERROR,
                "This colors format is not supported\n");
         return -1;
     }
-    s->bpp_tab_size = (s->bpp >> 3);
+    if (!is_yuv)
+        s->bpp_tab_size = (s->bpp >> 3);
 
     if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE)
         //best choose for DEFLATE
@@ -245,6 +280,9 @@
     else
         s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1);     // suggest size of strip
 
+    if (is_yuv) /// round up the rps to multiple of s->subsampling[1]
+        s->rps = ((s->rps -1) / s->subsampling[1] + 1) * s->subsampling[1];
+
     strips = (s->height - 1) / s->rps + 1;
 
     if (check_size(s, 8))
@@ -260,7 +298,13 @@
     strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips);
     strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips);
 
-    bytes_per_row = (s->width * s->bpp + 7) >> 3;
+    if (is_yuv){
+        bytes_per_row = (((s->width - 1)/s->subsampling[0] + 1) * s->bpp
+                        * s->subsampling[0] * s->subsampling[1]) >> 3;
+        yuv_line = av_malloc(bytes_per_row);
+    }
+    else
+        bytes_per_row = (s->width * s->bpp + 7) >> 3;
 
 #ifdef CONFIG_ZLIB
     if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
@@ -292,8 +336,15 @@
             if (strip_sizes[i / s->rps] == 0) {
                 strip_offsets[i / s->rps] = ptr - buf;
             }
-            if ((n = encode_strip(s, p->data[0] + i * p->linesize[0]
-                                  , ptr, bytes_per_row, s->compr)) < 0) {
+            if (is_yuv){
+                 pack_yuv(s, yuv_line, i);
+                 n = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr);
+                 i += s->subsampling[1] - 1;
+            }
+            else
+                n = encode_strip(s, p->data[0] + i * p->linesize[0],
+                        ptr, bytes_per_row, s->compr);
+            if (n < 0) {
                 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
                 goto fail;
             }
@@ -336,6 +387,12 @@
         }
         add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal);
     }
+    if (is_yuv){
+        /** according to CCIR Recommendation 601.1 */
+        uint32_t refbw[12] = {15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1};
+        add_entry(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT,    2, s->subsampling);
+        add_entry(s, TIFF_REFERENCE_BW,      TIFF_RATIONAL, 6, refbw);
+    }
     bytestream_put_le32(&offset, ptr - buf);    // write offset to dir
 
     if (check_size(s, 6 + s->num_entries * 12))
@@ -349,6 +406,8 @@
 fail:
     av_free(strip_sizes);
     av_free(strip_offsets);
+    if (is_yuv)
+        av_free(yuv_line);
     return ret;
 }
 
@@ -366,6 +425,9 @@
     .pix_fmts =
         (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
                               PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
+                              PIX_FMT_YUV420P, PIX_FMT_YUV422P,
+                              PIX_FMT_YUV444P, PIX_FMT_YUV410P,
+                              PIX_FMT_YUV411P
                               -1}
 
 };



More information about the ffmpeg-devel mailing list