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