annotate zmbvenc.c @ 4604:40cd5f7f34e7 libavcodec

WMV3 Complex Profile initial support
author kostya
date Wed, 28 Feb 2007 06:39:58 +0000
parents 6f34a614f5d9
children 255affa5bae7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
1 /*
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
2 * Zip Motion Blocks Video (ZMBV) encoder
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
3 * Copyright (c) 2006 Konstantin Shishkov
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
4 *
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
5 * This file is part of FFmpeg.
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
6 *
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
11 *
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
15 * Lesser General Public License for more details.
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
16 *
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
20 *
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
21 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
22
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
23 /**
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
24 * @file zmbvenc.c
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
25 * Zip Motion Blocks Video encoder
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
26 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
27
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
28 #include <stdio.h>
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
29 #include <stdlib.h>
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
30
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
31 #include "common.h"
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
32 #include "avcodec.h"
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
33
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
34 #include <zlib.h>
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
35
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
36 #define ZMBV_KEYFRAME 1
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
37 #define ZMBV_DELTAPAL 2
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
38
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
39 #define ZMBV_BLOCK 16
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
40
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
41 /**
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
42 * Encoder context
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
43 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
44 typedef struct ZmbvEncContext {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
45 AVCodecContext *avctx;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
46 AVFrame pic;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
47
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
48 int range;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
49 uint8_t *comp_buf, *work_buf;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
50 uint8_t pal[768];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
51 uint32_t pal2[256]; //for quick comparisons
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
52 uint8_t *prev;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
53 int pstride;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
54 int comp_size;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
55 int keyint, curfrm;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
56 z_stream zstream;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
57 } ZmbvEncContext;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
58
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
59 /** Block comparing function
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
60 * XXX should be optimized and moved to DSPContext
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
61 * TODO handle out of edge ME
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
62 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
63 static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2, int bw, int bh)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
64 {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
65 int sum = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
66 int i, j;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
67
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
68 for(j = 0; j < bh; j++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
69 for(i = 0; i < bw; i++)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
70 sum += src[i] ^ src2[i];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
71 src += stride;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
72 src2 += stride2;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
73 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
74 return sum;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
75 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
76
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
77 /** Motion estimation function
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
78 * TODO make better ME decisions
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
79 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
80 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev, int pstride,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
81 int x, int y, int *mx, int *my)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
82 {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
83 int dx, dy, tx, ty, tv, bv;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
84
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
85 *mx = *my = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
86 bv = block_cmp(src, sstride, prev, pstride, ZMBV_BLOCK, ZMBV_BLOCK);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
87 if(!bv) return 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
88 for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - ZMBV_BLOCK); ty++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
89 for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - ZMBV_BLOCK); tx++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
90 if(tx == x && ty == y) continue; // we already tested this block
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
91 dx = tx - x;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
92 dy = ty - y;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
93 tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, ZMBV_BLOCK, ZMBV_BLOCK);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
94 if(tv < bv){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
95 bv = tv;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
96 *mx = dx;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
97 *my = dy;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
98 if(!bv) return 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
99 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
100 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
101 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
102 return bv;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
103 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
104
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
105 static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
106 {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
107 ZmbvEncContext * const c = (ZmbvEncContext *)avctx->priv_data;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
108 AVFrame *pict = data;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
109 AVFrame * const p = &c->pic;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
110 uint8_t *src, *prev;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
111 uint32_t *palptr;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
112 int zret = Z_OK;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
113 int len = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
114 int keyframe, chpal;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
115 int fl;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
116 int work_size = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
117 int bw, bh;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
118 int i, j;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
119
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
120 keyframe = !c->curfrm;
4287
9900add82642 Deobfuscate expression
kostya
parents: 4285
diff changeset
121 c->curfrm++;
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
122 if(c->curfrm == c->keyint)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
123 c->curfrm = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
124 *p = *pict;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
125 p->pict_type= keyframe ? FF_I_TYPE : FF_P_TYPE;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
126 p->key_frame= keyframe;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
127 chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
128
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
129 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
130 *buf++ = fl; len++;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
131 if(keyframe){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
132 deflateReset(&c->zstream);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
133 *buf++ = 0; len++; // hi ver
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
134 *buf++ = 1; len++; // lo ver
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
135 *buf++ = 1; len++; // comp
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
136 *buf++ = 4; len++; // format - 8bpp
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
137 *buf++ = ZMBV_BLOCK; len++; // block width
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
138 *buf++ = ZMBV_BLOCK; len++; // block height
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
139 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
140 palptr = (uint32_t*)p->data[1];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
141 src = p->data[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
142 prev = c->prev;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
143 if(chpal){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
144 uint8_t tpal[3];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
145 for(i = 0; i < 256; i++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
146 tpal[0] = palptr[i] >> 16;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
147 tpal[1] = palptr[i] >> 8;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
148 tpal[2] = palptr[i];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
149 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
150 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
151 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
152 c->pal[i * 3 + 0] = tpal[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
153 c->pal[i * 3 + 1] = tpal[1];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
154 c->pal[i * 3 + 2] = tpal[2];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
155 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
156 memcpy(c->pal2, p->data[1], 1024);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
157 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
158 if(keyframe){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
159 for(i = 0; i < 256; i++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
160 c->pal[i*3 + 0] = palptr[i] >> 16;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
161 c->pal[i*3 + 1] = palptr[i] >> 8;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
162 c->pal[i*3 + 2] = palptr[i];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
163 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
164 memcpy(c->work_buf, c->pal, 768);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
165 memcpy(c->pal2, p->data[1], 1024);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
166 work_size = 768;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
167 for(i = 0; i < avctx->height; i++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
168 memcpy(c->work_buf + work_size, src, avctx->width);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
169 src += p->linesize[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
170 work_size += avctx->width;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
171 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
172 }else{
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
173 int x, y, bh2, bw2;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
174 uint8_t *tsrc, *tprev;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
175 uint8_t *mv;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
176 int mx, my, bv;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
177
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
178 bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
179 bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
180 mv = c->work_buf + work_size;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
181 memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
182 work_size += (bw * bh * 2 + 3) & ~3;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
183 /* for now just XOR'ing */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
184 for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
185 bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
186 for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
187 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
188
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
189 tsrc = src + x;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
190 tprev = prev + x;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
191
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
192 bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
193 mv[0] = (mx << 1) | !!bv;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
194 mv[1] = my << 1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
195 tprev += mx + my * c->pstride;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
196 if(bv){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
197 for(j = 0; j < bh2; j++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
198 for(i = 0; i < bw2; i++)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
199 c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
200 tsrc += p->linesize[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
201 tprev += c->pstride;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
202 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
203 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
204 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
205 src += p->linesize[0] * ZMBV_BLOCK;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
206 prev += c->pstride * ZMBV_BLOCK;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
207 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
208 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
209 /* save the previous frame */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
210 src = p->data[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
211 prev = c->prev;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
212 for(i = 0; i < avctx->height; i++){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
213 memcpy(prev, src, avctx->width);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
214 prev += c->pstride;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
215 src += p->linesize[0];
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
216 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
217
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
218 c->zstream.next_in = c->work_buf;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
219 c->zstream.avail_in = work_size;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
220 c->zstream.total_in = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
221
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
222 c->zstream.next_out = c->comp_buf;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
223 c->zstream.avail_out = c->comp_size;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
224 c->zstream.total_out = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
225 if((zret = deflate(&c->zstream, Z_SYNC_FLUSH)) != Z_OK){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
226 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
227 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
228 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
229
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
230 memcpy(buf, c->comp_buf, c->zstream.total_out);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
231 return len + c->zstream.total_out;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
232 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
233
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
234
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
235 /**
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
236 * Init zmbv encoder
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
237 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
238 static int encode_init(AVCodecContext *avctx)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
239 {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
240 ZmbvEncContext * const c = (ZmbvEncContext *)avctx->priv_data;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
241 int zret; // Zlib return code
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
242 int lvl = 9;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
243
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
244 c->avctx = avctx;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
245
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
246 c->pic.data[0] = NULL;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
247 c->curfrm = 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
248 c->keyint = avctx->keyint_min;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
249 c->range = 8;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
250 if(avctx->me_range > 0)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
251 c->range = FFMIN(avctx->me_range, 127);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
252
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
253 if(avctx->compression_level >= 0)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
254 lvl = avctx->compression_level;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
255 if(lvl < 0 || lvl > 9){
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
256 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
257 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
258 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
259
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
260 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
261 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
262 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
263
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
264 // Needed if zlib unused or init aborted before deflateInit
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
265 memset(&(c->zstream), 0, sizeof(z_stream));
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
266 c->comp_size = avctx->width * avctx->height + 1024 +
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
267 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
268 if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
269 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
270 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
271 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
272 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
273 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
274 ((c->comp_size + 63) >> 6) + 11;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
275
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
276 /* Allocate compression buffer */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
277 if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
278 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
279 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
280 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
281 c->pstride = (avctx->width + 15) & ~15;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
282 if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
283 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
284 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
285 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
286
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
287 c->zstream.zalloc = Z_NULL;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
288 c->zstream.zfree = Z_NULL;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
289 c->zstream.opaque = Z_NULL;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
290 zret = deflateInit(&(c->zstream), lvl);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
291 if (zret != Z_OK) {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
292 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
293 return -1;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
294 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
295
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
296 return 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
297 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
298
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
299
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
300
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
301 /**
4368
db099de6638f Fixed a typo, cosmetics.
banan
parents: 4287
diff changeset
302 * Uninit zmbv encoder
4285
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
303 */
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
304 static int encode_end(AVCodecContext *avctx)
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
305 {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
306 ZmbvEncContext * const c = (ZmbvEncContext *)avctx->priv_data;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
307
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
308 av_freep(&c->comp_buf);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
309 av_freep(&c->work_buf);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
310
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
311 deflateEnd(&(c->zstream));
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
312 av_freep(&c->prev);
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
313
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
314 return 0;
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
315 }
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
316
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
317 AVCodec zmbv_encoder = {
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
318 "zmbv",
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
319 CODEC_TYPE_VIDEO,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
320 CODEC_ID_ZMBV,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
321 sizeof(ZmbvEncContext),
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
322 encode_init,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
323 encode_frame,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
324 encode_end,
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
325 .pix_fmts = (enum PixelFormat[]){PIX_FMT_PAL8, -1},
60924fd86d18 1e6l forgot to add zmbvenc.c
kostya
parents:
diff changeset
326 };