00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "imgconvert.h"
00029 #include "raw.h"
00030 #include "libavutil/intreadwrite.h"
00031 #include "libavutil/imgutils.h"
00032 #include "libavutil/opt.h"
00033
00034 typedef struct RawVideoContext {
00035 AVClass *av_class;
00036 uint32_t palette[AVPALETTE_COUNT];
00037 unsigned char * buffer;
00038 int length;
00039 int flip;
00040 AVFrame pic;
00041 int tff;
00042 } RawVideoContext;
00043
00044 static const AVOption options[]={
00045 {"top", "top field first", offsetof(RawVideoContext, tff), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM},
00046 {NULL}
00047 };
00048 static const AVClass class = { "rawdec", NULL, options, LIBAVUTIL_VERSION_INT };
00049
00050 static const PixelFormatTag pix_fmt_bps_avi[] = {
00051 { PIX_FMT_MONOWHITE, 1 },
00052 { PIX_FMT_PAL8, 2 },
00053 { PIX_FMT_PAL8, 4 },
00054 { PIX_FMT_PAL8, 8 },
00055 { PIX_FMT_RGB444, 12 },
00056 { PIX_FMT_RGB555, 15 },
00057 { PIX_FMT_RGB555, 16 },
00058 { PIX_FMT_BGR24, 24 },
00059 { PIX_FMT_BGRA, 32 },
00060 { PIX_FMT_NONE, 0 },
00061 };
00062
00063 static const PixelFormatTag pix_fmt_bps_mov[] = {
00064 { PIX_FMT_MONOWHITE, 1 },
00065 { PIX_FMT_PAL8, 2 },
00066 { PIX_FMT_PAL8, 4 },
00067 { PIX_FMT_PAL8, 8 },
00068
00069
00070 { PIX_FMT_RGB555BE, 16 },
00071 { PIX_FMT_RGB24, 24 },
00072 { PIX_FMT_ARGB, 32 },
00073 { PIX_FMT_MONOWHITE,33 },
00074 { PIX_FMT_NONE, 0 },
00075 };
00076
00077 enum PixelFormat ff_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
00078 {
00079 while (tags->pix_fmt >= 0) {
00080 if (tags->fourcc == fourcc)
00081 return tags->pix_fmt;
00082 tags++;
00083 }
00084 return PIX_FMT_YUV420P;
00085 }
00086
00087 static av_cold int raw_init_decoder(AVCodecContext *avctx)
00088 {
00089 RawVideoContext *context = avctx->priv_data;
00090
00091 if (avctx->codec_tag == MKTAG('r','a','w',' '))
00092 avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_mov, avctx->bits_per_coded_sample);
00093 else if (avctx->codec_tag == MKTAG('W','R','A','W'))
00094 avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
00095 else if (avctx->codec_tag)
00096 avctx->pix_fmt = ff_find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
00097 else if (avctx->pix_fmt == PIX_FMT_NONE && avctx->bits_per_coded_sample)
00098 avctx->pix_fmt = ff_find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
00099
00100 if (avctx->pix_fmt == PIX_FMT_NONE) {
00101 av_log(avctx, AV_LOG_ERROR, "Pixel format was not specified and cannot be detected\n");
00102 return AVERROR(EINVAL);
00103 }
00104
00105 ff_set_systematic_pal2(context->palette, avctx->pix_fmt);
00106 if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
00107 avctx->pix_fmt==PIX_FMT_PAL8 &&
00108 (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
00109 context->length = avpicture_get_size(avctx->pix_fmt, FFALIGN(avctx->width, 16), avctx->height);
00110 context->buffer = av_malloc(context->length);
00111 if (!context->buffer)
00112 return -1;
00113 } else {
00114 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
00115 }
00116 context->pic.pict_type = AV_PICTURE_TYPE_I;
00117 context->pic.key_frame = 1;
00118
00119 avctx->coded_frame= &context->pic;
00120
00121 if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
00122 avctx->codec_tag == MKTAG('c','y','u','v') ||
00123 avctx->codec_tag == MKTAG(3, 0, 0, 0) || avctx->codec_tag == MKTAG('W','R','A','W'))
00124 context->flip=1;
00125
00126 return 0;
00127 }
00128
00129 static void flip(AVCodecContext *avctx, AVPicture * picture){
00130 picture->data[0] += picture->linesize[0] * (avctx->height-1);
00131 picture->linesize[0] *= -1;
00132 }
00133
00134 static int raw_decode(AVCodecContext *avctx,
00135 void *data, int *data_size,
00136 AVPacket *avpkt)
00137 {
00138 const uint8_t *buf = avpkt->data;
00139 int buf_size = avpkt->size;
00140 int linesize_align = 4;
00141 RawVideoContext *context = avctx->priv_data;
00142 int res;
00143
00144 AVFrame * frame = (AVFrame *) data;
00145 AVPicture * picture = (AVPicture *) data;
00146
00147 frame->pict_type = avctx->coded_frame->pict_type;
00148 frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
00149 frame->top_field_first = avctx->coded_frame->top_field_first;
00150 frame->reordered_opaque = avctx->reordered_opaque;
00151 frame->pkt_pts = avctx->pkt->pts;
00152 frame->pkt_pos = avctx->pkt->pos;
00153
00154 if(context->tff>=0){
00155 frame->interlaced_frame = 1;
00156 frame->top_field_first = context->tff;
00157 }
00158
00159 if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0))
00160 return -1;
00161
00162
00163 if (context->buffer) {
00164 int i;
00165 uint8_t *dst = context->buffer;
00166 buf_size = context->length - 256*4;
00167 if (avctx->bits_per_coded_sample == 4){
00168 for(i=0; 2*i+1 < buf_size; i++){
00169 dst[2*i+0]= buf[i]>>4;
00170 dst[2*i+1]= buf[i]&15;
00171 }
00172 linesize_align = 8;
00173 } else {
00174 for(i=0; 4*i+3 < buf_size; i++){
00175 dst[4*i+0]= buf[i]>>6;
00176 dst[4*i+1]= buf[i]>>4&3;
00177 dst[4*i+2]= buf[i]>>2&3;
00178 dst[4*i+3]= buf[i] &3;
00179 }
00180 linesize_align = 16;
00181 }
00182 buf= dst;
00183 }
00184
00185 if(avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
00186 avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
00187 buf += buf_size - context->length;
00188
00189 if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
00190 avctx->width, avctx->height)) < 0)
00191 return res;
00192 if((avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length) ||
00193 (avctx->pix_fmt!=PIX_FMT_PAL8 &&
00194 (av_pix_fmt_descriptors[avctx->pix_fmt].flags & PIX_FMT_PAL))){
00195 frame->data[1]= context->palette;
00196 }
00197 if (avctx->pix_fmt == PIX_FMT_PAL8) {
00198 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00199
00200 if (pal) {
00201 memcpy(frame->data[1], pal, AVPALETTE_SIZE);
00202 frame->palette_has_changed = 1;
00203 }
00204 }
00205 if((avctx->pix_fmt==PIX_FMT_BGR24 ||
00206 avctx->pix_fmt==PIX_FMT_GRAY8 ||
00207 avctx->pix_fmt==PIX_FMT_RGB555LE ||
00208 avctx->pix_fmt==PIX_FMT_RGB555BE ||
00209 avctx->pix_fmt==PIX_FMT_RGB565LE ||
00210 avctx->pix_fmt==PIX_FMT_MONOWHITE ||
00211 avctx->pix_fmt==PIX_FMT_PAL8) &&
00212 FFALIGN(frame->linesize[0], linesize_align)*avctx->height <= buf_size)
00213 frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
00214
00215 if(context->flip)
00216 flip(avctx, picture);
00217
00218 if ( avctx->codec_tag == MKTAG('Y', 'V', '1', '2')
00219 || avctx->codec_tag == MKTAG('Y', 'V', '1', '6')
00220 || avctx->codec_tag == MKTAG('Y', 'V', '2', '4')
00221 || avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
00222 FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
00223
00224 if(avctx->codec_tag == AV_RL32("yuv2") &&
00225 avctx->pix_fmt == PIX_FMT_YUYV422) {
00226 int x, y;
00227 uint8_t *line = picture->data[0];
00228 for(y = 0; y < avctx->height; y++) {
00229 for(x = 0; x < avctx->width; x++)
00230 line[2*x + 1] ^= 0x80;
00231 line += picture->linesize[0];
00232 }
00233 }
00234
00235 *data_size = sizeof(AVPicture);
00236 return buf_size;
00237 }
00238
00239 static av_cold int raw_close_decoder(AVCodecContext *avctx)
00240 {
00241 RawVideoContext *context = avctx->priv_data;
00242
00243 av_freep(&context->buffer);
00244 return 0;
00245 }
00246
00247 AVCodec ff_rawvideo_decoder = {
00248 .name = "rawvideo",
00249 .type = AVMEDIA_TYPE_VIDEO,
00250 .id = CODEC_ID_RAWVIDEO,
00251 .priv_data_size = sizeof(RawVideoContext),
00252 .init = raw_init_decoder,
00253 .close = raw_close_decoder,
00254 .decode = raw_decode,
00255 .long_name = NULL_IF_CONFIG_SMALL("raw video"),
00256 .priv_class= &class,
00257 };