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