Mercurial > libavcodec.hg
comparison tiffenc.c @ 4792:d8b17a09a114 libavcodec
YUV support patch by (Kamil Nowosad k.nowosad students mimuw edu pl)
author | michael |
---|---|
date | Fri, 06 Apr 2007 23:16:08 +0000 |
parents | caafad01dec0 |
children | 812f759a7c59 |
comparison
equal
deleted
inserted
replaced
4791:caafad01dec0 | 4792:d8b17a09a114 |
---|---|
55 uint8_t entries[TIFF_MAX_ENTRY*12]; ///< entires in header | 55 uint8_t entries[TIFF_MAX_ENTRY*12]; ///< entires in header |
56 int num_entries; ///< number of entires | 56 int num_entries; ///< number of entires |
57 uint8_t **buf; ///< actual position in buffer | 57 uint8_t **buf; ///< actual position in buffer |
58 uint8_t *buf_start; ///< pointer to first byte in buffer | 58 uint8_t *buf_start; ///< pointer to first byte in buffer |
59 int buf_size; ///< buffer size | 59 int buf_size; ///< buffer size |
60 uint16_t subsampling[2]; ///< YUV subsampling factors | |
60 } TiffEncoderContext; | 61 } TiffEncoderContext; |
61 | 62 |
62 | 63 |
63 /** | 64 /** |
64 * Check free space in buffer | 65 * Check free space in buffer |
168 return n; | 169 return n; |
169 case TIFF_PACKBITS: | 170 case TIFF_PACKBITS: |
170 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0); | 171 return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start), src, 1, n, 2, 0xff, -1, 0); |
171 default: | 172 default: |
172 return -1; | 173 return -1; |
174 } | |
175 } | |
176 | |
177 static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum) | |
178 { | |
179 AVFrame *p = &s->picture; | |
180 int i, j, k; | |
181 int w = (s->width - 1) / s->subsampling[0] + 1; | |
182 uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]]; | |
183 uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]]; | |
184 for (i = 0; i < w; i++){ | |
185 for (j = 0; j < s->subsampling[1]; j++) | |
186 for (k = 0; k < s->subsampling[0]; k++) | |
187 *dst++ = p->data[0][(lnum + j) * p->linesize[0] + | |
188 i * s->subsampling[0] + k]; | |
189 *dst++ = *pu++; | |
190 *dst++ = *pv++; | |
173 } | 191 } |
174 } | 192 } |
175 | 193 |
176 static int encode_frame(AVCodecContext * avctx, unsigned char *buf, | 194 static int encode_frame(AVCodecContext * avctx, unsigned char *buf, |
177 int buf_size, void *data) | 195 int buf_size, void *data) |
188 uint32_t *strip_offsets = NULL; | 206 uint32_t *strip_offsets = NULL; |
189 int bytes_per_row; | 207 int bytes_per_row; |
190 uint32_t res[2] = { 72, 1 }; // image resolution (72/1) | 208 uint32_t res[2] = { 72, 1 }; // image resolution (72/1) |
191 static const uint16_t bpp_tab[] = { 8, 8, 8, 8 }; | 209 static const uint16_t bpp_tab[] = { 8, 8, 8, 8 }; |
192 int ret = -1; | 210 int ret = -1; |
211 int is_yuv = 0; | |
212 uint8_t *yuv_line = NULL; | |
213 int shift_h, shift_v; | |
193 | 214 |
194 s->buf_start = buf; | 215 s->buf_start = buf; |
195 s->buf = &ptr; | 216 s->buf = &ptr; |
196 s->buf_size = buf_size; | 217 s->buf_size = buf_size; |
197 | 218 |
208 #endif | 229 #endif |
209 } | 230 } |
210 | 231 |
211 s->width = avctx->width; | 232 s->width = avctx->width; |
212 s->height = avctx->height; | 233 s->height = avctx->height; |
234 s->subsampling[0] = 1; | |
235 s->subsampling[1] = 1; | |
213 | 236 |
214 switch (avctx->pix_fmt) { | 237 switch (avctx->pix_fmt) { |
215 case PIX_FMT_RGB24: | 238 case PIX_FMT_RGB24: |
216 s->bpp = 24; | 239 s->bpp = 24; |
217 s->photometric_interpretation = 2; | 240 s->photometric_interpretation = 2; |
230 break; | 253 break; |
231 case PIX_FMT_MONOWHITE: | 254 case PIX_FMT_MONOWHITE: |
232 s->bpp = 1; | 255 s->bpp = 1; |
233 s->photometric_interpretation = 0; | 256 s->photometric_interpretation = 0; |
234 break; | 257 break; |
258 case PIX_FMT_YUV420P: | |
259 case PIX_FMT_YUV422P: | |
260 case PIX_FMT_YUV444P: | |
261 case PIX_FMT_YUV410P: | |
262 case PIX_FMT_YUV411P: | |
263 s->photometric_interpretation = 6; | |
264 avcodec_get_chroma_sub_sample(avctx->pix_fmt, | |
265 &shift_h, &shift_v); | |
266 s->bpp = 8 + (16 >> (shift_h + shift_v)); | |
267 s->subsampling[0] = 1 << shift_h; | |
268 s->subsampling[1] = 1 << shift_v; | |
269 s->bpp_tab_size = 3; | |
270 is_yuv = 1; | |
271 break; | |
235 default: | 272 default: |
236 av_log(s->avctx, AV_LOG_ERROR, | 273 av_log(s->avctx, AV_LOG_ERROR, |
237 "This colors format is not supported\n"); | 274 "This colors format is not supported\n"); |
238 return -1; | 275 return -1; |
239 } | 276 } |
277 if (!is_yuv) | |
240 s->bpp_tab_size = (s->bpp >> 3); | 278 s->bpp_tab_size = (s->bpp >> 3); |
241 | 279 |
242 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) | 280 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) |
243 //best choose for DEFLATE | 281 //best choose for DEFLATE |
244 s->rps = s->height; | 282 s->rps = s->height; |
245 else | 283 else |
246 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip | 284 s->rps = FFMAX(8192 / (((s->width * s->bpp) >> 3) + 1), 1); // suggest size of strip |
285 s->rps = ((s->rps - 1) / s->subsampling[1] + 1) * s->subsampling[1]; // round rps up | |
247 | 286 |
248 strips = (s->height - 1) / s->rps + 1; | 287 strips = (s->height - 1) / s->rps + 1; |
249 | 288 |
250 if (check_size(s, 8)) | 289 if (check_size(s, 8)) |
251 goto fail; | 290 goto fail; |
258 bytestream_put_le32(&ptr, 0); | 297 bytestream_put_le32(&ptr, 0); |
259 | 298 |
260 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips); | 299 strip_sizes = av_mallocz(sizeof(*strip_sizes) * strips); |
261 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips); | 300 strip_offsets = av_mallocz(sizeof(*strip_offsets) * strips); |
262 | 301 |
263 bytes_per_row = (s->width * s->bpp + 7) >> 3; | 302 bytes_per_row = (((s->width - 1)/s->subsampling[0] + 1) * s->bpp |
303 * s->subsampling[0] * s->subsampling[1] + 7) >> 3; | |
304 if (is_yuv){ | |
305 yuv_line = av_malloc(bytes_per_row); | |
306 if (yuv_line == NULL){ | |
307 av_log(s->avctx, AV_LOG_ERROR, "Not enough memory\n"); | |
308 goto fail; | |
309 } | |
310 } | |
264 | 311 |
265 #ifdef CONFIG_ZLIB | 312 #ifdef CONFIG_ZLIB |
266 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) { | 313 if (s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE) { |
267 uint8_t *zbuf; | 314 uint8_t *zbuf; |
268 int zlen, zn; | 315 int zlen, zn; |
271 zlen = bytes_per_row * s->rps; | 318 zlen = bytes_per_row * s->rps; |
272 zbuf = av_malloc(zlen); | 319 zbuf = av_malloc(zlen); |
273 strip_offsets[0] = ptr - buf; | 320 strip_offsets[0] = ptr - buf; |
274 zn = 0; | 321 zn = 0; |
275 for (j = 0; j < s->rps; j++) { | 322 for (j = 0; j < s->rps; j++) { |
323 if (is_yuv){ | |
324 pack_yuv(s, yuv_line, j); | |
325 memcpy(zbuf + zn, yuv_line, bytes_per_row); | |
326 j += s->subsampling[1] - 1; | |
327 } | |
328 else | |
276 memcpy(zbuf + j * bytes_per_row, | 329 memcpy(zbuf + j * bytes_per_row, |
277 p->data[0] + j * p->linesize[0], bytes_per_row); | 330 p->data[0] + j * p->linesize[0], bytes_per_row); |
278 zn += bytes_per_row; | 331 zn += bytes_per_row; |
279 } | 332 } |
280 n = encode_strip(s, zbuf, ptr, zn, s->compr); | 333 n = encode_strip(s, zbuf, ptr, zn, s->compr); |
290 { | 343 { |
291 for (i = 0; i < s->height; i++) { | 344 for (i = 0; i < s->height; i++) { |
292 if (strip_sizes[i / s->rps] == 0) { | 345 if (strip_sizes[i / s->rps] == 0) { |
293 strip_offsets[i / s->rps] = ptr - buf; | 346 strip_offsets[i / s->rps] = ptr - buf; |
294 } | 347 } |
295 if ((n = encode_strip(s, p->data[0] + i * p->linesize[0] | 348 if (is_yuv){ |
296 , ptr, bytes_per_row, s->compr)) < 0) { | 349 pack_yuv(s, yuv_line, i); |
350 n = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr); | |
351 i += s->subsampling[1] - 1; | |
352 } | |
353 else | |
354 n = encode_strip(s, p->data[0] + i * p->linesize[0], | |
355 ptr, bytes_per_row, s->compr); | |
356 if (n < 0) { | |
297 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n"); | 357 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n"); |
298 goto fail; | 358 goto fail; |
299 } | 359 } |
300 strip_sizes[i / s->rps] += n; | 360 strip_sizes[i / s->rps] += n; |
301 ptr += n; | 361 ptr += n; |
334 pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257; | 394 pal[i + 256] = ((rgb >> 8 ) & 0xff) * 257; |
335 pal[i + 512] = ( rgb & 0xff) * 257; | 395 pal[i + 512] = ( rgb & 0xff) * 257; |
336 } | 396 } |
337 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal); | 397 add_entry(s, TIFF_PAL, TIFF_SHORT, 256 * 3, pal); |
338 } | 398 } |
399 if (is_yuv){ | |
400 /** according to CCIR Recommendation 601.1 */ | |
401 uint32_t refbw[12] = {15, 1, 235, 1, 128, 1, 240, 1, 128, 1, 240, 1}; | |
402 add_entry(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT, 2, s->subsampling); | |
403 add_entry(s, TIFF_REFERENCE_BW, TIFF_RATIONAL, 6, refbw); | |
404 } | |
339 bytestream_put_le32(&offset, ptr - buf); // write offset to dir | 405 bytestream_put_le32(&offset, ptr - buf); // write offset to dir |
340 | 406 |
341 if (check_size(s, 6 + s->num_entries * 12)) | 407 if (check_size(s, 6 + s->num_entries * 12)) |
342 goto fail; | 408 goto fail; |
343 bytestream_put_le16(&ptr, s->num_entries); // write tag count | 409 bytestream_put_le16(&ptr, s->num_entries); // write tag count |
347 ret = ptr - buf; | 413 ret = ptr - buf; |
348 | 414 |
349 fail: | 415 fail: |
350 av_free(strip_sizes); | 416 av_free(strip_sizes); |
351 av_free(strip_offsets); | 417 av_free(strip_offsets); |
418 av_free(yuv_line); | |
352 return ret; | 419 return ret; |
353 } | 420 } |
354 | 421 |
355 AVCodec tiff_encoder = { | 422 AVCodec tiff_encoder = { |
356 "tiff", | 423 "tiff", |
364 0, | 431 0, |
365 NULL, | 432 NULL, |
366 .pix_fmts = | 433 .pix_fmts = |
367 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8, | 434 (enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8, |
368 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE, | 435 PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE, |
436 PIX_FMT_YUV420P, PIX_FMT_YUV422P, | |
437 PIX_FMT_YUV444P, PIX_FMT_YUV410P, | |
438 PIX_FMT_YUV411P | |
369 -1} | 439 -1} |
370 | 440 |
371 }; | 441 }; |