Mercurial > libavcodec.hg
comparison lclenc.c @ 9746:e8421cfcc381 libavcodec
lclenc.c: compress directly into output buffer instead of using a pointless
temporary buffer and then using put_bits to copy the data over.
author | reimar |
---|---|
date | Sun, 31 May 2009 08:49:27 +0000 |
parents | 2b2cf4039589 |
children | 9db25052df0e |
comparison
equal
deleted
inserted
replaced
9745:2b2cf4039589 | 9746:e8421cfcc381 |
---|---|
81 AVFrame *pict = data; | 81 AVFrame *pict = data; |
82 AVFrame * const p = &c->pic; | 82 AVFrame * const p = &c->pic; |
83 int i; | 83 int i; |
84 int zret; // Zlib return code | 84 int zret; // Zlib return code |
85 | 85 |
86 init_put_bits(&c->pb, buf, buf_size); | |
87 | |
88 *p = *pict; | 86 *p = *pict; |
89 p->pict_type= FF_I_TYPE; | 87 p->pict_type= FF_I_TYPE; |
90 p->key_frame= 1; | 88 p->key_frame= 1; |
91 | 89 |
92 if(avctx->pix_fmt != PIX_FMT_BGR24){ | 90 if(avctx->pix_fmt != PIX_FMT_BGR24){ |
97 zret = deflateReset(&c->zstream); | 95 zret = deflateReset(&c->zstream); |
98 if (zret != Z_OK) { | 96 if (zret != Z_OK) { |
99 av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret); | 97 av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret); |
100 return -1; | 98 return -1; |
101 } | 99 } |
102 c->zstream.next_out = c->comp_buf; | 100 c->zstream.next_out = buf; |
103 c->zstream.avail_out = c->max_comp_size; | 101 c->zstream.avail_out = buf_size; |
104 | 102 |
105 for(i = avctx->height - 1; i >= 0; i--) { | 103 for(i = avctx->height - 1; i >= 0; i--) { |
106 c->zstream.next_in = p->data[0]+p->linesize[0]*i; | 104 c->zstream.next_in = p->data[0]+p->linesize[0]*i; |
107 c->zstream.avail_in = avctx->width*3; | 105 c->zstream.avail_in = avctx->width*3; |
108 zret = deflate(&c->zstream, Z_NO_FLUSH); | 106 zret = deflate(&c->zstream, Z_NO_FLUSH); |
114 zret = deflate(&c->zstream, Z_FINISH); | 112 zret = deflate(&c->zstream, Z_FINISH); |
115 if (zret != Z_STREAM_END) { | 113 if (zret != Z_STREAM_END) { |
116 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret); | 114 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret); |
117 return -1; | 115 return -1; |
118 } | 116 } |
119 | |
120 for (i = 0; i < c->zstream.total_out; i++) | |
121 put_bits(&c->pb, 8, c->comp_buf[i]); | |
122 flush_put_bits(&c->pb); | |
123 | 117 |
124 return c->zstream.total_out; | 118 return c->zstream.total_out; |
125 } | 119 } |
126 | 120 |
127 /* | 121 /* |
170 c->zstream.zfree = Z_NULL; | 164 c->zstream.zfree = Z_NULL; |
171 c->zstream.opaque = Z_NULL; | 165 c->zstream.opaque = Z_NULL; |
172 zret = deflateInit(&c->zstream, c->compression); | 166 zret = deflateInit(&c->zstream, c->compression); |
173 if (zret != Z_OK) { | 167 if (zret != Z_OK) { |
174 av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret); | 168 av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret); |
175 return 1; | |
176 } | |
177 | |
178 /* Conservative upper bound taken from zlib v1.2.1 source */ | |
179 c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) + | |
180 ((c->decomp_size + 63) >> 6) + 11; | |
181 if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) { | |
182 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n"); | |
183 return 1; | 169 return 1; |
184 } | 170 } |
185 | 171 |
186 return 0; | 172 return 0; |
187 } | 173 } |