| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Targa (.tga) image encoder | 2 * Targa (.tga) image encoder |
| 3 * Copyright (c) 2007 Bobby Bingham | 3 * Copyright (c) 2007 Bobby Bingham |
| 4 * | 4 * |
| 5 * This file is part of FFmpeg. | 5 * This file is part of FFmpeg. |
| 6 * | 6 * |
| 7 * FFmpeg is free software; you can redistribute it and/or | 7 * FFmpeg is free software; you can redistribute it and/or |
| 8 * modify it under the terms of the GNU Lesser General Public | 8 * modify it under the terms of the GNU Lesser General Public |
| 9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
| 10 * version 2.1 of the License, or (at your option) any later version. | 10 * version 2.1 of the License, or (at your option) any later version. |
| 11 * | 11 * |
| 12 * FFmpeg is distributed in the hope that it will be useful, | 12 * FFmpeg is distributed in the hope that it will be useful, |
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 * Lesser General Public License for more details. | 15 * Lesser General Public License for more details. |
| 16 * | 16 * |
| 17 * You should have received a copy of the GNU Lesser General Public | 17 * You should have received a copy of the GNU Lesser General Public |
| 18 * License along with FFmpeg; if not, write to the Free Software | 18 * License along with FFmpeg; if not, write to the Free Software |
| 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 */ | 20 */ |
| 21 | 21 |
| 22 #include "libavutil/intreadwrite.h" | 22 #include "libavutil/intreadwrite.h" |
| 23 #include "avcodec.h" | 23 #include "avcodec.h" |
| 24 #include "rle.h" | 24 #include "rle.h" |
| 25 #include "targa.h" |
| 25 | 26 |
| 26 typedef struct TargaContext { | 27 typedef struct TargaContext { |
| 27 AVFrame picture; | 28 AVFrame picture; |
| 28 } TargaContext; | 29 } TargaContext; |
| 29 | 30 |
| 30 /** | 31 /** |
| 31 * RLE compress the image, with maximum size of out_size | 32 * RLE compress the image, with maximum size of out_size |
| 32 * @param outbuf Output buffer | 33 * @param outbuf Output buffer |
| 33 * @param out_size Maximum output size | 34 * @param out_size Maximum output size |
| 34 * @param pic Image to compress | 35 * @param pic Image to compress |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 75 |
| 75 static int targa_encode_frame(AVCodecContext *avctx, | 76 static int targa_encode_frame(AVCodecContext *avctx, |
| 76 unsigned char *outbuf, | 77 unsigned char *outbuf, |
| 77 int buf_size, void *data){ | 78 int buf_size, void *data){ |
| 78 AVFrame *p = data; | 79 AVFrame *p = data; |
| 79 int bpp, picsize, datasize = -1; | 80 int bpp, picsize, datasize = -1; |
| 80 uint8_t *out; | 81 uint8_t *out; |
| 81 | 82 |
| 82 if(avctx->width > 0xffff || avctx->height > 0xffff) { | 83 if(avctx->width > 0xffff || avctx->height > 0xffff) { |
| 83 av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n"); | 84 av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n"); |
| 84 return -1; | 85 return AVERROR(EINVAL); |
| 85 } | 86 } |
| 86 picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height); | 87 picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height); |
| 87 if(buf_size < picsize + 45) { | 88 if(buf_size < picsize + 45) { |
| 88 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n"); | 89 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n"); |
| 89 return -1; | 90 return AVERROR(EINVAL); |
| 90 } | 91 } |
| 91 | 92 |
| 92 p->pict_type= FF_I_TYPE; | 93 p->pict_type= FF_I_TYPE; |
| 93 p->key_frame= 1; | 94 p->key_frame= 1; |
| 94 | 95 |
| 95 /* zero out the header and only set applicable fields */ | 96 /* zero out the header and only set applicable fields */ |
| 96 memset(outbuf, 0, 12); | 97 memset(outbuf, 0, 12); |
| 97 AV_WL16(outbuf+12, avctx->width); | 98 AV_WL16(outbuf+12, avctx->width); |
| 98 AV_WL16(outbuf+14, avctx->height); | 99 AV_WL16(outbuf+14, avctx->height); |
| 99 outbuf[17] = 0x20; /* origin is top-left. no alpha */ | 100 /* image descriptor byte: origin is always top-left, bits 0-3 specify alpha
*/ |
| 101 outbuf[17] = 0x20 | (avctx->pix_fmt == PIX_FMT_BGRA ? 8 : 0); |
| 100 | 102 |
| 101 /* TODO: support alpha channel */ | |
| 102 switch(avctx->pix_fmt) { | 103 switch(avctx->pix_fmt) { |
| 103 case PIX_FMT_GRAY8: | 104 case PIX_FMT_GRAY8: |
| 104 outbuf[2] = 3; /* uncompressed grayscale image */ | 105 outbuf[2] = TGA_BW; /* uncompressed grayscale image */ |
| 105 outbuf[16] = 8; /* bpp */ | 106 outbuf[16] = 8; /* bpp */ |
| 106 break; | 107 break; |
| 107 case PIX_FMT_RGB555LE: | 108 case PIX_FMT_RGB555LE: |
| 108 outbuf[2] = 2; /* uncompresses true-color image */ | 109 outbuf[2] = TGA_RGB; /* uncompresses true-color image */ |
| 109 outbuf[16] = 16; /* bpp */ | 110 outbuf[16] = 16; /* bpp */ |
| 110 break; | 111 break; |
| 111 case PIX_FMT_BGR24: | 112 case PIX_FMT_BGR24: |
| 112 outbuf[2] = 2; /* uncompressed true-color image */ | 113 outbuf[2] = TGA_RGB; /* uncompressed true-color image */ |
| 113 outbuf[16] = 24; /* bpp */ | 114 outbuf[16] = 24; /* bpp */ |
| 114 break; | 115 break; |
| 116 case PIX_FMT_BGRA: |
| 117 outbuf[2] = TGA_RGB; /* uncompressed true-color image */ |
| 118 outbuf[16] = 32; /* bpp */ |
| 119 break; |
| 115 default: | 120 default: |
| 116 return -1; | 121 av_log(avctx, AV_LOG_ERROR, "Pixel format '%s' not supported.\n", |
| 122 avcodec_get_pix_fmt_name(avctx->pix_fmt)); |
| 123 return AVERROR(EINVAL); |
| 117 } | 124 } |
| 118 bpp = outbuf[16] >> 3; | 125 bpp = outbuf[16] >> 3; |
| 119 | 126 |
| 120 out = outbuf + 18; /* skip past the header we just output */ | 127 out = outbuf + 18; /* skip past the header we just output */ |
| 121 | 128 |
| 122 /* try RLE compression */ | 129 /* try RLE compression */ |
| 123 if (avctx->coder_type != FF_CODER_TYPE_RAW) | 130 if (avctx->coder_type != FF_CODER_TYPE_RAW) |
| 124 datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->h
eight); | 131 datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->h
eight); |
| 125 | 132 |
| 126 /* if that worked well, mark the picture as RLE compressed */ | 133 /* if that worked well, mark the picture as RLE compressed */ |
| (...skipping 24 matching lines...) Expand all Loading... |
| 151 return 0; | 158 return 0; |
| 152 } | 159 } |
| 153 | 160 |
| 154 AVCodec targa_encoder = { | 161 AVCodec targa_encoder = { |
| 155 .name = "targa", | 162 .name = "targa", |
| 156 .type = AVMEDIA_TYPE_VIDEO, | 163 .type = AVMEDIA_TYPE_VIDEO, |
| 157 .id = CODEC_ID_TARGA, | 164 .id = CODEC_ID_TARGA, |
| 158 .priv_data_size = sizeof(TargaContext), | 165 .priv_data_size = sizeof(TargaContext), |
| 159 .init = targa_encode_init, | 166 .init = targa_encode_init, |
| 160 .encode = targa_encode_frame, | 167 .encode = targa_encode_frame, |
| 161 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555LE, PIX_F
MT_GRAY8, PIX_FMT_NONE}, | 168 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_R
GB555LE, PIX_FMT_GRAY8, PIX_FMT_NONE}, |
| 162 .long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"), | 169 .long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"), |
| 163 }; | 170 }; |
| OLD | NEW |