00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/common.h"
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/imgutils.h"
00025 #include "avcodec.h"
00026
00027 typedef struct PTXContext {
00028 AVFrame picture;
00029 } PTXContext;
00030
00031 static av_cold int ptx_init(AVCodecContext *avctx) {
00032 PTXContext *s = avctx->priv_data;
00033
00034 avcodec_get_frame_defaults(&s->picture);
00035 avctx->coded_frame= &s->picture;
00036
00037 return 0;
00038 }
00039
00040 static int ptx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00041 AVPacket *avpkt) {
00042 const uint8_t *buf = avpkt->data;
00043 const uint8_t *buf_end = avpkt->data + avpkt->size;
00044 PTXContext * const s = avctx->priv_data;
00045 AVFrame *picture = data;
00046 AVFrame * const p = &s->picture;
00047 unsigned int offset, w, h, y, stride, bytes_per_pixel;
00048 uint8_t *ptr;
00049
00050 if (buf_end - buf < 14)
00051 return AVERROR_INVALIDDATA;
00052 offset = AV_RL16(buf);
00053 w = AV_RL16(buf+8);
00054 h = AV_RL16(buf+10);
00055 bytes_per_pixel = AV_RL16(buf+12) >> 3;
00056
00057 if (bytes_per_pixel != 2) {
00058 av_log_ask_for_sample(avctx, "Image format is not RGB15.\n");
00059 return -1;
00060 }
00061
00062 avctx->pix_fmt = PIX_FMT_BGR555LE;
00063
00064 if (buf_end - buf < offset)
00065 return AVERROR_INVALIDDATA;
00066 if (offset != 0x2c)
00067 av_log_ask_for_sample(avctx, "offset != 0x2c\n");
00068
00069 buf += offset;
00070
00071 if (p->data[0])
00072 avctx->release_buffer(avctx, p);
00073
00074 if (av_image_check_size(w, h, 0, avctx))
00075 return -1;
00076 if (w != avctx->width || h != avctx->height)
00077 avcodec_set_dimensions(avctx, w, h);
00078 if (avctx->get_buffer(avctx, p) < 0) {
00079 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00080 return -1;
00081 }
00082
00083 p->pict_type = AV_PICTURE_TYPE_I;
00084
00085 ptr = p->data[0];
00086 stride = p->linesize[0];
00087
00088 for (y = 0; y < h && buf_end - buf >= w * bytes_per_pixel; y++) {
00089 memcpy(ptr, buf, w*bytes_per_pixel);
00090 ptr += stride;
00091 buf += w*bytes_per_pixel;
00092 }
00093
00094 *picture = s->picture;
00095 *data_size = sizeof(AVPicture);
00096
00097 if (y < h) {
00098 av_log(avctx, AV_LOG_WARNING, "incomplete packet\n");
00099 return avpkt->size;
00100 }
00101
00102 return offset + w*h*bytes_per_pixel;
00103 }
00104
00105 static av_cold int ptx_end(AVCodecContext *avctx) {
00106 PTXContext *s = avctx->priv_data;
00107
00108 if(s->picture.data[0])
00109 avctx->release_buffer(avctx, &s->picture);
00110
00111 return 0;
00112 }
00113
00114 AVCodec ff_ptx_decoder = {
00115 .name = "ptx",
00116 .type = AVMEDIA_TYPE_VIDEO,
00117 .id = AV_CODEC_ID_PTX,
00118 .priv_data_size = sizeof(PTXContext),
00119 .init = ptx_init,
00120 .close = ptx_end,
00121 .decode = ptx_decode_frame,
00122 .capabilities = CODEC_CAP_DR1,
00123 .long_name = NULL_IF_CONFIG_SMALL("V.Flash PTX image"),
00124 };