huffyuv.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. /*
  2. * huffyuv codec for libavcodec
  3. *
  4. * Copyright (c) 2002-2003 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
  7. * the algorithm used
  8. *
  9. * This file is part of FFmpeg.
  10. *
  11. * FFmpeg is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * FFmpeg is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with FFmpeg; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /**
  26. * @file libavcodec/huffyuv.c
  27. * huffyuv codec for libavcodec.
  28. */
  29. #include "avcodec.h"
  30. #include "bitstream.h"
  31. #include "dsputil.h"
  32. #define VLC_BITS 11
  33. #ifdef WORDS_BIGENDIAN
  34. #define B 3
  35. #define G 2
  36. #define R 1
  37. #else
  38. #define B 0
  39. #define G 1
  40. #define R 2
  41. #endif
  42. typedef enum Predictor{
  43. LEFT= 0,
  44. PLANE,
  45. MEDIAN,
  46. } Predictor;
  47. typedef struct HYuvContext{
  48. AVCodecContext *avctx;
  49. Predictor predictor;
  50. GetBitContext gb;
  51. PutBitContext pb;
  52. int interlaced;
  53. int decorrelate;
  54. int bitstream_bpp;
  55. int version;
  56. int yuy2; //use yuy2 instead of 422P
  57. int bgr32; //use bgr32 instead of bgr24
  58. int width, height;
  59. int flags;
  60. int context;
  61. int picture_number;
  62. int last_slice_end;
  63. uint8_t *temp[3];
  64. uint64_t stats[3][256];
  65. uint8_t len[3][256];
  66. uint32_t bits[3][256];
  67. uint32_t pix_bgr_map[1<<VLC_BITS];
  68. VLC vlc[6]; //Y,U,V,YY,YU,YV
  69. AVFrame picture;
  70. uint8_t *bitstream_buffer;
  71. unsigned int bitstream_buffer_size;
  72. DSPContext dsp;
  73. }HYuvContext;
  74. static const unsigned char classic_shift_luma[] = {
  75. 34,36,35,69,135,232,9,16,10,24,11,23,12,16,13,10,14,8,15,8,
  76. 16,8,17,20,16,10,207,206,205,236,11,8,10,21,9,23,8,8,199,70,
  77. 69,68, 0
  78. };
  79. static const unsigned char classic_shift_chroma[] = {
  80. 66,36,37,38,39,40,41,75,76,77,110,239,144,81,82,83,84,85,118,183,
  81. 56,57,88,89,56,89,154,57,58,57,26,141,57,56,58,57,58,57,184,119,
  82. 214,245,116,83,82,49,80,79,78,77,44,75,41,40,39,38,37,36,34, 0
  83. };
  84. static const unsigned char classic_add_luma[256] = {
  85. 3, 9, 5, 12, 10, 35, 32, 29, 27, 50, 48, 45, 44, 41, 39, 37,
  86. 73, 70, 68, 65, 64, 61, 58, 56, 53, 50, 49, 46, 44, 41, 38, 36,
  87. 68, 65, 63, 61, 58, 55, 53, 51, 48, 46, 45, 43, 41, 39, 38, 36,
  88. 35, 33, 32, 30, 29, 27, 26, 25, 48, 47, 46, 44, 43, 41, 40, 39,
  89. 37, 36, 35, 34, 32, 31, 30, 28, 27, 26, 24, 23, 22, 20, 19, 37,
  90. 35, 34, 33, 31, 30, 29, 27, 26, 24, 23, 21, 20, 18, 17, 15, 29,
  91. 27, 26, 24, 22, 21, 19, 17, 16, 14, 26, 25, 23, 21, 19, 18, 16,
  92. 15, 27, 25, 23, 21, 19, 17, 16, 14, 26, 25, 23, 21, 18, 17, 14,
  93. 12, 17, 19, 13, 4, 9, 2, 11, 1, 7, 8, 0, 16, 3, 14, 6,
  94. 12, 10, 5, 15, 18, 11, 10, 13, 15, 16, 19, 20, 22, 24, 27, 15,
  95. 18, 20, 22, 24, 26, 14, 17, 20, 22, 24, 27, 15, 18, 20, 23, 25,
  96. 28, 16, 19, 22, 25, 28, 32, 36, 21, 25, 29, 33, 38, 42, 45, 49,
  97. 28, 31, 34, 37, 40, 42, 44, 47, 49, 50, 52, 54, 56, 57, 59, 60,
  98. 62, 64, 66, 67, 69, 35, 37, 39, 40, 42, 43, 45, 47, 48, 51, 52,
  99. 54, 55, 57, 59, 60, 62, 63, 66, 67, 69, 71, 72, 38, 40, 42, 43,
  100. 46, 47, 49, 51, 26, 28, 30, 31, 33, 34, 18, 19, 11, 13, 7, 8,
  101. };
  102. static const unsigned char classic_add_chroma[256] = {
  103. 3, 1, 2, 2, 2, 2, 3, 3, 7, 5, 7, 5, 8, 6, 11, 9,
  104. 7, 13, 11, 10, 9, 8, 7, 5, 9, 7, 6, 4, 7, 5, 8, 7,
  105. 11, 8, 13, 11, 19, 15, 22, 23, 20, 33, 32, 28, 27, 29, 51, 77,
  106. 43, 45, 76, 81, 46, 82, 75, 55, 56,144, 58, 80, 60, 74,147, 63,
  107. 143, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  108. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 27, 30, 21, 22,
  109. 17, 14, 5, 6,100, 54, 47, 50, 51, 53,106,107,108,109,110,111,
  110. 112,113,114,115, 4,117,118, 92, 94,121,122, 3,124,103, 2, 1,
  111. 0,129,130,131,120,119,126,125,136,137,138,139,140,141,142,134,
  112. 135,132,133,104, 64,101, 62, 57,102, 95, 93, 59, 61, 28, 97, 96,
  113. 52, 49, 48, 29, 32, 25, 24, 46, 23, 98, 45, 44, 43, 20, 42, 41,
  114. 19, 18, 99, 40, 15, 39, 38, 16, 13, 12, 11, 37, 10, 9, 8, 36,
  115. 7,128,127,105,123,116, 35, 34, 33,145, 31, 79, 42,146, 78, 26,
  116. 83, 48, 49, 50, 44, 47, 26, 31, 30, 18, 17, 19, 21, 24, 25, 13,
  117. 14, 16, 17, 18, 20, 21, 12, 14, 15, 9, 10, 6, 9, 6, 5, 8,
  118. 6, 12, 8, 10, 7, 9, 6, 4, 6, 2, 2, 3, 3, 3, 3, 2,
  119. };
  120. static inline int add_left_prediction(uint8_t *dst, uint8_t *src, int w, int acc){
  121. int i;
  122. for(i=0; i<w-1; i++){
  123. acc+= src[i];
  124. dst[i]= acc;
  125. i++;
  126. acc+= src[i];
  127. dst[i]= acc;
  128. }
  129. for(; i<w; i++){
  130. acc+= src[i];
  131. dst[i]= acc;
  132. }
  133. return acc;
  134. }
  135. static inline void add_left_prediction_bgr32(uint8_t *dst, uint8_t *src, int w, int *red, int *green, int *blue){
  136. int i;
  137. int r,g,b;
  138. r= *red;
  139. g= *green;
  140. b= *blue;
  141. for(i=0; i<w; i++){
  142. b+= src[4*i+B];
  143. g+= src[4*i+G];
  144. r+= src[4*i+R];
  145. dst[4*i+B]= b;
  146. dst[4*i+G]= g;
  147. dst[4*i+R]= r;
  148. }
  149. *red= r;
  150. *green= g;
  151. *blue= b;
  152. }
  153. static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int left){
  154. int i;
  155. if(w<32){
  156. for(i=0; i<w; i++){
  157. const int temp= src[i];
  158. dst[i]= temp - left;
  159. left= temp;
  160. }
  161. return left;
  162. }else{
  163. for(i=0; i<16; i++){
  164. const int temp= src[i];
  165. dst[i]= temp - left;
  166. left= temp;
  167. }
  168. s->dsp.diff_bytes(dst+16, src+16, src+15, w-16);
  169. return src[w-1];
  170. }
  171. }
  172. static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst, uint8_t *src, int w, int *red, int *green, int *blue){
  173. int i;
  174. int r,g,b;
  175. r= *red;
  176. g= *green;
  177. b= *blue;
  178. for(i=0; i<FFMIN(w,4); i++){
  179. const int rt= src[i*4+R];
  180. const int gt= src[i*4+G];
  181. const int bt= src[i*4+B];
  182. dst[i*4+R]= rt - r;
  183. dst[i*4+G]= gt - g;
  184. dst[i*4+B]= bt - b;
  185. r = rt;
  186. g = gt;
  187. b = bt;
  188. }
  189. s->dsp.diff_bytes(dst+16, src+16, src+12, w*4-16);
  190. *red= src[(w-1)*4+R];
  191. *green= src[(w-1)*4+G];
  192. *blue= src[(w-1)*4+B];
  193. }
  194. static void read_len_table(uint8_t *dst, GetBitContext *gb){
  195. int i, val, repeat;
  196. for(i=0; i<256;){
  197. repeat= get_bits(gb, 3);
  198. val = get_bits(gb, 5);
  199. if(repeat==0)
  200. repeat= get_bits(gb, 8);
  201. //printf("%d %d\n", val, repeat);
  202. while (repeat--)
  203. dst[i++] = val;
  204. }
  205. }
  206. static int generate_bits_table(uint32_t *dst, uint8_t *len_table){
  207. int len, index;
  208. uint32_t bits=0;
  209. for(len=32; len>0; len--){
  210. for(index=0; index<256; index++){
  211. if(len_table[index]==len)
  212. dst[index]= bits++;
  213. }
  214. if(bits & 1){
  215. av_log(NULL, AV_LOG_ERROR, "Error generating huffman table\n");
  216. return -1;
  217. }
  218. bits >>= 1;
  219. }
  220. return 0;
  221. }
  222. #if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
  223. typedef struct {
  224. uint64_t val;
  225. int name;
  226. } HeapElem;
  227. static void heap_sift(HeapElem *h, int root, int size)
  228. {
  229. while(root*2+1 < size) {
  230. int child = root*2+1;
  231. if(child < size-1 && h[child].val > h[child+1].val)
  232. child++;
  233. if(h[root].val > h[child].val) {
  234. FFSWAP(HeapElem, h[root], h[child]);
  235. root = child;
  236. } else
  237. break;
  238. }
  239. }
  240. static void generate_len_table(uint8_t *dst, uint64_t *stats, int size){
  241. HeapElem h[size];
  242. int up[2*size];
  243. int len[2*size];
  244. int offset, i, next;
  245. for(offset=1; ; offset<<=1){
  246. for(i=0; i<size; i++){
  247. h[i].name = i;
  248. h[i].val = (stats[i] << 8) + offset;
  249. }
  250. for(i=size/2-1; i>=0; i--)
  251. heap_sift(h, i, size);
  252. for(next=size; next<size*2-1; next++){
  253. // merge the two smallest entries, and put it back in the heap
  254. uint64_t min1v = h[0].val;
  255. up[h[0].name] = next;
  256. h[0].val = INT64_MAX;
  257. heap_sift(h, 0, size);
  258. up[h[0].name] = next;
  259. h[0].name = next;
  260. h[0].val += min1v;
  261. heap_sift(h, 0, size);
  262. }
  263. len[2*size-2] = 0;
  264. for(i=2*size-3; i>=size; i--)
  265. len[i] = len[up[i]] + 1;
  266. for(i=0; i<size; i++) {
  267. dst[i] = len[up[i]] + 1;
  268. if(dst[i] >= 32) break;
  269. }
  270. if(i==size) break;
  271. }
  272. }
  273. #endif /* CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER */
  274. static void generate_joint_tables(HYuvContext *s){
  275. uint16_t symbols[1<<VLC_BITS];
  276. uint16_t bits[1<<VLC_BITS];
  277. uint8_t len[1<<VLC_BITS];
  278. if(s->bitstream_bpp < 24){
  279. int p, i, y, u;
  280. for(p=0; p<3; p++){
  281. for(i=y=0; y<256; y++){
  282. int len0 = s->len[0][y];
  283. int limit = VLC_BITS - len0;
  284. if(limit <= 0 || !len0)
  285. continue;
  286. for(u=0; u<256; u++){
  287. int len1 = s->len[p][u];
  288. if (len1 > limit || !len1)
  289. continue;
  290. assert(i < (1 << VLC_BITS));
  291. len[i] = len0 + len1;
  292. bits[i] = (s->bits[0][y] << len1) + s->bits[p][u];
  293. symbols[i] = (y<<8) + u;
  294. if(symbols[i] != 0xffff) // reserved to mean "invalid"
  295. i++;
  296. }
  297. }
  298. free_vlc(&s->vlc[3+p]);
  299. init_vlc_sparse(&s->vlc[3+p], VLC_BITS, i, len, 1, 1, bits, 2, 2, symbols, 2, 2, 0);
  300. }
  301. }else{
  302. uint8_t (*map)[4] = (uint8_t(*)[4])s->pix_bgr_map;
  303. int i, b, g, r, code;
  304. int p0 = s->decorrelate;
  305. int p1 = !s->decorrelate;
  306. // restrict the range to +/-16 becaues that's pretty much guaranteed to
  307. // cover all the combinations that fit in 11 bits total, and it doesn't
  308. // matter if we miss a few rare codes.
  309. for(i=0, g=-16; g<16; g++){
  310. int len0 = s->len[p0][g&255];
  311. int limit0 = VLC_BITS - len0;
  312. if (limit0 < 2 || !len0)
  313. continue;
  314. for(b=-16; b<16; b++){
  315. int len1 = s->len[p1][b&255];
  316. int limit1 = limit0 - len1;
  317. if (limit1 < 1 || !len1)
  318. continue;
  319. code = (s->bits[p0][g&255] << len1) + s->bits[p1][b&255];
  320. for(r=-16; r<16; r++){
  321. int len2 = s->len[2][r&255];
  322. if (len2 > limit1 || !len2)
  323. continue;
  324. assert(i < (1 << VLC_BITS));
  325. len[i] = len0 + len1 + len2;
  326. bits[i] = (code << len2) + s->bits[2][r&255];
  327. if(s->decorrelate){
  328. map[i][G] = g;
  329. map[i][B] = g+b;
  330. map[i][R] = g+r;
  331. }else{
  332. map[i][B] = g;
  333. map[i][G] = b;
  334. map[i][R] = r;
  335. }
  336. i++;
  337. }
  338. }
  339. }
  340. free_vlc(&s->vlc[3]);
  341. init_vlc(&s->vlc[3], VLC_BITS, i, len, 1, 1, bits, 2, 2, 0);
  342. }
  343. }
  344. static int read_huffman_tables(HYuvContext *s, uint8_t *src, int length){
  345. GetBitContext gb;
  346. int i;
  347. int ret;
  348. init_get_bits(&gb, src, length*8);
  349. for(i=0; i<3; i++){
  350. read_len_table(s->len[i], &gb);
  351. if(generate_bits_table(s->bits[i], s->len[i])<0){
  352. return -1;
  353. }
  354. #if 0
  355. for(j=0; j<256; j++){
  356. printf("%6X, %2d, %3d\n", s->bits[i][j], s->len[i][j], j);
  357. }
  358. #endif
  359. free_vlc(&s->vlc[i]);
  360. if ((ret = init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0)) < 0)
  361. return ret;
  362. }
  363. generate_joint_tables(s);
  364. return (get_bits_count(&gb)+7)/8;
  365. }
  366. static int read_old_huffman_tables(HYuvContext *s){
  367. #if 1
  368. GetBitContext gb;
  369. int i;
  370. int ret;
  371. init_get_bits(&gb, classic_shift_luma, sizeof(classic_shift_luma)*8);
  372. read_len_table(s->len[0], &gb);
  373. init_get_bits(&gb, classic_shift_chroma, sizeof(classic_shift_chroma)*8);
  374. read_len_table(s->len[1], &gb);
  375. for(i=0; i<256; i++) s->bits[0][i] = classic_add_luma [i];
  376. for(i=0; i<256; i++) s->bits[1][i] = classic_add_chroma[i];
  377. if(s->bitstream_bpp >= 24){
  378. memcpy(s->bits[1], s->bits[0], 256*sizeof(uint32_t));
  379. memcpy(s->len[1] , s->len [0], 256*sizeof(uint8_t));
  380. }
  381. memcpy(s->bits[2], s->bits[1], 256*sizeof(uint32_t));
  382. memcpy(s->len[2] , s->len [1], 256*sizeof(uint8_t));
  383. for(i=0; i<3; i++){
  384. free_vlc(&s->vlc[i]);
  385. if ((ret = init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1, s->bits[i], 4, 4, 0)) < 0)
  386. return ret;
  387. }
  388. generate_joint_tables(s);
  389. return 0;
  390. #else
  391. av_log(s->avctx, AV_LOG_DEBUG, "v1 huffyuv is not supported \n");
  392. return -1;
  393. #endif
  394. }
  395. static av_cold void alloc_temp(HYuvContext *s){
  396. int i;
  397. if(s->bitstream_bpp<24){
  398. for(i=0; i<3; i++){
  399. s->temp[i]= av_malloc(s->width + 16);
  400. }
  401. }else{
  402. for(i=0; i<2; i++){
  403. s->temp[i]= av_malloc(4*s->width + 16);
  404. }
  405. }
  406. }
  407. static av_cold int common_init(AVCodecContext *avctx){
  408. HYuvContext *s = avctx->priv_data;
  409. s->avctx= avctx;
  410. s->flags= avctx->flags;
  411. dsputil_init(&s->dsp, avctx);
  412. s->width= avctx->width;
  413. s->height= avctx->height;
  414. assert(s->width>0 && s->height>0);
  415. return 0;
  416. }
  417. #if CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER
  418. static av_cold int decode_init(AVCodecContext *avctx)
  419. {
  420. HYuvContext *s = avctx->priv_data;
  421. common_init(avctx);
  422. memset(s->vlc, 0, 3*sizeof(VLC));
  423. avctx->coded_frame= &s->picture;
  424. s->interlaced= s->height > 288;
  425. s->bgr32=1;
  426. //if(avctx->extradata)
  427. // printf("extradata:%X, extradata_size:%d\n", *(uint32_t*)avctx->extradata, avctx->extradata_size);
  428. if(avctx->extradata_size){
  429. if((avctx->bits_per_coded_sample&7) && avctx->bits_per_coded_sample != 12)
  430. s->version=1; // do such files exist at all?
  431. else
  432. s->version=2;
  433. }else
  434. s->version=0;
  435. if(s->version==2){
  436. int method, interlace;
  437. method= ((uint8_t*)avctx->extradata)[0];
  438. s->decorrelate= method&64 ? 1 : 0;
  439. s->predictor= method&63;
  440. s->bitstream_bpp= ((uint8_t*)avctx->extradata)[1];
  441. if(s->bitstream_bpp==0)
  442. s->bitstream_bpp= avctx->bits_per_coded_sample&~7;
  443. interlace= (((uint8_t*)avctx->extradata)[2] & 0x30) >> 4;
  444. s->interlaced= (interlace==1) ? 1 : (interlace==2) ? 0 : s->interlaced;
  445. s->context= ((uint8_t*)avctx->extradata)[2] & 0x40 ? 1 : 0;
  446. if(read_huffman_tables(s, ((uint8_t*)avctx->extradata)+4, avctx->extradata_size) < 0)
  447. return -1;
  448. }else{
  449. switch(avctx->bits_per_coded_sample&7){
  450. case 1:
  451. s->predictor= LEFT;
  452. s->decorrelate= 0;
  453. break;
  454. case 2:
  455. s->predictor= LEFT;
  456. s->decorrelate= 1;
  457. break;
  458. case 3:
  459. s->predictor= PLANE;
  460. s->decorrelate= avctx->bits_per_coded_sample >= 24;
  461. break;
  462. case 4:
  463. s->predictor= MEDIAN;
  464. s->decorrelate= 0;
  465. break;
  466. default:
  467. s->predictor= LEFT; //OLD
  468. s->decorrelate= 0;
  469. break;
  470. }
  471. s->bitstream_bpp= avctx->bits_per_coded_sample & ~7;
  472. s->context= 0;
  473. if(read_old_huffman_tables(s) < 0)
  474. return -1;
  475. }
  476. switch(s->bitstream_bpp){
  477. case 12:
  478. avctx->pix_fmt = PIX_FMT_YUV420P;
  479. break;
  480. case 16:
  481. if(s->yuy2){
  482. avctx->pix_fmt = PIX_FMT_YUYV422;
  483. }else{
  484. avctx->pix_fmt = PIX_FMT_YUV422P;
  485. }
  486. break;
  487. case 24:
  488. case 32:
  489. if(s->bgr32){
  490. avctx->pix_fmt = PIX_FMT_RGB32;
  491. }else{
  492. avctx->pix_fmt = PIX_FMT_BGR24;
  493. }
  494. break;
  495. default:
  496. assert(0);
  497. }
  498. if (s->predictor == MEDIAN && avctx->pix_fmt == PIX_FMT_YUV422P && avctx->width%4) {
  499. av_log(avctx, AV_LOG_ERROR, "width must be a multiple of 4 this colorspace and predictor\n");
  500. return AVERROR_INVALIDDATA;
  501. }
  502. alloc_temp(s);
  503. // av_log(NULL, AV_LOG_DEBUG, "pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_coded_sample, s->interlaced);
  504. return 0;
  505. }
  506. #endif /* CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER */
  507. #if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
  508. static int store_table(HYuvContext *s, uint8_t *len, uint8_t *buf){
  509. int i;
  510. int index= 0;
  511. for(i=0; i<256;){
  512. int val= len[i];
  513. int repeat=0;
  514. for(; i<256 && len[i]==val && repeat<255; i++)
  515. repeat++;
  516. assert(val < 32 && val >0 && repeat<256 && repeat>0);
  517. if(repeat>7){
  518. buf[index++]= val;
  519. buf[index++]= repeat;
  520. }else{
  521. buf[index++]= val | (repeat<<5);
  522. }
  523. }
  524. return index;
  525. }
  526. static av_cold int encode_init(AVCodecContext *avctx)
  527. {
  528. HYuvContext *s = avctx->priv_data;
  529. int i, j;
  530. common_init(avctx);
  531. avctx->extradata= av_mallocz(1024*30); // 256*3+4 == 772
  532. avctx->stats_out= av_mallocz(1024*30); // 21*256*3(%llu ) + 3(\n) + 1(0) = 16132
  533. s->version=2;
  534. avctx->coded_frame= &s->picture;
  535. switch(avctx->pix_fmt){
  536. case PIX_FMT_YUV420P:
  537. s->bitstream_bpp= 12;
  538. break;
  539. case PIX_FMT_YUV422P:
  540. s->bitstream_bpp= 16;
  541. break;
  542. case PIX_FMT_RGB32:
  543. s->bitstream_bpp= 24;
  544. break;
  545. default:
  546. av_log(avctx, AV_LOG_ERROR, "format not supported\n");
  547. return -1;
  548. }
  549. avctx->bits_per_coded_sample= s->bitstream_bpp;
  550. s->decorrelate= s->bitstream_bpp >= 24;
  551. s->predictor= avctx->prediction_method;
  552. s->interlaced= avctx->flags&CODEC_FLAG_INTERLACED_ME ? 1 : 0;
  553. if(avctx->context_model==1){
  554. s->context= avctx->context_model;
  555. if(s->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)){
  556. av_log(avctx, AV_LOG_ERROR, "context=1 is not compatible with 2 pass huffyuv encoding\n");
  557. return -1;
  558. }
  559. }else s->context= 0;
  560. if(avctx->codec->id==CODEC_ID_HUFFYUV){
  561. if(avctx->pix_fmt==PIX_FMT_YUV420P){
  562. av_log(avctx, AV_LOG_ERROR, "Error: YV12 is not supported by huffyuv; use vcodec=ffvhuff or format=422p\n");
  563. return -1;
  564. }
  565. if(avctx->context_model){
  566. av_log(avctx, AV_LOG_ERROR, "Error: per-frame huffman tables are not supported by huffyuv; use vcodec=ffvhuff\n");
  567. return -1;
  568. }
  569. if(s->interlaced != ( s->height > 288 ))
  570. av_log(avctx, AV_LOG_INFO, "using huffyuv 2.2.0 or newer interlacing flag\n");
  571. }
  572. if(s->bitstream_bpp>=24 && s->predictor==MEDIAN){
  573. av_log(avctx, AV_LOG_ERROR, "Error: RGB is incompatible with median predictor\n");
  574. return -1;
  575. }
  576. ((uint8_t*)avctx->extradata)[0]= s->predictor | (s->decorrelate << 6);
  577. ((uint8_t*)avctx->extradata)[1]= s->bitstream_bpp;
  578. ((uint8_t*)avctx->extradata)[2]= s->interlaced ? 0x10 : 0x20;
  579. if(s->context)
  580. ((uint8_t*)avctx->extradata)[2]|= 0x40;
  581. ((uint8_t*)avctx->extradata)[3]= 0;
  582. s->avctx->extradata_size= 4;
  583. if(avctx->stats_in){
  584. char *p= avctx->stats_in;
  585. for(i=0; i<3; i++)
  586. for(j=0; j<256; j++)
  587. s->stats[i][j]= 1;
  588. for(;;){
  589. for(i=0; i<3; i++){
  590. char *next;
  591. for(j=0; j<256; j++){
  592. s->stats[i][j]+= strtol(p, &next, 0);
  593. if(next==p) return -1;
  594. p=next;
  595. }
  596. }
  597. if(p[0]==0 || p[1]==0 || p[2]==0) break;
  598. }
  599. }else{
  600. for(i=0; i<3; i++)
  601. for(j=0; j<256; j++){
  602. int d= FFMIN(j, 256-j);
  603. s->stats[i][j]= 100000000/(d+1);
  604. }
  605. }
  606. for(i=0; i<3; i++){
  607. generate_len_table(s->len[i], s->stats[i], 256);
  608. if(generate_bits_table(s->bits[i], s->len[i])<0){
  609. return -1;
  610. }
  611. s->avctx->extradata_size+=
  612. store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]);
  613. }
  614. if(s->context){
  615. for(i=0; i<3; i++){
  616. int pels = s->width*s->height / (i?40:10);
  617. for(j=0; j<256; j++){
  618. int d= FFMIN(j, 256-j);
  619. s->stats[i][j]= pels/(d+1);
  620. }
  621. }
  622. }else{
  623. for(i=0; i<3; i++)
  624. for(j=0; j<256; j++)
  625. s->stats[i][j]= 0;
  626. }
  627. // printf("pred:%d bpp:%d hbpp:%d il:%d\n", s->predictor, s->bitstream_bpp, avctx->bits_per_coded_sample, s->interlaced);
  628. alloc_temp(s);
  629. s->picture_number=0;
  630. return 0;
  631. }
  632. #endif /* CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER */
  633. /* TODO instead of restarting the read when the code isn't in the first level
  634. * of the joint table, jump into the 2nd level of the individual table. */
  635. #define READ_2PIX(dst0, dst1, plane1){\
  636. uint16_t code = get_vlc2(&s->gb, s->vlc[3+plane1].table, VLC_BITS, 1);\
  637. if(code != 0xffff){\
  638. dst0 = code>>8;\
  639. dst1 = code;\
  640. }else{\
  641. dst0 = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);\
  642. dst1 = get_vlc2(&s->gb, s->vlc[plane1].table, VLC_BITS, 3);\
  643. }\
  644. }
  645. static void decode_422_bitstream(HYuvContext *s, int count){
  646. int i;
  647. count/=2;
  648. if(count >= (s->gb.size_in_bits - get_bits_count(&s->gb))/(31*4)){
  649. for(i=0; i<count && get_bits_count(&s->gb) < s->gb.size_in_bits; i++){
  650. READ_2PIX(s->temp[0][2*i ], s->temp[1][i], 1);
  651. READ_2PIX(s->temp[0][2*i+1], s->temp[2][i], 2);
  652. }
  653. }else{
  654. for(i=0; i<count; i++){
  655. READ_2PIX(s->temp[0][2*i ], s->temp[1][i], 1);
  656. READ_2PIX(s->temp[0][2*i+1], s->temp[2][i], 2);
  657. }
  658. }
  659. }
  660. static void decode_gray_bitstream(HYuvContext *s, int count){
  661. int i;
  662. count/=2;
  663. if(count >= (s->gb.size_in_bits - get_bits_count(&s->gb))/(31*2)){
  664. for(i=0; i<count && get_bits_count(&s->gb) < s->gb.size_in_bits; i++){
  665. READ_2PIX(s->temp[0][2*i ], s->temp[0][2*i+1], 0);
  666. }
  667. }else{
  668. for(i=0; i<count; i++){
  669. READ_2PIX(s->temp[0][2*i ], s->temp[0][2*i+1], 0);
  670. }
  671. }
  672. }
  673. #if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
  674. static int encode_422_bitstream(HYuvContext *s, int count){
  675. int i;
  676. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 2*4*count){
  677. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  678. return -1;
  679. }
  680. #define LOAD4\
  681. int y0 = s->temp[0][2*i];\
  682. int y1 = s->temp[0][2*i+1];\
  683. int u0 = s->temp[1][i];\
  684. int v0 = s->temp[2][i];
  685. count/=2;
  686. if(s->flags&CODEC_FLAG_PASS1){
  687. for(i=0; i<count; i++){
  688. LOAD4;
  689. s->stats[0][y0]++;
  690. s->stats[1][u0]++;
  691. s->stats[0][y1]++;
  692. s->stats[2][v0]++;
  693. }
  694. }
  695. if(s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT)
  696. return 0;
  697. if(s->context){
  698. for(i=0; i<count; i++){
  699. LOAD4;
  700. s->stats[0][y0]++;
  701. put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
  702. s->stats[1][u0]++;
  703. put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
  704. s->stats[0][y1]++;
  705. put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
  706. s->stats[2][v0]++;
  707. put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
  708. }
  709. }else{
  710. for(i=0; i<count; i++){
  711. LOAD4;
  712. put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
  713. put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
  714. put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
  715. put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
  716. }
  717. }
  718. return 0;
  719. }
  720. static int encode_gray_bitstream(HYuvContext *s, int count){
  721. int i;
  722. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 4*count){
  723. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  724. return -1;
  725. }
  726. #define LOAD2\
  727. int y0 = s->temp[0][2*i];\
  728. int y1 = s->temp[0][2*i+1];
  729. #define STAT2\
  730. s->stats[0][y0]++;\
  731. s->stats[0][y1]++;
  732. #define WRITE2\
  733. put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
  734. put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
  735. count/=2;
  736. if(s->flags&CODEC_FLAG_PASS1){
  737. for(i=0; i<count; i++){
  738. LOAD2;
  739. STAT2;
  740. }
  741. }
  742. if(s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT)
  743. return 0;
  744. if(s->context){
  745. for(i=0; i<count; i++){
  746. LOAD2;
  747. STAT2;
  748. WRITE2;
  749. }
  750. }else{
  751. for(i=0; i<count; i++){
  752. LOAD2;
  753. WRITE2;
  754. }
  755. }
  756. return 0;
  757. }
  758. #endif /* CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER */
  759. static av_always_inline void decode_bgr_1(HYuvContext *s, int count, int decorrelate, int alpha){
  760. int i;
  761. for(i=0; i<count; i++){
  762. int code = get_vlc2(&s->gb, s->vlc[3].table, VLC_BITS, 1);
  763. if(code != -1){
  764. *(uint32_t*)&s->temp[0][4*i] = s->pix_bgr_map[code];
  765. }else if(decorrelate){
  766. s->temp[0][4*i+G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
  767. s->temp[0][4*i+B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3) + s->temp[0][4*i+G];
  768. s->temp[0][4*i+R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3) + s->temp[0][4*i+G];
  769. }else{
  770. s->temp[0][4*i+B] = get_vlc2(&s->gb, s->vlc[0].table, VLC_BITS, 3);
  771. s->temp[0][4*i+G] = get_vlc2(&s->gb, s->vlc[1].table, VLC_BITS, 3);
  772. s->temp[0][4*i+R] = get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3);
  773. }
  774. if(alpha)
  775. get_vlc2(&s->gb, s->vlc[2].table, VLC_BITS, 3); //?!
  776. }
  777. }
  778. static void decode_bgr_bitstream(HYuvContext *s, int count){
  779. if(s->decorrelate){
  780. if(s->bitstream_bpp==24)
  781. decode_bgr_1(s, count, 1, 0);
  782. else
  783. decode_bgr_1(s, count, 1, 1);
  784. }else{
  785. if(s->bitstream_bpp==24)
  786. decode_bgr_1(s, count, 0, 0);
  787. else
  788. decode_bgr_1(s, count, 0, 1);
  789. }
  790. }
  791. static int encode_bgr_bitstream(HYuvContext *s, int count){
  792. int i;
  793. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 3*4*count){
  794. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  795. return -1;
  796. }
  797. #define LOAD3\
  798. int g= s->temp[0][4*i+G];\
  799. int b= (s->temp[0][4*i+B] - g) & 0xff;\
  800. int r= (s->temp[0][4*i+R] - g) & 0xff;
  801. #define STAT3\
  802. s->stats[0][b]++;\
  803. s->stats[1][g]++;\
  804. s->stats[2][r]++;
  805. #define WRITE3\
  806. put_bits(&s->pb, s->len[1][g], s->bits[1][g]);\
  807. put_bits(&s->pb, s->len[0][b], s->bits[0][b]);\
  808. put_bits(&s->pb, s->len[2][r], s->bits[2][r]);
  809. if((s->flags&CODEC_FLAG_PASS1) && (s->avctx->flags2&CODEC_FLAG2_NO_OUTPUT)){
  810. for(i=0; i<count; i++){
  811. LOAD3;
  812. STAT3;
  813. }
  814. }else if(s->context || (s->flags&CODEC_FLAG_PASS1)){
  815. for(i=0; i<count; i++){
  816. LOAD3;
  817. STAT3;
  818. WRITE3;
  819. }
  820. }else{
  821. for(i=0; i<count; i++){
  822. LOAD3;
  823. WRITE3;
  824. }
  825. }
  826. return 0;
  827. }
  828. #if CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER
  829. static void draw_slice(HYuvContext *s, int y){
  830. int h, cy;
  831. int offset[4];
  832. if(s->avctx->draw_horiz_band==NULL)
  833. return;
  834. h= y - s->last_slice_end;
  835. y -= h;
  836. if(s->bitstream_bpp==12){
  837. cy= y>>1;
  838. }else{
  839. cy= y;
  840. }
  841. offset[0] = s->picture.linesize[0]*y;
  842. offset[1] = s->picture.linesize[1]*cy;
  843. offset[2] = s->picture.linesize[2]*cy;
  844. offset[3] = 0;
  845. emms_c();
  846. s->avctx->draw_horiz_band(s->avctx, &s->picture, offset, y, 3, h);
  847. s->last_slice_end= y + h;
  848. }
  849. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size){
  850. HYuvContext *s = avctx->priv_data;
  851. const int width= s->width;
  852. const int width2= s->width>>1;
  853. const int height= s->height;
  854. int fake_ystride, fake_ustride, fake_vstride;
  855. AVFrame * const p= &s->picture;
  856. int table_size= 0;
  857. AVFrame *picture = data;
  858. s->bitstream_buffer= av_fast_realloc(s->bitstream_buffer, &s->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  859. s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer, (const uint32_t*)buf, buf_size/4);
  860. if(p->data[0])
  861. avctx->release_buffer(avctx, p);
  862. p->reference= 0;
  863. if(avctx->get_buffer(avctx, p) < 0){
  864. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  865. return -1;
  866. }
  867. if(s->context){
  868. table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
  869. if(table_size < 0)
  870. return -1;
  871. }
  872. if((unsigned)(buf_size-table_size) >= INT_MAX/8)
  873. return -1;
  874. init_get_bits(&s->gb, s->bitstream_buffer+table_size, (buf_size-table_size)*8);
  875. fake_ystride= s->interlaced ? p->linesize[0]*2 : p->linesize[0];
  876. fake_ustride= s->interlaced ? p->linesize[1]*2 : p->linesize[1];
  877. fake_vstride= s->interlaced ? p->linesize[2]*2 : p->linesize[2];
  878. s->last_slice_end= 0;
  879. if(s->bitstream_bpp<24){
  880. int y, cy;
  881. int lefty, leftu, leftv;
  882. int lefttopy, lefttopu, lefttopv;
  883. if(s->yuy2){
  884. p->data[0][3]= get_bits(&s->gb, 8);
  885. p->data[0][2]= get_bits(&s->gb, 8);
  886. p->data[0][1]= get_bits(&s->gb, 8);
  887. p->data[0][0]= get_bits(&s->gb, 8);
  888. av_log(avctx, AV_LOG_ERROR, "YUY2 output is not implemented yet\n");
  889. return -1;
  890. }else{
  891. leftv= p->data[2][0]= get_bits(&s->gb, 8);
  892. lefty= p->data[0][1]= get_bits(&s->gb, 8);
  893. leftu= p->data[1][0]= get_bits(&s->gb, 8);
  894. p->data[0][0]= get_bits(&s->gb, 8);
  895. switch(s->predictor){
  896. case LEFT:
  897. case PLANE:
  898. decode_422_bitstream(s, width-2);
  899. lefty= add_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty);
  900. if(!(s->flags&CODEC_FLAG_GRAY)){
  901. leftu= add_left_prediction(p->data[1] + 1, s->temp[1], width2-1, leftu);
  902. leftv= add_left_prediction(p->data[2] + 1, s->temp[2], width2-1, leftv);
  903. }
  904. for(cy=y=1; y<s->height; y++,cy++){
  905. uint8_t *ydst, *udst, *vdst;
  906. if(s->bitstream_bpp==12){
  907. decode_gray_bitstream(s, width);
  908. ydst= p->data[0] + p->linesize[0]*y;
  909. lefty= add_left_prediction(ydst, s->temp[0], width, lefty);
  910. if(s->predictor == PLANE){
  911. if(y>s->interlaced)
  912. s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
  913. }
  914. y++;
  915. if(y>=s->height) break;
  916. }
  917. draw_slice(s, y);
  918. ydst= p->data[0] + p->linesize[0]*y;
  919. udst= p->data[1] + p->linesize[1]*cy;
  920. vdst= p->data[2] + p->linesize[2]*cy;
  921. decode_422_bitstream(s, width);
  922. lefty= add_left_prediction(ydst, s->temp[0], width, lefty);
  923. if(!(s->flags&CODEC_FLAG_GRAY)){
  924. leftu= add_left_prediction(udst, s->temp[1], width2, leftu);
  925. leftv= add_left_prediction(vdst, s->temp[2], width2, leftv);
  926. }
  927. if(s->predictor == PLANE){
  928. if(cy>s->interlaced){
  929. s->dsp.add_bytes(ydst, ydst - fake_ystride, width);
  930. if(!(s->flags&CODEC_FLAG_GRAY)){
  931. s->dsp.add_bytes(udst, udst - fake_ustride, width2);
  932. s->dsp.add_bytes(vdst, vdst - fake_vstride, width2);
  933. }
  934. }
  935. }
  936. }
  937. draw_slice(s, height);
  938. break;
  939. case MEDIAN:
  940. /* first line except first 2 pixels is left predicted */
  941. decode_422_bitstream(s, width-2);
  942. lefty= add_left_prediction(p->data[0] + 2, s->temp[0], width-2, lefty);
  943. if(!(s->flags&CODEC_FLAG_GRAY)){
  944. leftu= add_left_prediction(p->data[1] + 1, s->temp[1], width2-1, leftu);
  945. leftv= add_left_prediction(p->data[2] + 1, s->temp[2], width2-1, leftv);
  946. }
  947. cy=y=1;
  948. /* second line is left predicted for interlaced case */
  949. if(s->interlaced){
  950. decode_422_bitstream(s, width);
  951. lefty= add_left_prediction(p->data[0] + p->linesize[0], s->temp[0], width, lefty);
  952. if(!(s->flags&CODEC_FLAG_GRAY)){
  953. leftu= add_left_prediction(p->data[1] + p->linesize[2], s->temp[1], width2, leftu);
  954. leftv= add_left_prediction(p->data[2] + p->linesize[1], s->temp[2], width2, leftv);
  955. }
  956. y++; cy++;
  957. }
  958. /* next 4 pixels are left predicted too */
  959. decode_422_bitstream(s, 4);
  960. lefty= add_left_prediction(p->data[0] + fake_ystride, s->temp[0], 4, lefty);
  961. if(!(s->flags&CODEC_FLAG_GRAY)){
  962. leftu= add_left_prediction(p->data[1] + fake_ustride, s->temp[1], 2, leftu);
  963. leftv= add_left_prediction(p->data[2] + fake_vstride, s->temp[2], 2, leftv);
  964. }
  965. /* next line except the first 4 pixels is median predicted */
  966. lefttopy= p->data[0][3];
  967. decode_422_bitstream(s, width-4);
  968. s->dsp.add_hfyu_median_prediction(p->data[0] + fake_ystride+4, p->data[0]+4, s->temp[0], width-4, &lefty, &lefttopy);
  969. if(!(s->flags&CODEC_FLAG_GRAY)){
  970. lefttopu= p->data[1][1];
  971. lefttopv= p->data[2][1];
  972. s->dsp.add_hfyu_median_prediction(p->data[1] + fake_ustride+2, p->data[1]+2, s->temp[1], width2-2, &leftu, &lefttopu);
  973. s->dsp.add_hfyu_median_prediction(p->data[2] + fake_vstride+2, p->data[2]+2, s->temp[2], width2-2, &leftv, &lefttopv);
  974. }
  975. y++; cy++;
  976. for(; y<height; y++,cy++){
  977. uint8_t *ydst, *udst, *vdst;
  978. if(s->bitstream_bpp==12){
  979. while(2*cy > y){
  980. decode_gray_bitstream(s, width);
  981. ydst= p->data[0] + p->linesize[0]*y;
  982. s->dsp.add_hfyu_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
  983. y++;
  984. }
  985. if(y>=height) break;
  986. }
  987. draw_slice(s, y);
  988. decode_422_bitstream(s, width);
  989. ydst= p->data[0] + p->linesize[0]*y;
  990. udst= p->data[1] + p->linesize[1]*cy;
  991. vdst= p->data[2] + p->linesize[2]*cy;
  992. s->dsp.add_hfyu_median_prediction(ydst, ydst - fake_ystride, s->temp[0], width, &lefty, &lefttopy);
  993. if(!(s->flags&CODEC_FLAG_GRAY)){
  994. s->dsp.add_hfyu_median_prediction(udst, udst - fake_ustride, s->temp[1], width2, &leftu, &lefttopu);
  995. s->dsp.add_hfyu_median_prediction(vdst, vdst - fake_vstride, s->temp[2], width2, &leftv, &lefttopv);
  996. }
  997. }
  998. draw_slice(s, height);
  999. break;
  1000. }
  1001. }
  1002. }else{
  1003. int y;
  1004. int leftr, leftg, leftb;
  1005. const int last_line= (height-1)*p->linesize[0];
  1006. if(s->bitstream_bpp==32){
  1007. skip_bits(&s->gb, 8);
  1008. leftr= p->data[0][last_line+R]= get_bits(&s->gb, 8);
  1009. leftg= p->data[0][last_line+G]= get_bits(&s->gb, 8);
  1010. leftb= p->data[0][last_line+B]= get_bits(&s->gb, 8);
  1011. }else{
  1012. leftr= p->data[0][last_line+R]= get_bits(&s->gb, 8);
  1013. leftg= p->data[0][last_line+G]= get_bits(&s->gb, 8);
  1014. leftb= p->data[0][last_line+B]= get_bits(&s->gb, 8);
  1015. skip_bits(&s->gb, 8);
  1016. }
  1017. if(s->bgr32){
  1018. switch(s->predictor){
  1019. case LEFT:
  1020. case PLANE:
  1021. decode_bgr_bitstream(s, width-1);
  1022. add_left_prediction_bgr32(p->data[0] + last_line+4, s->temp[0], width-1, &leftr, &leftg, &leftb);
  1023. for(y=s->height-2; y>=0; y--){ //Yes it is stored upside down.
  1024. decode_bgr_bitstream(s, width);
  1025. add_left_prediction_bgr32(p->data[0] + p->linesize[0]*y, s->temp[0], width, &leftr, &leftg, &leftb);
  1026. if(s->predictor == PLANE){
  1027. if((y&s->interlaced)==0 && y<s->height-1-s->interlaced){
  1028. s->dsp.add_bytes(p->data[0] + p->linesize[0]*y,
  1029. p->data[0] + p->linesize[0]*y + fake_ystride, fake_ystride);
  1030. }
  1031. }
  1032. }
  1033. draw_slice(s, height); // just 1 large slice as this is not possible in reverse order
  1034. break;
  1035. default:
  1036. av_log(avctx, AV_LOG_ERROR, "prediction type not supported!\n");
  1037. }
  1038. }else{
  1039. av_log(avctx, AV_LOG_ERROR, "BGR24 output is not implemented yet\n");
  1040. return -1;
  1041. }
  1042. }
  1043. emms_c();
  1044. *picture= *p;
  1045. *data_size = sizeof(AVFrame);
  1046. return (get_bits_count(&s->gb)+31)/32*4 + table_size;
  1047. }
  1048. #endif /* CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER */
  1049. static int common_end(HYuvContext *s){
  1050. int i;
  1051. for(i=0; i<3; i++){
  1052. av_freep(&s->temp[i]);
  1053. }
  1054. return 0;
  1055. }
  1056. #if CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER
  1057. static av_cold int decode_end(AVCodecContext *avctx)
  1058. {
  1059. HYuvContext *s = avctx->priv_data;
  1060. int i;
  1061. common_end(s);
  1062. av_freep(&s->bitstream_buffer);
  1063. for(i=0; i<6; i++){
  1064. free_vlc(&s->vlc[i]);
  1065. }
  1066. return 0;
  1067. }
  1068. #endif /* CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER */
  1069. #if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
  1070. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  1071. HYuvContext *s = avctx->priv_data;
  1072. AVFrame *pict = data;
  1073. const int width= s->width;
  1074. const int width2= s->width>>1;
  1075. const int height= s->height;
  1076. const int fake_ystride= s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
  1077. const int fake_ustride= s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
  1078. const int fake_vstride= s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
  1079. AVFrame * const p= &s->picture;
  1080. int i, j, size=0;
  1081. *p = *pict;
  1082. p->pict_type= FF_I_TYPE;
  1083. p->key_frame= 1;
  1084. if(s->context){
  1085. for(i=0; i<3; i++){
  1086. generate_len_table(s->len[i], s->stats[i], 256);
  1087. if(generate_bits_table(s->bits[i], s->len[i])<0)
  1088. return -1;
  1089. size+= store_table(s, s->len[i], &buf[size]);
  1090. }
  1091. for(i=0; i<3; i++)
  1092. for(j=0; j<256; j++)
  1093. s->stats[i][j] >>= 1;
  1094. }
  1095. init_put_bits(&s->pb, buf+size, buf_size-size);
  1096. if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){
  1097. int lefty, leftu, leftv, y, cy;
  1098. put_bits(&s->pb, 8, leftv= p->data[2][0]);
  1099. put_bits(&s->pb, 8, lefty= p->data[0][1]);
  1100. put_bits(&s->pb, 8, leftu= p->data[1][0]);
  1101. put_bits(&s->pb, 8, p->data[0][0]);
  1102. lefty= sub_left_prediction(s, s->temp[0], p->data[0]+2, width-2 , lefty);
  1103. leftu= sub_left_prediction(s, s->temp[1], p->data[1]+1, width2-1, leftu);
  1104. leftv= sub_left_prediction(s, s->temp[2], p->data[2]+1, width2-1, leftv);
  1105. encode_422_bitstream(s, width-2);
  1106. if(s->predictor==MEDIAN){
  1107. int lefttopy, lefttopu, lefttopv;
  1108. cy=y=1;
  1109. if(s->interlaced){
  1110. lefty= sub_left_prediction(s, s->temp[0], p->data[0]+p->linesize[0], width , lefty);
  1111. leftu= sub_left_prediction(s, s->temp[1], p->data[1]+p->linesize[1], width2, leftu);
  1112. leftv= sub_left_prediction(s, s->temp[2], p->data[2]+p->linesize[2], width2, leftv);
  1113. encode_422_bitstream(s, width);
  1114. y++; cy++;
  1115. }
  1116. lefty= sub_left_prediction(s, s->temp[0], p->data[0]+fake_ystride, 4, lefty);
  1117. leftu= sub_left_prediction(s, s->temp[1], p->data[1]+fake_ustride, 2, leftu);
  1118. leftv= sub_left_prediction(s, s->temp[2], p->data[2]+fake_vstride, 2, leftv);
  1119. encode_422_bitstream(s, 4);
  1120. lefttopy= p->data[0][3];
  1121. lefttopu= p->data[1][1];
  1122. lefttopv= p->data[2][1];
  1123. s->dsp.sub_hfyu_median_prediction(s->temp[0], p->data[0]+4, p->data[0] + fake_ystride+4, width-4 , &lefty, &lefttopy);
  1124. s->dsp.sub_hfyu_median_prediction(s->temp[1], p->data[1]+2, p->data[1] + fake_ustride+2, width2-2, &leftu, &lefttopu);
  1125. s->dsp.sub_hfyu_median_prediction(s->temp[2], p->data[2]+2, p->data[2] + fake_vstride+2, width2-2, &leftv, &lefttopv);
  1126. encode_422_bitstream(s, width-4);
  1127. y++; cy++;
  1128. for(; y<height; y++,cy++){
  1129. uint8_t *ydst, *udst, *vdst;
  1130. if(s->bitstream_bpp==12){
  1131. while(2*cy > y){
  1132. ydst= p->data[0] + p->linesize[0]*y;
  1133. s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
  1134. encode_gray_bitstream(s, width);
  1135. y++;
  1136. }
  1137. if(y>=height) break;
  1138. }
  1139. ydst= p->data[0] + p->linesize[0]*y;
  1140. udst= p->data[1] + p->linesize[1]*cy;
  1141. vdst= p->data[2] + p->linesize[2]*cy;
  1142. s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
  1143. s->dsp.sub_hfyu_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
  1144. s->dsp.sub_hfyu_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
  1145. encode_422_bitstream(s, width);
  1146. }
  1147. }else{
  1148. for(cy=y=1; y<height; y++,cy++){
  1149. uint8_t *ydst, *udst, *vdst;
  1150. /* encode a luma only line & y++ */
  1151. if(s->bitstream_bpp==12){
  1152. ydst= p->data[0] + p->linesize[0]*y;
  1153. if(s->predictor == PLANE && s->interlaced < y){
  1154. s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
  1155. lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
  1156. }else{
  1157. lefty= sub_left_prediction(s, s->temp[0], ydst, width , lefty);
  1158. }
  1159. encode_gray_bitstream(s, width);
  1160. y++;
  1161. if(y>=height) break;
  1162. }
  1163. ydst= p->data[0] + p->linesize[0]*y;
  1164. udst= p->data[1] + p->linesize[1]*cy;
  1165. vdst= p->data[2] + p->linesize[2]*cy;
  1166. if(s->predictor == PLANE && s->interlaced < cy){
  1167. s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
  1168. s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
  1169. s->dsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
  1170. lefty= sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
  1171. leftu= sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
  1172. leftv= sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
  1173. }else{
  1174. lefty= sub_left_prediction(s, s->temp[0], ydst, width , lefty);
  1175. leftu= sub_left_prediction(s, s->temp[1], udst, width2, leftu);
  1176. leftv= sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
  1177. }
  1178. encode_422_bitstream(s, width);
  1179. }
  1180. }
  1181. }else if(avctx->pix_fmt == PIX_FMT_RGB32){
  1182. uint8_t *data = p->data[0] + (height-1)*p->linesize[0];
  1183. const int stride = -p->linesize[0];
  1184. const int fake_stride = -fake_ystride;
  1185. int y;
  1186. int leftr, leftg, leftb;
  1187. put_bits(&s->pb, 8, leftr= data[R]);
  1188. put_bits(&s->pb, 8, leftg= data[G]);
  1189. put_bits(&s->pb, 8, leftb= data[B]);
  1190. put_bits(&s->pb, 8, 0);
  1191. sub_left_prediction_bgr32(s, s->temp[0], data+4, width-1, &leftr, &leftg, &leftb);
  1192. encode_bgr_bitstream(s, width-1);
  1193. for(y=1; y<s->height; y++){
  1194. uint8_t *dst = data + y*stride;
  1195. if(s->predictor == PLANE && s->interlaced < y){
  1196. s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width*4);
  1197. sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width, &leftr, &leftg, &leftb);
  1198. }else{
  1199. sub_left_prediction_bgr32(s, s->temp[0], dst, width, &leftr, &leftg, &leftb);
  1200. }
  1201. encode_bgr_bitstream(s, width);
  1202. }
  1203. }else{
  1204. av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
  1205. }
  1206. emms_c();
  1207. size+= (put_bits_count(&s->pb)+31)/8;
  1208. size/= 4;
  1209. if((s->flags&CODEC_FLAG_PASS1) && (s->picture_number&31)==0){
  1210. int j;
  1211. char *p= avctx->stats_out;
  1212. char *end= p + 1024*30;
  1213. for(i=0; i<3; i++){
  1214. for(j=0; j<256; j++){
  1215. snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
  1216. p+= strlen(p);
  1217. s->stats[i][j]= 0;
  1218. }
  1219. snprintf(p, end-p, "\n");
  1220. p++;
  1221. }
  1222. } else
  1223. avctx->stats_out[0] = '\0';
  1224. if(!(s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)){
  1225. flush_put_bits(&s->pb);
  1226. s->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
  1227. }
  1228. s->picture_number++;
  1229. return size*4;
  1230. }
  1231. static av_cold int encode_end(AVCodecContext *avctx)
  1232. {
  1233. HYuvContext *s = avctx->priv_data;
  1234. common_end(s);
  1235. av_freep(&avctx->extradata);
  1236. av_freep(&avctx->stats_out);
  1237. return 0;
  1238. }
  1239. #endif /* CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER */
  1240. #if CONFIG_HUFFYUV_DECODER
  1241. AVCodec huffyuv_decoder = {
  1242. "huffyuv",
  1243. CODEC_TYPE_VIDEO,
  1244. CODEC_ID_HUFFYUV,
  1245. sizeof(HYuvContext),
  1246. decode_init,
  1247. NULL,
  1248. decode_end,
  1249. decode_frame,
  1250. CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
  1251. NULL,
  1252. .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
  1253. };
  1254. #endif
  1255. #if CONFIG_FFVHUFF_DECODER
  1256. AVCodec ffvhuff_decoder = {
  1257. "ffvhuff",
  1258. CODEC_TYPE_VIDEO,
  1259. CODEC_ID_FFVHUFF,
  1260. sizeof(HYuvContext),
  1261. decode_init,
  1262. NULL,
  1263. decode_end,
  1264. decode_frame,
  1265. CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
  1266. NULL,
  1267. .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
  1268. };
  1269. #endif
  1270. #if CONFIG_HUFFYUV_ENCODER
  1271. AVCodec huffyuv_encoder = {
  1272. "huffyuv",
  1273. CODEC_TYPE_VIDEO,
  1274. CODEC_ID_HUFFYUV,
  1275. sizeof(HYuvContext),
  1276. encode_init,
  1277. encode_frame,
  1278. encode_end,
  1279. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_RGB32, PIX_FMT_NONE},
  1280. .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
  1281. };
  1282. #endif
  1283. #if CONFIG_FFVHUFF_ENCODER
  1284. AVCodec ffvhuff_encoder = {
  1285. "ffvhuff",
  1286. CODEC_TYPE_VIDEO,
  1287. CODEC_ID_FFVHUFF,
  1288. sizeof(HYuvContext),
  1289. encode_init,
  1290. encode_frame,
  1291. encode_end,
  1292. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_RGB32, PIX_FMT_NONE},
  1293. .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
  1294. };
  1295. #endif