Mercurial > libavcodec.hg
comparison rawdec.c @ 12507:46191b10d663 libavcodec
rawdec: only allocate a full-frame size buffer if it actually will
be used, place palette buffer in the context to simplify this.
author | reimar |
---|---|
date | Thu, 23 Sep 2010 20:23:15 +0000 |
parents | f6a2af58f3e0 |
children | e6d711ba5760 |
comparison
equal
deleted
inserted
replaced
12506:747e5f278c4b | 12507:46191b10d663 |
---|---|
27 #include "avcodec.h" | 27 #include "avcodec.h" |
28 #include "raw.h" | 28 #include "raw.h" |
29 #include "libavutil/intreadwrite.h" | 29 #include "libavutil/intreadwrite.h" |
30 | 30 |
31 typedef struct RawVideoContext { | 31 typedef struct RawVideoContext { |
32 uint32_t palette[AVPALETTE_COUNT]; | |
32 unsigned char * buffer; /* block of memory for holding one frame */ | 33 unsigned char * buffer; /* block of memory for holding one frame */ |
33 int length; /* number of bytes in buffer */ | 34 int length; /* number of bytes in buffer */ |
34 int flip; | 35 int flip; |
35 AVFrame pic; ///< AVCodecContext.coded_frame | 36 AVFrame pic; ///< AVCodecContext.coded_frame |
36 } RawVideoContext; | 37 } RawVideoContext; |
79 avctx->pix_fmt = find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag); | 80 avctx->pix_fmt = find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag); |
80 else if (avctx->pix_fmt == PIX_FMT_NONE && avctx->bits_per_coded_sample) | 81 else if (avctx->pix_fmt == PIX_FMT_NONE && avctx->bits_per_coded_sample) |
81 avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample); | 82 avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample); |
82 | 83 |
83 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height); | 84 context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height); |
84 context->buffer = av_malloc(context->length); | 85 if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) && |
86 avctx->pix_fmt==PIX_FMT_PAL8 && | |
87 (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){ | |
88 context->buffer = av_malloc(context->length); | |
89 if (!context->buffer) | |
90 return -1; | |
91 } | |
85 context->pic.pict_type = FF_I_TYPE; | 92 context->pic.pict_type = FF_I_TYPE; |
86 context->pic.key_frame = 1; | 93 context->pic.key_frame = 1; |
87 | 94 |
88 avctx->coded_frame= &context->pic; | 95 avctx->coded_frame= &context->pic; |
89 | |
90 if (!context->buffer) | |
91 return -1; | |
92 | 96 |
93 if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) || | 97 if((avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) || |
94 avctx->codec_tag == MKTAG( 3 , 0 , 0 , 0 )) | 98 avctx->codec_tag == MKTAG( 3 , 0 , 0 , 0 )) |
95 context->flip=1; | 99 context->flip=1; |
96 | 100 |
115 | 119 |
116 frame->interlaced_frame = avctx->coded_frame->interlaced_frame; | 120 frame->interlaced_frame = avctx->coded_frame->interlaced_frame; |
117 frame->top_field_first = avctx->coded_frame->top_field_first; | 121 frame->top_field_first = avctx->coded_frame->top_field_first; |
118 | 122 |
119 //2bpp and 4bpp raw in avi and mov (yes this is ugly ...) | 123 //2bpp and 4bpp raw in avi and mov (yes this is ugly ...) |
120 if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) && | 124 if (context->buffer) { |
121 avctx->pix_fmt==PIX_FMT_PAL8 && | |
122 (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){ | |
123 int i; | 125 int i; |
124 uint8_t *dst = context->buffer + 256*4; | 126 uint8_t *dst = context->buffer; |
125 buf_size = context->length - 256*4; | 127 buf_size = context->length - 256*4; |
126 if (avctx->bits_per_coded_sample == 4){ | 128 if (avctx->bits_per_coded_sample == 4){ |
127 for(i=0; 2*i+1 < buf_size; i++){ | 129 for(i=0; 2*i+1 < buf_size; i++){ |
128 dst[2*i+0]= buf[i]>>4; | 130 dst[2*i+0]= buf[i]>>4; |
129 dst[2*i+1]= buf[i]&15; | 131 dst[2*i+1]= buf[i]&15; |
145 if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0)) | 147 if(buf_size < context->length - (avctx->pix_fmt==PIX_FMT_PAL8 ? 256*4 : 0)) |
146 return -1; | 148 return -1; |
147 | 149 |
148 avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height); | 150 avpicture_fill(picture, buf, avctx->pix_fmt, avctx->width, avctx->height); |
149 if(avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length){ | 151 if(avctx->pix_fmt==PIX_FMT_PAL8 && buf_size < context->length){ |
150 frame->data[1]= context->buffer; | 152 frame->data[1]= context->palette; |
151 } | 153 } |
152 if (avctx->palctrl && avctx->palctrl->palette_changed) { | 154 if (avctx->palctrl && avctx->palctrl->palette_changed) { |
153 memcpy(frame->data[1], avctx->palctrl->palette, AVPALETTE_SIZE); | 155 memcpy(frame->data[1], avctx->palctrl->palette, AVPALETTE_SIZE); |
154 avctx->palctrl->palette_changed = 0; | 156 avctx->palctrl->palette_changed = 0; |
155 } | 157 } |