annotate targaenc.c @ 4677:47237f2638b2 libavcodec

Move the encoding of the image data to its own function. patch by Bobby Bingham, uhmmmm gmail com
author diego
date Sat, 17 Mar 2007 13:16:18 +0000
parents 046456cf7d31
children ae5abc4dc946
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
1 /*
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
2 * Targa (.tga) image encoder
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
3 * Copyright (c) 2007 Bobby Bingham
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
4 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
5 * This file is part of FFmpeg.
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
6 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
11 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
15 * Lesser General Public License for more details.
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
16 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
20 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
21 */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
22 #include "avcodec.h"
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
23
4677
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
24 static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
25 {
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
26 int i, n = bpp * w;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
27 uint8_t *out = outbuf;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
28 uint8_t *ptr = pic->data[0];
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
29
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
30 for(i=0; i < h; i++) {
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
31 memcpy(out, ptr, n);
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
32 out += n;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
33 ptr += pic->linesize[0];
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
34 }
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
35
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
36 return out - outbuf;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
37 }
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
38
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
39 static int targa_encode_frame(AVCodecContext *avctx,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
40 unsigned char *outbuf,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
41 int buf_size, void *data){
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
42 AVFrame *p = data;
4677
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
43 int bpp;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
44 uint8_t *out;
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
45
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
46 if(avctx->width > 0xffff || avctx->height > 0xffff) {
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
47 av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
48 return -1;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
49 }
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
50 if(buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 45) {
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
51 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
52 return -1;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
53 }
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
54
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
55 p->pict_type= FF_I_TYPE;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
56 p->key_frame= 1;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
57
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
58 /* zero out the header and only set applicable fields */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
59 memset(outbuf, 0, 11);
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
60 AV_WL16(outbuf+12, avctx->width);
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
61 AV_WL16(outbuf+14, avctx->height);
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
62 outbuf[17] = 0x20; /* origin is top-left. no alpha */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
63
4676
046456cf7d31 Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4673
diff changeset
64 /* TODO: support alpha channel and RLE */
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
65 switch(avctx->pix_fmt) {
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
66 case PIX_FMT_GRAY8:
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
67 outbuf[2] = 3; /* uncompressed grayscale image */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
68 outbuf[16] = 8; /* bpp */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
69 break;
4676
046456cf7d31 Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4673
diff changeset
70 case PIX_FMT_RGB555:
046456cf7d31 Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4673
diff changeset
71 outbuf[2] = 2; /* uncompresses true-color image */
046456cf7d31 Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4673
diff changeset
72 outbuf[16] = 16; /* bpp */
046456cf7d31 Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4673
diff changeset
73 break;
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
74 case PIX_FMT_BGR24:
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
75 outbuf[2] = 2; /* uncompressed true-color image */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
76 outbuf[16] = 24; /* bpp */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
77 break;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
78 default:
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
79 return -1;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
80 }
4677
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
81 bpp = outbuf[16] >> 3;
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
82
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
83 out = outbuf + 18; /* skip past the header we just output */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
84
4677
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
85 out += targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
86
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
87 /* The standard recommends including this section, even if we don't use
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
88 * any of the features it affords. TODO: take advantage of the pixel
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
89 * aspect ratio and encoder ID fields available? */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
90 memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
91
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
92 return out + 26 - outbuf;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
93 }
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
94
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
95 static int targa_encode_init(AVCodecContext *avctx)
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
96 {
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
97 return 0;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
98 }
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
99
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
100 AVCodec targa_encoder = {
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
101 .name = "targa",
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
102 .type = CODEC_TYPE_VIDEO,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
103 .id = CODEC_ID_TARGA,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
104 .priv_data_size = 0,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
105 .init = targa_encode_init,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
106 .encode = targa_encode_frame,
4676
046456cf7d31 Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4673
diff changeset
107 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555, PIX_FMT_GRAY8, -1},
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
108 };