MCPcopy Create free account
hub / github.com/FFmpeg/FFmpeg / targa_encode_frame

Function targa_encode_frame

libavcodec/targaenc.c:87–181  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

85}
86
87static int targa_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
88 const AVFrame *p, int *got_packet)
89{
90 TargaContext *s = avctx->priv_data;
91 int bpp, picsize, datasize = -1, ret, i;
92 uint8_t *out;
93 int maxpal = 32*32;
94
95 picsize = av_image_get_buffer_size(avctx->pix_fmt,
96 avctx->width, avctx->height, 1);
97 if ((ret = ff_alloc_packet(avctx, pkt, picsize + 45 + maxpal)) < 0)
98 return ret;
99
100 /* zero out the header and only set applicable fields */
101 memset(pkt->data, 0, 12);
102 AV_WL16(pkt->data+12, avctx->width);
103 AV_WL16(pkt->data+14, avctx->height);
104 /* image descriptor byte: origin is always top-left, bits 0-3 specify alpha */
105 pkt->data[17] = 0x20 | (avctx->pix_fmt == AV_PIX_FMT_BGRA ? 8 : 0);
106
107 out = pkt->data + 18; /* skip past the header we write */
108
109 avctx->bits_per_coded_sample = av_get_bits_per_pixel(av_pix_fmt_desc_get(avctx->pix_fmt));
110 switch(avctx->pix_fmt) {
111 case AV_PIX_FMT_PAL8: {
112 int pal_bpp = 24; /* Only write 32bit palette if there is transparency information */
113 for (i = 0; i < 256; i++)
114 if (AV_RN32(p->data[1] + 4 * i) >> 24 != 0xFF) {
115 pal_bpp = 32;
116 break;
117 }
118 pkt->data[1] = 1; /* palette present */
119 pkt->data[2] = TGA_PAL; /* uncompressed palettised image */
120 pkt->data[6] = 1; /* palette contains 256 entries */
121 pkt->data[7] = pal_bpp; /* palette contains pal_bpp bit entries */
122 pkt->data[16] = 8; /* bpp */
123 for (i = 0; i < 256; i++)
124 if (pal_bpp == 32) {
125 AV_WL32(pkt->data + 18 + 4 * i, *(uint32_t *)(p->data[1] + i * 4));
126 } else {
127 AV_WL24(pkt->data + 18 + 3 * i, *(uint32_t *)(p->data[1] + i * 4));
128 }
129 out += 32 * pal_bpp; /* skip past the palette we just output */
130 av_assert0(32 * pal_bpp <= maxpal);
131 break;
132 }
133 case AV_PIX_FMT_GRAY8:
134 pkt->data[2] = TGA_BW; /* uncompressed grayscale image */
135 avctx->bits_per_coded_sample = 0x28;
136 pkt->data[16] = 8; /* bpp */
137 break;
138 case AV_PIX_FMT_RGB555LE:
139 pkt->data[2] = TGA_RGB; /* uncompressed true-color image */
140 avctx->bits_per_coded_sample =
141 pkt->data[16] = 16; /* bpp */
142 break;
143 case AV_PIX_FMT_BGR24:
144 pkt->data[2] = TGA_RGB; /* uncompressed true-color image */

Callers

nothing calls this directly

Calls 9

av_image_get_buffer_sizeFunction · 0.85
ff_alloc_packetFunction · 0.85
av_get_bits_per_pixelFunction · 0.85
av_pix_fmt_desc_getFunction · 0.85
AV_RN32Function · 0.85
av_logFunction · 0.85
av_get_pix_fmt_nameFunction · 0.85
targa_encode_rleFunction · 0.85
targa_encode_normalFunction · 0.85

Tested by

no test coverage detected