[Ffmpeg-devel] [PATCH] TIFF encoder (Google SoC qualification task)
Kamil Nowosad
k.nowosad
Fri Apr 6 22:20:56 CEST 2007
Hi
On Fri, Apr 06, 2007 at 04:37:18PM +0200, Michael Niedermayer wrote:
> > +static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum)
> > +{
>
> > + AVFrame *p = (AVFrame*) &s->picture;
>
> senseless cast
removed
> > + 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];
> > + }
>
> uint8_t *pu= p->data[1] + lnum / s->subsampling[1] * p->linesize[1];
> uint8_t *pv= p->data[2] + lnum / s->subsampling[1] * p->linesize[2];
> before the loop would avoid / and * inside it
done
> [...]
> > @@ -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;
>
> this conflicts with the photometric interpretation patch, that is
> we cannot apply this and the photometric interpretation patch no matter
> in which order, please send patches against svn + all previous approved
> patches which touch the same code
I've attached the new version of the p.i. patch. The old one is outdated
due to tiff_add_entry function changes. The yuv patch doesn't conflict with
the coder_type patch.
> > + 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;
>
> silently overriding the compression selected by the user is not ok,
> if the combination isnt supported just fail (return -1)
Changed. Now yuv works with all algorithms.
> > @@ -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;
>
> if s->subsampling[*] is 1 by default then this and the previous if can be
> simplified
Changed.
> > + if (is_yuv)
> > + av_free(yuv_line);
>
> hmm, id rather set yuv_line to NULL and remove that if()
>
OK. Changed.
--
Best regards,
Kamil Nowosad
-------------- next part --------------
--- tiffenc-orig.c 2007-04-06 20:43:58.000000000 +0200
+++ tiffenc.c 2007-04-06 21:55:52.000000000 +0200
@@ -63,6 +63,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;
@@ -179,6 +180,23 @@
}
}
+static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum)
+{
+ AVFrame *p = &s->picture;
+ int i, j, k;
+ int w = (s->width - 1) / s->subsampling[0] + 1;
+ uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]];
+ uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]];
+ 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++ = *pu++;
+ *dst++ = *pv++;
+ }
+}
+
static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
int buf_size, void *data)
{
@@ -196,6 +214,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 = NULL;
+ int shift_h, shift_v;
s->buf_start = buf;
s->buf = &ptr;
@@ -223,6 +244,8 @@
s->width = avctx->width;
s->height = avctx->height;
+ s->subsampling[0] = 1;
+ s->subsampling[1] = 1;
switch (avctx->pix_fmt) {
case PIX_FMT_RGB24:
@@ -245,18 +268,35 @@
s->bpp = 1;
s->photometric_interpretation = 0;
break;
+ case PIX_FMT_YUV420P:
+ case PIX_FMT_YUV422P:
+ case PIX_FMT_YUV444P:
+ case PIX_FMT_YUV410P:
+ case PIX_FMT_YUV411P:
+ s->photometric_interpretation = 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;
+ is_yuv = 1;
+ break;
default:
av_log(s->avctx, AV_LOG_ERROR,
"This colors format is not supported\n");
return -1;
}
+ if (!is_yuv)
s->bpp_tab_size = (s->bpp >> 3);
if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE)
//best choose for DEFLATE
- s->rps = s->height;
- else
+ s->rps = ((s->height - 1) / s->subsampling[1] + 1) * s->subsampling[1];
+ else {
s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip
+ s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1]; // round rps up
+ }
strips = (s->height - 1) / s->rps + 1;
@@ -273,7 +313,15 @@
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;
+ bytes_per_row = (((s->width - 1)/s->subsampling[0] + 1) * s->bpp
+ * s->subsampling[0] * s->subsampling[1] + 7) >> 3;
+ if (is_yuv){
+ yuv_line = av_malloc(bytes_per_row);
+ if (yuv_line == NULL){
+ av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n");
+ goto fail;
+ }
+ }
#ifdef CONFIG_ZLIB
if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) {
@@ -286,6 +334,12 @@
strip_offsets[0] = ptr - buf;
zn = 0;
for (j = 0; j < s->rps; j++) {
+ if (is_yuv){
+ pack_yuv(s, yuv_line, j);
+ memcpy(zbuf + zn, yuv_line, bytes_per_row);
+ j += s->subsampling[1] - 1;
+ }
+ else
memcpy(zbuf + j * bytes_per_row,
p->data[0] + j * p->linesize[0], bytes_per_row);
zn += bytes_per_row;
@@ -305,8 +359,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;
}
@@ -349,6 +410,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))
@@ -362,6 +429,7 @@
fail:
av_free(strip_sizes);
av_free(strip_offsets);
+ av_free(yuv_line);
return ret;
}
@@ -379,6 +447,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}
};
-------------- next part --------------
Index: libavcodec/tiffenc.c
===================================================================
--- libavcodec/tiffenc.c (wersja 8629)
+++ libavcodec/tiffenc.c (kopia robocza)
@@ -49,7 +49,7 @@
unsigned int bpp; ///< bits per pixel
int compr; ///< compression level
int bpp_tab_size; ///< bpp_tab size
- int invert; ///< photometric interpretation
+ int photometric_interpretation; ///< photometric interpretation
int strips; ///< number of strips
int rps; ///< row per strip
uint8_t entries[TIFF_MAX_ENTRY*12]; ///< entires in header
@@ -214,23 +214,23 @@
switch (avctx->pix_fmt) {
case PIX_FMT_RGB24:
s->bpp = 24;
- s->invert = 2;
+ s->photometric_interpretation = 2;
break;
case PIX_FMT_GRAY8:
s->bpp = 8;
- s->invert = 1;
+ s->photometric_interpretation = 1;
break;
case PIX_FMT_PAL8:
s->bpp = 8;
- s->invert = 3;
+ s->photometric_interpretation = 3;
break;
case PIX_FMT_MONOBLACK:
s->bpp = 1;
- s->invert = 1;
+ s->photometric_interpretation = 1;
break;
case PIX_FMT_MONOWHITE:
s->bpp = 1;
- s->invert = 0;
+ s->photometric_interpretation = 0;
break;
default:
av_log(s->avctx, AV_LOG_ERROR,
@@ -312,7 +312,7 @@
add_entry(s, TIFF_BPP, TIFF_SHORT, s->bpp_tab_size, bpp_tab);
add_entry1(s,TIFF_COMPR, TIFF_SHORT, s->compr);
- add_entry1(s,TIFF_INVERT, TIFF_SHORT, s->invert);
+ add_entry1(s,TIFF_INVERT, TIFF_SHORT, s->photometric_interpretation);
add_entry(s, TIFF_STRIP_OFFS, TIFF_LONG, strips, strip_offsets);
if (s->bpp_tab_size)
More information about the ffmpeg-devel
mailing list