ffv1.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*
  2. * FFV1 codec for libavcodec
  3. *
  4. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/ffv1.c
  24. * FF Video Codec 1 (an experimental lossless codec)
  25. */
  26. #include "avcodec.h"
  27. #include "bitstream.h"
  28. #include "dsputil.h"
  29. #include "rangecoder.h"
  30. #include "golomb.h"
  31. #include "mathops.h"
  32. #define MAX_PLANES 4
  33. #define CONTEXT_SIZE 32
  34. extern const uint8_t ff_log2_run[32];
  35. static const int8_t quant3[256]={
  36. 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  37. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  39. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  40. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  41. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  42. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  43. 1, 1, 1, 1, 1, 1, 1,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  86. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
  87. -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
  88. };
  89. static const int8_t quant9[256]={
  90. 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  91. 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  92. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  93. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  94. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  95. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  96. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  97. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  98. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  99. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  100. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  101. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  102. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  103. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
  104. -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
  105. -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
  106. };
  107. static const int8_t quant11[256]={
  108. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4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 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  131. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  132. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  133. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  134. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  135. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  136. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  137. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
  138. -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
  139. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  140. -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
  141. -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
  142. };
  143. typedef struct VlcState{
  144. int16_t drift;
  145. uint16_t error_sum;
  146. int8_t bias;
  147. uint8_t count;
  148. } VlcState;
  149. typedef struct PlaneContext{
  150. int context_count;
  151. uint8_t (*state)[CONTEXT_SIZE];
  152. VlcState *vlc_state;
  153. uint8_t interlace_bit_state[2];
  154. } PlaneContext;
  155. typedef struct FFV1Context{
  156. AVCodecContext *avctx;
  157. RangeCoder c;
  158. GetBitContext gb;
  159. PutBitContext pb;
  160. int version;
  161. int width, height;
  162. int chroma_h_shift, chroma_v_shift;
  163. int flags;
  164. int picture_number;
  165. AVFrame picture;
  166. int plane_count;
  167. int ac; ///< 1-> CABAC 0-> golomb rice
  168. PlaneContext plane[MAX_PLANES];
  169. int16_t quant_table[5][256];
  170. int run_index;
  171. int colorspace;
  172. DSPContext dsp;
  173. }FFV1Context;
  174. static av_always_inline int fold(int diff, int bits){
  175. if(bits==8)
  176. diff= (int8_t)diff;
  177. else{
  178. diff+= 1<<(bits-1);
  179. diff&=(1<<bits)-1;
  180. diff-= 1<<(bits-1);
  181. }
  182. return diff;
  183. }
  184. static inline int predict(int_fast16_t *src, int_fast16_t *last){
  185. const int LT= last[-1];
  186. const int T= last[ 0];
  187. const int L = src[-1];
  188. return mid_pred(L, L + T - LT, T);
  189. }
  190. static inline int get_context(FFV1Context *f, int_fast16_t *src, int_fast16_t *last, int_fast16_t *last2){
  191. const int LT= last[-1];
  192. const int T= last[ 0];
  193. const int RT= last[ 1];
  194. const int L = src[-1];
  195. if(f->quant_table[3][127]){
  196. const int TT= last2[0];
  197. const int LL= src[-2];
  198. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF]
  199. +f->quant_table[3][(LL-L) & 0xFF] + f->quant_table[4][(TT-T) & 0xFF];
  200. }else
  201. return f->quant_table[0][(L-LT) & 0xFF] + f->quant_table[1][(LT-T) & 0xFF] + f->quant_table[2][(T-RT) & 0xFF];
  202. }
  203. static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
  204. int i;
  205. if(v){
  206. const int a= FFABS(v);
  207. const int e= av_log2(a);
  208. put_rac(c, state+0, 0);
  209. assert(e<=9);
  210. for(i=0; i<e; i++){
  211. put_rac(c, state+1+i, 1); //1..10
  212. }
  213. put_rac(c, state+1+i, 0);
  214. for(i=e-1; i>=0; i--){
  215. put_rac(c, state+22+i, (a>>i)&1); //22..31
  216. }
  217. if(is_signed)
  218. put_rac(c, state+11 + e, v < 0); //11..21
  219. }else{
  220. put_rac(c, state+0, 1);
  221. }
  222. }
  223. static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
  224. if(get_rac(c, state+0))
  225. return 0;
  226. else{
  227. int i, e, a;
  228. e= 0;
  229. while(get_rac(c, state+1 + e) && e<9){ //1..10
  230. e++;
  231. }
  232. a= 1;
  233. for(i=e-1; i>=0; i--){
  234. a += a + get_rac(c, state+22 + i); //22..31
  235. }
  236. if(is_signed && get_rac(c, state+11 + e)) //11..21
  237. return -a;
  238. else
  239. return a;
  240. }
  241. }
  242. static inline void update_vlc_state(VlcState * const state, const int v){
  243. int drift= state->drift;
  244. int count= state->count;
  245. state->error_sum += FFABS(v);
  246. drift += v;
  247. if(count == 128){ //FIXME variable
  248. count >>= 1;
  249. drift >>= 1;
  250. state->error_sum >>= 1;
  251. }
  252. count++;
  253. if(drift <= -count){
  254. if(state->bias > -128) state->bias--;
  255. drift += count;
  256. if(drift <= -count)
  257. drift= -count + 1;
  258. }else if(drift > 0){
  259. if(state->bias < 127) state->bias++;
  260. drift -= count;
  261. if(drift > 0)
  262. drift= 0;
  263. }
  264. state->drift= drift;
  265. state->count= count;
  266. }
  267. static inline void put_vlc_symbol(PutBitContext *pb, VlcState * const state, int v, int bits){
  268. int i, k, code;
  269. //printf("final: %d ", v);
  270. v = fold(v - state->bias, bits);
  271. i= state->count;
  272. k=0;
  273. while(i < state->error_sum){ //FIXME optimize
  274. k++;
  275. i += i;
  276. }
  277. assert(k<=8);
  278. #if 0 // JPEG LS
  279. if(k==0 && 2*state->drift <= - state->count) code= v ^ (-1);
  280. else code= v;
  281. #else
  282. code= v ^ ((2*state->drift + state->count)>>31);
  283. #endif
  284. //printf("v:%d/%d bias:%d error:%d drift:%d count:%d k:%d\n", v, code, state->bias, state->error_sum, state->drift, state->count, k);
  285. set_sr_golomb(pb, code, k, 12, bits);
  286. update_vlc_state(state, v);
  287. }
  288. static inline int get_vlc_symbol(GetBitContext *gb, VlcState * const state, int bits){
  289. int k, i, v, ret;
  290. i= state->count;
  291. k=0;
  292. while(i < state->error_sum){ //FIXME optimize
  293. k++;
  294. i += i;
  295. }
  296. assert(k<=8);
  297. v= get_sr_golomb(gb, k, 12, bits);
  298. //printf("v:%d bias:%d error:%d drift:%d count:%d k:%d", v, state->bias, state->error_sum, state->drift, state->count, k);
  299. #if 0 // JPEG LS
  300. if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
  301. #else
  302. v ^= ((2*state->drift + state->count)>>31);
  303. #endif
  304. ret= fold(v + state->bias, bits);
  305. update_vlc_state(state, v);
  306. //printf("final: %d\n", ret);
  307. return ret;
  308. }
  309. #if CONFIG_FFV1_ENCODER
  310. static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  311. PlaneContext * const p= &s->plane[plane_index];
  312. RangeCoder * const c= &s->c;
  313. int x;
  314. int run_index= s->run_index;
  315. int run_count=0;
  316. int run_mode=0;
  317. if(s->ac){
  318. if(c->bytestream_end - c->bytestream < w*35){
  319. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  320. return -1;
  321. }
  322. }else{
  323. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
  324. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  325. return -1;
  326. }
  327. }
  328. for(x=0; x<w; x++){
  329. int diff, context;
  330. context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
  331. diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
  332. if(context < 0){
  333. context = -context;
  334. diff= -diff;
  335. }
  336. diff= fold(diff, bits);
  337. if(s->ac){
  338. put_symbol(c, p->state[context], diff, 1);
  339. }else{
  340. if(context == 0) run_mode=1;
  341. if(run_mode){
  342. if(diff){
  343. while(run_count >= 1<<ff_log2_run[run_index]){
  344. run_count -= 1<<ff_log2_run[run_index];
  345. run_index++;
  346. put_bits(&s->pb, 1, 1);
  347. }
  348. put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);
  349. if(run_index) run_index--;
  350. run_count=0;
  351. run_mode=0;
  352. if(diff>0) diff--;
  353. }else{
  354. run_count++;
  355. }
  356. }
  357. // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, (int)put_bits_count(&s->pb));
  358. if(run_mode == 0)
  359. put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
  360. }
  361. }
  362. if(run_mode){
  363. while(run_count >= 1<<ff_log2_run[run_index]){
  364. run_count -= 1<<ff_log2_run[run_index];
  365. run_index++;
  366. put_bits(&s->pb, 1, 1);
  367. }
  368. if(run_count)
  369. put_bits(&s->pb, 1, 1);
  370. }
  371. s->run_index= run_index;
  372. return 0;
  373. }
  374. static void encode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  375. int x,y,i;
  376. const int ring_size= s->avctx->context_model ? 3 : 2;
  377. int_fast16_t sample_buffer[ring_size][w+6], *sample[ring_size];
  378. s->run_index=0;
  379. memset(sample_buffer, 0, sizeof(sample_buffer));
  380. for(y=0; y<h; y++){
  381. for(i=0; i<ring_size; i++)
  382. sample[i]= sample_buffer[(h+i-y)%ring_size]+3;
  383. sample[0][-1]= sample[1][0 ];
  384. sample[1][ w]= sample[1][w-1];
  385. //{START_TIMER
  386. for(x=0; x<w; x++){
  387. sample[0][x]= src[x + stride*y];
  388. }
  389. encode_line(s, w, sample, plane_index, 8);
  390. //STOP_TIMER("encode line")}
  391. }
  392. }
  393. static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  394. int x, y, p, i;
  395. const int ring_size= s->avctx->context_model ? 3 : 2;
  396. int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];
  397. s->run_index=0;
  398. memset(sample_buffer, 0, sizeof(sample_buffer));
  399. for(y=0; y<h; y++){
  400. for(i=0; i<ring_size; i++)
  401. for(p=0; p<3; p++)
  402. sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;
  403. for(x=0; x<w; x++){
  404. int v= src[x + stride*y];
  405. int b= v&0xFF;
  406. int g= (v>>8)&0xFF;
  407. int r= (v>>16)&0xFF;
  408. b -= g;
  409. r -= g;
  410. g += (b + r)>>2;
  411. b += 0x100;
  412. r += 0x100;
  413. // assert(g>=0 && b>=0 && r>=0);
  414. // assert(g<256 && b<512 && r<512);
  415. sample[0][0][x]= g;
  416. sample[1][0][x]= b;
  417. sample[2][0][x]= r;
  418. }
  419. for(p=0; p<3; p++){
  420. sample[p][0][-1]= sample[p][1][0 ];
  421. sample[p][1][ w]= sample[p][1][w-1];
  422. encode_line(s, w, sample[p], FFMIN(p, 1), 9);
  423. }
  424. }
  425. }
  426. static void write_quant_table(RangeCoder *c, int16_t *quant_table){
  427. int last=0;
  428. int i;
  429. uint8_t state[CONTEXT_SIZE];
  430. memset(state, 128, sizeof(state));
  431. for(i=1; i<128 ; i++){
  432. if(quant_table[i] != quant_table[i-1]){
  433. put_symbol(c, state, i-last-1, 0);
  434. last= i;
  435. }
  436. }
  437. put_symbol(c, state, i-last-1, 0);
  438. }
  439. static void write_header(FFV1Context *f){
  440. uint8_t state[CONTEXT_SIZE];
  441. int i;
  442. RangeCoder * const c= &f->c;
  443. memset(state, 128, sizeof(state));
  444. put_symbol(c, state, f->version, 0);
  445. put_symbol(c, state, f->avctx->coder_type, 0);
  446. put_symbol(c, state, f->colorspace, 0); //YUV cs type
  447. put_rac(c, state, 1); //chroma planes
  448. put_symbol(c, state, f->chroma_h_shift, 0);
  449. put_symbol(c, state, f->chroma_v_shift, 0);
  450. put_rac(c, state, 0); //no transparency plane
  451. for(i=0; i<5; i++)
  452. write_quant_table(c, f->quant_table[i]);
  453. }
  454. #endif /* CONFIG_FFV1_ENCODER */
  455. static av_cold int common_init(AVCodecContext *avctx){
  456. FFV1Context *s = avctx->priv_data;
  457. int width, height;
  458. s->avctx= avctx;
  459. s->flags= avctx->flags;
  460. dsputil_init(&s->dsp, avctx);
  461. width= s->width= avctx->width;
  462. height= s->height= avctx->height;
  463. assert(width && height);
  464. return 0;
  465. }
  466. #if CONFIG_FFV1_ENCODER
  467. static av_cold int encode_init(AVCodecContext *avctx)
  468. {
  469. FFV1Context *s = avctx->priv_data;
  470. int i;
  471. common_init(avctx);
  472. s->version=0;
  473. s->ac= avctx->coder_type;
  474. s->plane_count=2;
  475. for(i=0; i<256; i++){
  476. s->quant_table[0][i]= quant11[i];
  477. s->quant_table[1][i]= 11*quant11[i];
  478. if(avctx->context_model==0){
  479. s->quant_table[2][i]= 11*11*quant11[i];
  480. s->quant_table[3][i]=
  481. s->quant_table[4][i]=0;
  482. }else{
  483. s->quant_table[2][i]= 11*11*quant5 [i];
  484. s->quant_table[3][i]= 5*11*11*quant5 [i];
  485. s->quant_table[4][i]= 5*5*11*11*quant5 [i];
  486. }
  487. }
  488. for(i=0; i<s->plane_count; i++){
  489. PlaneContext * const p= &s->plane[i];
  490. if(avctx->context_model==0){
  491. p->context_count= (11*11*11+1)/2;
  492. }else{
  493. p->context_count= (11*11*5*5*5+1)/2;
  494. }
  495. if(s->ac){
  496. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  497. }else{
  498. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  499. }
  500. }
  501. avctx->coded_frame= &s->picture;
  502. switch(avctx->pix_fmt){
  503. case PIX_FMT_YUV444P:
  504. case PIX_FMT_YUV422P:
  505. case PIX_FMT_YUV420P:
  506. case PIX_FMT_YUV411P:
  507. case PIX_FMT_YUV410P:
  508. s->colorspace= 0;
  509. break;
  510. case PIX_FMT_RGB32:
  511. s->colorspace= 1;
  512. break;
  513. default:
  514. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  515. return -1;
  516. }
  517. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
  518. s->picture_number=0;
  519. return 0;
  520. }
  521. #endif /* CONFIG_FFV1_ENCODER */
  522. static void clear_state(FFV1Context *f){
  523. int i, j;
  524. for(i=0; i<f->plane_count; i++){
  525. PlaneContext *p= &f->plane[i];
  526. p->interlace_bit_state[0]= 128;
  527. p->interlace_bit_state[1]= 128;
  528. for(j=0; j<p->context_count; j++){
  529. if(f->ac){
  530. memset(p->state[j], 128, sizeof(uint8_t)*CONTEXT_SIZE);
  531. }else{
  532. p->vlc_state[j].drift= 0;
  533. p->vlc_state[j].error_sum= 4; //FFMAX((RANGE + 32)/64, 2);
  534. p->vlc_state[j].bias= 0;
  535. p->vlc_state[j].count= 1;
  536. }
  537. }
  538. }
  539. }
  540. #if CONFIG_FFV1_ENCODER
  541. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  542. FFV1Context *f = avctx->priv_data;
  543. RangeCoder * const c= &f->c;
  544. AVFrame *pict = data;
  545. const int width= f->width;
  546. const int height= f->height;
  547. AVFrame * const p= &f->picture;
  548. int used_count= 0;
  549. uint8_t keystate=128;
  550. ff_init_range_encoder(c, buf, buf_size);
  551. // ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
  552. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  553. *p = *pict;
  554. p->pict_type= FF_I_TYPE;
  555. if(avctx->gop_size==0 || f->picture_number % avctx->gop_size == 0){
  556. put_rac(c, &keystate, 1);
  557. p->key_frame= 1;
  558. write_header(f);
  559. clear_state(f);
  560. }else{
  561. put_rac(c, &keystate, 0);
  562. p->key_frame= 0;
  563. }
  564. if(!f->ac){
  565. used_count += ff_rac_terminate(c);
  566. //printf("pos=%d\n", used_count);
  567. init_put_bits(&f->pb, buf + used_count, buf_size - used_count);
  568. }
  569. if(f->colorspace==0){
  570. const int chroma_width = -((-width )>>f->chroma_h_shift);
  571. const int chroma_height= -((-height)>>f->chroma_v_shift);
  572. encode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  573. encode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  574. encode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  575. }else{
  576. encode_rgb_frame(f, (uint32_t*)(p->data[0]), width, height, p->linesize[0]/4);
  577. }
  578. emms_c();
  579. f->picture_number++;
  580. if(f->ac){
  581. return ff_rac_terminate(c);
  582. }else{
  583. flush_put_bits(&f->pb); //nicer padding FIXME
  584. return used_count + (put_bits_count(&f->pb)+7)/8;
  585. }
  586. }
  587. #endif /* CONFIG_FFV1_ENCODER */
  588. static av_cold int common_end(AVCodecContext *avctx){
  589. FFV1Context *s = avctx->priv_data;
  590. int i;
  591. for(i=0; i<s->plane_count; i++){
  592. PlaneContext *p= &s->plane[i];
  593. av_freep(&p->state);
  594. av_freep(&p->vlc_state);
  595. }
  596. return 0;
  597. }
  598. static inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
  599. PlaneContext * const p= &s->plane[plane_index];
  600. RangeCoder * const c= &s->c;
  601. int x;
  602. int run_count=0;
  603. int run_mode=0;
  604. int run_index= s->run_index;
  605. for(x=0; x<w; x++){
  606. int diff, context, sign;
  607. context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);
  608. if(context < 0){
  609. context= -context;
  610. sign=1;
  611. }else
  612. sign=0;
  613. if(s->ac){
  614. diff= get_symbol(c, p->state[context], 1);
  615. }else{
  616. if(context == 0 && run_mode==0) run_mode=1;
  617. if(run_mode){
  618. if(run_count==0 && run_mode==1){
  619. if(get_bits1(&s->gb)){
  620. run_count = 1<<ff_log2_run[run_index];
  621. if(x + run_count <= w) run_index++;
  622. }else{
  623. if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);
  624. else run_count=0;
  625. if(run_index) run_index--;
  626. run_mode=2;
  627. }
  628. }
  629. run_count--;
  630. if(run_count < 0){
  631. run_mode=0;
  632. run_count=0;
  633. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  634. if(diff>=0) diff++;
  635. }else
  636. diff=0;
  637. }else
  638. diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
  639. // printf("count:%d index:%d, mode:%d, x:%d y:%d pos:%d\n", run_count, run_index, run_mode, x, y, get_bits_count(&s->gb));
  640. }
  641. if(sign) diff= -diff;
  642. sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);
  643. }
  644. s->run_index= run_index;
  645. }
  646. static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){
  647. int x, y;
  648. int_fast16_t sample_buffer[2][w+6];
  649. int_fast16_t *sample[2];
  650. sample[0]=sample_buffer[0]+3;
  651. sample[1]=sample_buffer[1]+3;
  652. s->run_index=0;
  653. memset(sample_buffer, 0, sizeof(sample_buffer));
  654. for(y=0; y<h; y++){
  655. int_fast16_t *temp= sample[0]; //FIXME try a normal buffer
  656. sample[0]= sample[1];
  657. sample[1]= temp;
  658. sample[1][-1]= sample[0][0 ];
  659. sample[0][ w]= sample[0][w-1];
  660. //{START_TIMER
  661. decode_line(s, w, sample, plane_index, 8);
  662. for(x=0; x<w; x++){
  663. src[x + stride*y]= sample[1][x];
  664. }
  665. //STOP_TIMER("decode-line")}
  666. }
  667. }
  668. static void decode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){
  669. int x, y, p;
  670. int_fast16_t sample_buffer[3][2][w+6];
  671. int_fast16_t *sample[3][2];
  672. for(x=0; x<3; x++){
  673. sample[x][0] = sample_buffer[x][0]+3;
  674. sample[x][1] = sample_buffer[x][1]+3;
  675. }
  676. s->run_index=0;
  677. memset(sample_buffer, 0, sizeof(sample_buffer));
  678. for(y=0; y<h; y++){
  679. for(p=0; p<3; p++){
  680. int_fast16_t *temp= sample[p][0]; //FIXME try a normal buffer
  681. sample[p][0]= sample[p][1];
  682. sample[p][1]= temp;
  683. sample[p][1][-1]= sample[p][0][0 ];
  684. sample[p][0][ w]= sample[p][0][w-1];
  685. decode_line(s, w, sample[p], FFMIN(p, 1), 9);
  686. }
  687. for(x=0; x<w; x++){
  688. int g= sample[0][1][x];
  689. int b= sample[1][1][x];
  690. int r= sample[2][1][x];
  691. // assert(g>=0 && b>=0 && r>=0);
  692. // assert(g<256 && b<512 && r<512);
  693. b -= 0x100;
  694. r -= 0x100;
  695. g -= (b + r)>>2;
  696. b += g;
  697. r += g;
  698. src[x + stride*y]= b + (g<<8) + (r<<16);
  699. }
  700. }
  701. }
  702. static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale){
  703. int v;
  704. int i=0;
  705. uint8_t state[CONTEXT_SIZE];
  706. memset(state, 128, sizeof(state));
  707. for(v=0; i<128 ; v++){
  708. int len= get_symbol(c, state, 0) + 1;
  709. if(len + i > 128) return -1;
  710. while(len--){
  711. quant_table[i] = scale*v;
  712. i++;
  713. //printf("%2d ",v);
  714. //if(i%16==0) printf("\n");
  715. }
  716. }
  717. for(i=1; i<128; i++){
  718. quant_table[256-i]= -quant_table[i];
  719. }
  720. quant_table[128]= -quant_table[127];
  721. return 2*v - 1;
  722. }
  723. static int read_header(FFV1Context *f){
  724. uint8_t state[CONTEXT_SIZE];
  725. int i, context_count;
  726. RangeCoder * const c= &f->c;
  727. memset(state, 128, sizeof(state));
  728. f->version= get_symbol(c, state, 0);
  729. f->ac= f->avctx->coder_type= get_symbol(c, state, 0);
  730. f->colorspace= get_symbol(c, state, 0); //YUV cs type
  731. get_rac(c, state); //no chroma = false
  732. f->chroma_h_shift= get_symbol(c, state, 0);
  733. f->chroma_v_shift= get_symbol(c, state, 0);
  734. get_rac(c, state); //transparency plane
  735. f->plane_count= 2;
  736. if(f->colorspace==0){
  737. switch(16*f->chroma_h_shift + f->chroma_v_shift){
  738. case 0x00: f->avctx->pix_fmt= PIX_FMT_YUV444P; break;
  739. case 0x10: f->avctx->pix_fmt= PIX_FMT_YUV422P; break;
  740. case 0x11: f->avctx->pix_fmt= PIX_FMT_YUV420P; break;
  741. case 0x20: f->avctx->pix_fmt= PIX_FMT_YUV411P; break;
  742. case 0x22: f->avctx->pix_fmt= PIX_FMT_YUV410P; break;
  743. default:
  744. av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
  745. return -1;
  746. }
  747. }else if(f->colorspace==1){
  748. if(f->chroma_h_shift || f->chroma_v_shift){
  749. av_log(f->avctx, AV_LOG_ERROR, "chroma subsampling not supported in this colorspace\n");
  750. return -1;
  751. }
  752. f->avctx->pix_fmt= PIX_FMT_RGB32;
  753. }else{
  754. av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
  755. return -1;
  756. }
  757. //printf("%d %d %d\n", f->chroma_h_shift, f->chroma_v_shift,f->avctx->pix_fmt);
  758. context_count=1;
  759. for(i=0; i<5; i++){
  760. context_count*= read_quant_table(c, f->quant_table[i], context_count);
  761. if(context_count < 0 || context_count > 32768){
  762. av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
  763. return -1;
  764. }
  765. }
  766. context_count= (context_count+1)/2;
  767. for(i=0; i<f->plane_count; i++){
  768. PlaneContext * const p= &f->plane[i];
  769. p->context_count= context_count;
  770. if(f->ac){
  771. if(!p->state) p->state= av_malloc(CONTEXT_SIZE*p->context_count*sizeof(uint8_t));
  772. }else{
  773. if(!p->vlc_state) p->vlc_state= av_malloc(p->context_count*sizeof(VlcState));
  774. }
  775. }
  776. return 0;
  777. }
  778. static av_cold int decode_init(AVCodecContext *avctx)
  779. {
  780. // FFV1Context *s = avctx->priv_data;
  781. common_init(avctx);
  782. return 0;
  783. }
  784. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size){
  785. FFV1Context *f = avctx->priv_data;
  786. RangeCoder * const c= &f->c;
  787. const int width= f->width;
  788. const int height= f->height;
  789. AVFrame * const p= &f->picture;
  790. int bytes_read;
  791. uint8_t keystate= 128;
  792. AVFrame *picture = data;
  793. ff_init_range_decoder(c, buf, buf_size);
  794. ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
  795. p->pict_type= FF_I_TYPE; //FIXME I vs. P
  796. if(get_rac(c, &keystate)){
  797. p->key_frame= 1;
  798. if(read_header(f) < 0)
  799. return -1;
  800. clear_state(f);
  801. }else{
  802. p->key_frame= 0;
  803. }
  804. if(!f->plane[0].state && !f->plane[0].vlc_state)
  805. return -1;
  806. p->reference= 0;
  807. if(avctx->get_buffer(avctx, p) < 0){
  808. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  809. return -1;
  810. }
  811. if(avctx->debug&FF_DEBUG_PICT_INFO)
  812. av_log(avctx, AV_LOG_ERROR, "keyframe:%d coder:%d\n", p->key_frame, f->ac);
  813. if(!f->ac){
  814. bytes_read = c->bytestream - c->bytestream_start - 1;
  815. if(bytes_read ==0) av_log(avctx, AV_LOG_ERROR, "error at end of AC stream\n"); //FIXME
  816. //printf("pos=%d\n", bytes_read);
  817. init_get_bits(&f->gb, buf + bytes_read, buf_size - bytes_read);
  818. } else {
  819. bytes_read = 0; /* avoid warning */
  820. }
  821. if(f->colorspace==0){
  822. const int chroma_width = -((-width )>>f->chroma_h_shift);
  823. const int chroma_height= -((-height)>>f->chroma_v_shift);
  824. decode_plane(f, p->data[0], width, height, p->linesize[0], 0);
  825. decode_plane(f, p->data[1], chroma_width, chroma_height, p->linesize[1], 1);
  826. decode_plane(f, p->data[2], chroma_width, chroma_height, p->linesize[2], 1);
  827. }else{
  828. decode_rgb_frame(f, (uint32_t*)p->data[0], width, height, p->linesize[0]/4);
  829. }
  830. emms_c();
  831. f->picture_number++;
  832. *picture= *p;
  833. avctx->release_buffer(avctx, p); //FIXME
  834. *data_size = sizeof(AVFrame);
  835. if(f->ac){
  836. bytes_read= c->bytestream - c->bytestream_start - 1;
  837. if(bytes_read ==0) av_log(f->avctx, AV_LOG_ERROR, "error at end of frame\n");
  838. }else{
  839. bytes_read+= (get_bits_count(&f->gb)+7)/8;
  840. }
  841. return bytes_read;
  842. }
  843. AVCodec ffv1_decoder = {
  844. "ffv1",
  845. CODEC_TYPE_VIDEO,
  846. CODEC_ID_FFV1,
  847. sizeof(FFV1Context),
  848. decode_init,
  849. NULL,
  850. common_end,
  851. decode_frame,
  852. CODEC_CAP_DR1 /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
  853. NULL,
  854. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
  855. };
  856. #if CONFIG_FFV1_ENCODER
  857. AVCodec ffv1_encoder = {
  858. "ffv1",
  859. CODEC_TYPE_VIDEO,
  860. CODEC_ID_FFV1,
  861. sizeof(FFV1Context),
  862. encode_init,
  863. encode_frame,
  864. common_end,
  865. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_RGB32, PIX_FMT_NONE},
  866. .long_name= NULL_IF_CONFIG_SMALL("FFmpeg codec #1"),
  867. };
  868. #endif