bitstream.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "vp8/common/header.h"
  11. #include "encodemv.h"
  12. #include "vp8/common/entropymode.h"
  13. #include "vp8/common/findnearmv.h"
  14. #include "mcomp.h"
  15. #include "vp8/common/systemdependent.h"
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <limits.h>
  19. #include "vpx/vpx_encoder.h"
  20. #include "vpx_mem/vpx_mem.h"
  21. #include "vpx_ports/system_state.h"
  22. #include "bitstream.h"
  23. #include "defaultcoefcounts.h"
  24. #include "vp8/common/common.h"
  25. const int vp8cx_base_skip_false_prob[128] = {
  26. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  27. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  28. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  29. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 251, 248, 244, 240,
  30. 236, 232, 229, 225, 221, 217, 213, 208, 204, 199, 194, 190, 187, 183, 179,
  31. 175, 172, 168, 164, 160, 157, 153, 149, 145, 142, 138, 134, 130, 127, 124,
  32. 120, 117, 114, 110, 107, 104, 101, 98, 95, 92, 89, 86, 83, 80, 77,
  33. 74, 71, 68, 65, 62, 59, 56, 53, 50, 47, 44, 41, 38, 35, 32,
  34. 30, 28, 26, 24, 22, 20, 18, 16,
  35. };
  36. #if defined(SECTIONBITS_OUTPUT)
  37. unsigned __int64 Sectionbits[500];
  38. #endif
  39. #ifdef VP8_ENTROPY_STATS
  40. int intra_mode_stats[10][10][10];
  41. static unsigned int tree_update_hist[BLOCK_TYPES][COEF_BANDS]
  42. [PREV_COEF_CONTEXTS][ENTROPY_NODES][2];
  43. extern unsigned int active_section;
  44. #endif
  45. #ifdef MODE_STATS
  46. int count_mb_seg[4] = { 0, 0, 0, 0 };
  47. #endif
  48. static void update_mode(vp8_writer *const w, int n, vp8_token tok[/* n */],
  49. vp8_tree tree, vp8_prob Pnew[/* n-1 */],
  50. vp8_prob Pcur[/* n-1 */],
  51. unsigned int bct[/* n-1 */][2],
  52. const unsigned int num_events[/* n */]) {
  53. unsigned int new_b = 0, old_b = 0;
  54. int i = 0;
  55. vp8_tree_probs_from_distribution(n--, tok, tree, Pnew, bct, num_events, 256,
  56. 1);
  57. do {
  58. new_b += vp8_cost_branch(bct[i], Pnew[i]);
  59. old_b += vp8_cost_branch(bct[i], Pcur[i]);
  60. } while (++i < n);
  61. if (new_b + (n << 8) < old_b) {
  62. int j = 0;
  63. vp8_write_bit(w, 1);
  64. do {
  65. const vp8_prob p = Pnew[j];
  66. vp8_write_literal(w, Pcur[j] = p ? p : 1, 8);
  67. } while (++j < n);
  68. } else
  69. vp8_write_bit(w, 0);
  70. }
  71. static void update_mbintra_mode_probs(VP8_COMP *cpi) {
  72. VP8_COMMON *const x = &cpi->common;
  73. vp8_writer *const w = cpi->bc;
  74. {
  75. vp8_prob Pnew[VP8_YMODES - 1];
  76. unsigned int bct[VP8_YMODES - 1][2];
  77. update_mode(w, VP8_YMODES, vp8_ymode_encodings, vp8_ymode_tree, Pnew,
  78. x->fc.ymode_prob, bct, (unsigned int *)cpi->mb.ymode_count);
  79. }
  80. {
  81. vp8_prob Pnew[VP8_UV_MODES - 1];
  82. unsigned int bct[VP8_UV_MODES - 1][2];
  83. update_mode(w, VP8_UV_MODES, vp8_uv_mode_encodings, vp8_uv_mode_tree, Pnew,
  84. x->fc.uv_mode_prob, bct, (unsigned int *)cpi->mb.uv_mode_count);
  85. }
  86. }
  87. static void write_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
  88. vp8_write_token(bc, vp8_ymode_tree, p, vp8_ymode_encodings + m);
  89. }
  90. static void kfwrite_ymode(vp8_writer *bc, int m, const vp8_prob *p) {
  91. vp8_write_token(bc, vp8_kf_ymode_tree, p, vp8_kf_ymode_encodings + m);
  92. }
  93. static void write_uv_mode(vp8_writer *bc, int m, const vp8_prob *p) {
  94. vp8_write_token(bc, vp8_uv_mode_tree, p, vp8_uv_mode_encodings + m);
  95. }
  96. static void write_bmode(vp8_writer *bc, int m, const vp8_prob *p) {
  97. vp8_write_token(bc, vp8_bmode_tree, p, vp8_bmode_encodings + m);
  98. }
  99. static void write_split(vp8_writer *bc, int x) {
  100. vp8_write_token(bc, vp8_mbsplit_tree, vp8_mbsplit_probs,
  101. vp8_mbsplit_encodings + x);
  102. }
  103. void vp8_pack_tokens(vp8_writer *w, const TOKENEXTRA *p, int xcount) {
  104. const TOKENEXTRA *stop = p + xcount;
  105. unsigned int split;
  106. int shift;
  107. int count = w->count;
  108. unsigned int range = w->range;
  109. unsigned int lowvalue = w->lowvalue;
  110. while (p < stop) {
  111. const int t = p->Token;
  112. vp8_token *a = vp8_coef_encodings + t;
  113. const vp8_extra_bit_struct *b = vp8_extra_bits + t;
  114. int i = 0;
  115. const unsigned char *pp = p->context_tree;
  116. int v = a->value;
  117. int n = a->Len;
  118. if (p->skip_eob_node) {
  119. n--;
  120. i = 2;
  121. }
  122. do {
  123. const int bb = (v >> --n) & 1;
  124. split = 1 + (((range - 1) * pp[i >> 1]) >> 8);
  125. i = vp8_coef_tree[i + bb];
  126. if (bb) {
  127. lowvalue += split;
  128. range = range - split;
  129. } else {
  130. range = split;
  131. }
  132. shift = vp8_norm[range];
  133. range <<= shift;
  134. count += shift;
  135. if (count >= 0) {
  136. int offset = shift - count;
  137. if ((lowvalue << (offset - 1)) & 0x80000000) {
  138. int x = w->pos - 1;
  139. while (x >= 0 && w->buffer[x] == 0xff) {
  140. w->buffer[x] = (unsigned char)0;
  141. x--;
  142. }
  143. w->buffer[x] += 1;
  144. }
  145. validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
  146. w->buffer[w->pos++] = (lowvalue >> (24 - offset));
  147. lowvalue <<= offset;
  148. shift = count;
  149. lowvalue &= 0xffffff;
  150. count -= 8;
  151. }
  152. lowvalue <<= shift;
  153. } while (n);
  154. if (b->base_val) {
  155. const int e = p->Extra, L = b->Len;
  156. if (L) {
  157. const unsigned char *proba = b->prob;
  158. const int v2 = e >> 1;
  159. int n2 = L; /* number of bits in v2, assumed nonzero */
  160. i = 0;
  161. do {
  162. const int bb = (v2 >> --n2) & 1;
  163. split = 1 + (((range - 1) * proba[i >> 1]) >> 8);
  164. i = b->tree[i + bb];
  165. if (bb) {
  166. lowvalue += split;
  167. range = range - split;
  168. } else {
  169. range = split;
  170. }
  171. shift = vp8_norm[range];
  172. range <<= shift;
  173. count += shift;
  174. if (count >= 0) {
  175. int offset = shift - count;
  176. if ((lowvalue << (offset - 1)) & 0x80000000) {
  177. int x = w->pos - 1;
  178. while (x >= 0 && w->buffer[x] == 0xff) {
  179. w->buffer[x] = (unsigned char)0;
  180. x--;
  181. }
  182. w->buffer[x] += 1;
  183. }
  184. validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
  185. w->buffer[w->pos++] = (lowvalue >> (24 - offset));
  186. lowvalue <<= offset;
  187. shift = count;
  188. lowvalue &= 0xffffff;
  189. count -= 8;
  190. }
  191. lowvalue <<= shift;
  192. } while (n2);
  193. }
  194. {
  195. split = (range + 1) >> 1;
  196. if (e & 1) {
  197. lowvalue += split;
  198. range = range - split;
  199. } else {
  200. range = split;
  201. }
  202. range <<= 1;
  203. if ((lowvalue & 0x80000000)) {
  204. int x = w->pos - 1;
  205. while (x >= 0 && w->buffer[x] == 0xff) {
  206. w->buffer[x] = (unsigned char)0;
  207. x--;
  208. }
  209. w->buffer[x] += 1;
  210. }
  211. lowvalue <<= 1;
  212. if (!++count) {
  213. count = -8;
  214. validate_buffer(w->buffer + w->pos, 1, w->buffer_end, w->error);
  215. w->buffer[w->pos++] = (lowvalue >> 24);
  216. lowvalue &= 0xffffff;
  217. }
  218. }
  219. }
  220. ++p;
  221. }
  222. w->count = count;
  223. w->lowvalue = lowvalue;
  224. w->range = range;
  225. }
  226. static void write_partition_size(unsigned char *cx_data, int size) {
  227. signed char csize;
  228. csize = size & 0xff;
  229. *cx_data = csize;
  230. csize = (size >> 8) & 0xff;
  231. *(cx_data + 1) = csize;
  232. csize = (size >> 16) & 0xff;
  233. *(cx_data + 2) = csize;
  234. }
  235. static void pack_tokens_into_partitions(VP8_COMP *cpi, unsigned char *cx_data,
  236. unsigned char *cx_data_end,
  237. int num_part) {
  238. int i;
  239. unsigned char *ptr = cx_data;
  240. unsigned char *ptr_end = cx_data_end;
  241. vp8_writer *w;
  242. for (i = 0; i < num_part; ++i) {
  243. int mb_row;
  244. w = cpi->bc + i + 1;
  245. vp8_start_encode(w, ptr, ptr_end);
  246. for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part) {
  247. const TOKENEXTRA *p = cpi->tplist[mb_row].start;
  248. const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
  249. int tokens = (int)(stop - p);
  250. vp8_pack_tokens(w, p, tokens);
  251. }
  252. vp8_stop_encode(w);
  253. ptr += w->pos;
  254. }
  255. }
  256. #if CONFIG_MULTITHREAD
  257. static void pack_mb_row_tokens(VP8_COMP *cpi, vp8_writer *w) {
  258. int mb_row;
  259. for (mb_row = 0; mb_row < cpi->common.mb_rows; ++mb_row) {
  260. const TOKENEXTRA *p = cpi->tplist[mb_row].start;
  261. const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
  262. int tokens = (int)(stop - p);
  263. vp8_pack_tokens(w, p, tokens);
  264. }
  265. }
  266. #endif // CONFIG_MULTITHREAD
  267. static void write_mv_ref(vp8_writer *w, MB_PREDICTION_MODE m,
  268. const vp8_prob *p) {
  269. assert(NEARESTMV <= m && m <= SPLITMV);
  270. vp8_write_token(w, vp8_mv_ref_tree, p,
  271. vp8_mv_ref_encoding_array + (m - NEARESTMV));
  272. }
  273. static void write_sub_mv_ref(vp8_writer *w, B_PREDICTION_MODE m,
  274. const vp8_prob *p) {
  275. assert(LEFT4X4 <= m && m <= NEW4X4);
  276. vp8_write_token(w, vp8_sub_mv_ref_tree, p,
  277. vp8_sub_mv_ref_encoding_array + (m - LEFT4X4));
  278. }
  279. static void write_mv(vp8_writer *w, const MV *mv, const int_mv *ref,
  280. const MV_CONTEXT *mvc) {
  281. MV e;
  282. e.row = mv->row - ref->as_mv.row;
  283. e.col = mv->col - ref->as_mv.col;
  284. vp8_encode_motion_vector(w, &e, mvc);
  285. }
  286. static void write_mb_features(vp8_writer *w, const MB_MODE_INFO *mi,
  287. const MACROBLOCKD *x) {
  288. /* Encode the MB segment id. */
  289. if (x->segmentation_enabled && x->update_mb_segmentation_map) {
  290. switch (mi->segment_id) {
  291. case 0:
  292. vp8_write(w, 0, x->mb_segment_tree_probs[0]);
  293. vp8_write(w, 0, x->mb_segment_tree_probs[1]);
  294. break;
  295. case 1:
  296. vp8_write(w, 0, x->mb_segment_tree_probs[0]);
  297. vp8_write(w, 1, x->mb_segment_tree_probs[1]);
  298. break;
  299. case 2:
  300. vp8_write(w, 1, x->mb_segment_tree_probs[0]);
  301. vp8_write(w, 0, x->mb_segment_tree_probs[2]);
  302. break;
  303. case 3:
  304. vp8_write(w, 1, x->mb_segment_tree_probs[0]);
  305. vp8_write(w, 1, x->mb_segment_tree_probs[2]);
  306. break;
  307. /* TRAP.. This should not happen */
  308. default:
  309. vp8_write(w, 0, x->mb_segment_tree_probs[0]);
  310. vp8_write(w, 0, x->mb_segment_tree_probs[1]);
  311. break;
  312. }
  313. }
  314. }
  315. void vp8_convert_rfct_to_prob(VP8_COMP *const cpi) {
  316. const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
  317. const int rf_intra = rfct[INTRA_FRAME];
  318. const int rf_inter =
  319. rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
  320. /* Calculate the probabilities used to code the ref frame based on usage */
  321. if (!(cpi->prob_intra_coded = rf_intra * 255 / (rf_intra + rf_inter))) {
  322. cpi->prob_intra_coded = 1;
  323. }
  324. cpi->prob_last_coded = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
  325. if (!cpi->prob_last_coded) cpi->prob_last_coded = 1;
  326. cpi->prob_gf_coded = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
  327. ? (rfct[GOLDEN_FRAME] * 255) /
  328. (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
  329. : 128;
  330. if (!cpi->prob_gf_coded) cpi->prob_gf_coded = 1;
  331. }
  332. static void pack_inter_mode_mvs(VP8_COMP *const cpi) {
  333. VP8_COMMON *const pc = &cpi->common;
  334. vp8_writer *const w = cpi->bc;
  335. const MV_CONTEXT *mvc = pc->fc.mvc;
  336. MODE_INFO *m = pc->mi;
  337. const int mis = pc->mode_info_stride;
  338. int mb_row = -1;
  339. int prob_skip_false = 0;
  340. cpi->mb.partition_info = cpi->mb.pi;
  341. vp8_convert_rfct_to_prob(cpi);
  342. #ifdef VP8_ENTROPY_STATS
  343. active_section = 1;
  344. #endif
  345. if (pc->mb_no_coeff_skip) {
  346. int total_mbs = pc->mb_rows * pc->mb_cols;
  347. prob_skip_false = (total_mbs - cpi->mb.skip_true_count) * 256 / total_mbs;
  348. if (prob_skip_false <= 1) prob_skip_false = 1;
  349. if (prob_skip_false > 255) prob_skip_false = 255;
  350. cpi->prob_skip_false = prob_skip_false;
  351. vp8_write_literal(w, prob_skip_false, 8);
  352. }
  353. vp8_write_literal(w, cpi->prob_intra_coded, 8);
  354. vp8_write_literal(w, cpi->prob_last_coded, 8);
  355. vp8_write_literal(w, cpi->prob_gf_coded, 8);
  356. update_mbintra_mode_probs(cpi);
  357. vp8_write_mvprobs(cpi);
  358. while (++mb_row < pc->mb_rows) {
  359. int mb_col = -1;
  360. while (++mb_col < pc->mb_cols) {
  361. const MB_MODE_INFO *const mi = &m->mbmi;
  362. const MV_REFERENCE_FRAME rf = mi->ref_frame;
  363. const MB_PREDICTION_MODE mode = mi->mode;
  364. MACROBLOCKD *xd = &cpi->mb.e_mbd;
  365. /* Distance of Mb to the various image edges.
  366. * These specified to 8th pel as they are always compared to MV
  367. * values that are in 1/8th pel units
  368. */
  369. xd->mb_to_left_edge = -((mb_col * 16) << 3);
  370. xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
  371. xd->mb_to_top_edge = -((mb_row * 16) << 3);
  372. xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
  373. #ifdef VP8_ENTROPY_STATS
  374. active_section = 9;
  375. #endif
  376. if (cpi->mb.e_mbd.update_mb_segmentation_map) {
  377. write_mb_features(w, mi, &cpi->mb.e_mbd);
  378. }
  379. if (pc->mb_no_coeff_skip) {
  380. vp8_encode_bool(w, m->mbmi.mb_skip_coeff, prob_skip_false);
  381. }
  382. if (rf == INTRA_FRAME) {
  383. vp8_write(w, 0, cpi->prob_intra_coded);
  384. #ifdef VP8_ENTROPY_STATS
  385. active_section = 6;
  386. #endif
  387. write_ymode(w, mode, pc->fc.ymode_prob);
  388. if (mode == B_PRED) {
  389. int j = 0;
  390. do {
  391. write_bmode(w, m->bmi[j].as_mode, pc->fc.bmode_prob);
  392. } while (++j < 16);
  393. }
  394. write_uv_mode(w, mi->uv_mode, pc->fc.uv_mode_prob);
  395. } else /* inter coded */
  396. {
  397. int_mv best_mv;
  398. vp8_prob mv_ref_p[VP8_MVREFS - 1];
  399. vp8_write(w, 1, cpi->prob_intra_coded);
  400. if (rf == LAST_FRAME)
  401. vp8_write(w, 0, cpi->prob_last_coded);
  402. else {
  403. vp8_write(w, 1, cpi->prob_last_coded);
  404. vp8_write(w, (rf == GOLDEN_FRAME) ? 0 : 1, cpi->prob_gf_coded);
  405. }
  406. {
  407. int_mv n1, n2;
  408. int ct[4];
  409. vp8_find_near_mvs(xd, m, &n1, &n2, &best_mv, ct, rf,
  410. cpi->common.ref_frame_sign_bias);
  411. vp8_clamp_mv2(&best_mv, xd);
  412. vp8_mv_ref_probs(mv_ref_p, ct);
  413. #ifdef VP8_ENTROPY_STATS
  414. accum_mv_refs(mode, ct);
  415. #endif
  416. }
  417. #ifdef VP8_ENTROPY_STATS
  418. active_section = 3;
  419. #endif
  420. write_mv_ref(w, mode, mv_ref_p);
  421. switch (mode) /* new, split require MVs */
  422. {
  423. case NEWMV:
  424. #ifdef VP8_ENTROPY_STATS
  425. active_section = 5;
  426. #endif
  427. write_mv(w, &mi->mv.as_mv, &best_mv, mvc);
  428. break;
  429. case SPLITMV: {
  430. int j = 0;
  431. #ifdef MODE_STATS
  432. ++count_mb_seg[mi->partitioning];
  433. #endif
  434. write_split(w, mi->partitioning);
  435. do {
  436. B_PREDICTION_MODE blockmode;
  437. int_mv blockmv;
  438. const int *const L = vp8_mbsplits[mi->partitioning];
  439. int k = -1; /* first block in subset j */
  440. int mv_contz;
  441. int_mv leftmv, abovemv;
  442. blockmode = cpi->mb.partition_info->bmi[j].mode;
  443. blockmv = cpi->mb.partition_info->bmi[j].mv;
  444. while (j != L[++k]) {
  445. assert(k < 16);
  446. }
  447. leftmv.as_int = left_block_mv(m, k);
  448. abovemv.as_int = above_block_mv(m, k, mis);
  449. mv_contz = vp8_mv_cont(&leftmv, &abovemv);
  450. write_sub_mv_ref(w, blockmode, vp8_sub_mv_ref_prob2[mv_contz]);
  451. if (blockmode == NEW4X4) {
  452. #ifdef VP8_ENTROPY_STATS
  453. active_section = 11;
  454. #endif
  455. write_mv(w, &blockmv.as_mv, &best_mv, (const MV_CONTEXT *)mvc);
  456. }
  457. } while (++j < cpi->mb.partition_info->count);
  458. break;
  459. }
  460. default: break;
  461. }
  462. }
  463. ++m;
  464. cpi->mb.partition_info++;
  465. }
  466. ++m; /* skip L prediction border */
  467. cpi->mb.partition_info++;
  468. }
  469. }
  470. static void write_kfmodes(VP8_COMP *cpi) {
  471. vp8_writer *const bc = cpi->bc;
  472. const VP8_COMMON *const c = &cpi->common;
  473. /* const */
  474. MODE_INFO *m = c->mi;
  475. int mb_row = -1;
  476. int prob_skip_false = 0;
  477. if (c->mb_no_coeff_skip) {
  478. int total_mbs = c->mb_rows * c->mb_cols;
  479. prob_skip_false = (total_mbs - cpi->mb.skip_true_count) * 256 / total_mbs;
  480. if (prob_skip_false <= 1) prob_skip_false = 1;
  481. if (prob_skip_false >= 255) prob_skip_false = 255;
  482. cpi->prob_skip_false = prob_skip_false;
  483. vp8_write_literal(bc, prob_skip_false, 8);
  484. }
  485. while (++mb_row < c->mb_rows) {
  486. int mb_col = -1;
  487. while (++mb_col < c->mb_cols) {
  488. const int ym = m->mbmi.mode;
  489. if (cpi->mb.e_mbd.update_mb_segmentation_map) {
  490. write_mb_features(bc, &m->mbmi, &cpi->mb.e_mbd);
  491. }
  492. if (c->mb_no_coeff_skip) {
  493. vp8_encode_bool(bc, m->mbmi.mb_skip_coeff, prob_skip_false);
  494. }
  495. kfwrite_ymode(bc, ym, vp8_kf_ymode_prob);
  496. if (ym == B_PRED) {
  497. const int mis = c->mode_info_stride;
  498. int i = 0;
  499. do {
  500. const B_PREDICTION_MODE A = above_block_mode(m, i, mis);
  501. const B_PREDICTION_MODE L = left_block_mode(m, i);
  502. const int bm = m->bmi[i].as_mode;
  503. #ifdef VP8_ENTROPY_STATS
  504. ++intra_mode_stats[A][L][bm];
  505. #endif
  506. write_bmode(bc, bm, vp8_kf_bmode_prob[A][L]);
  507. } while (++i < 16);
  508. }
  509. write_uv_mode(bc, (m++)->mbmi.uv_mode, vp8_kf_uv_mode_prob);
  510. }
  511. m++; /* skip L prediction border */
  512. }
  513. }
  514. #if 0
  515. /* This function is used for debugging probability trees. */
  516. static void print_prob_tree(vp8_prob
  517. coef_probs[BLOCK_TYPES][COEF_BANDS][PREV_COEF_CONTEXTS][ENTROPY_NODES])
  518. {
  519. /* print coef probability tree */
  520. int i,j,k,l;
  521. FILE* f = fopen("enc_tree_probs.txt", "a");
  522. fprintf(f, "{\n");
  523. for (i = 0; i < BLOCK_TYPES; ++i)
  524. {
  525. fprintf(f, " {\n");
  526. for (j = 0; j < COEF_BANDS; ++j)
  527. {
  528. fprintf(f, " {\n");
  529. for (k = 0; k < PREV_COEF_CONTEXTS; ++k)
  530. {
  531. fprintf(f, " {");
  532. for (l = 0; l < ENTROPY_NODES; ++l)
  533. {
  534. fprintf(f, "%3u, ",
  535. (unsigned int)(coef_probs [i][j][k][l]));
  536. }
  537. fprintf(f, " }\n");
  538. }
  539. fprintf(f, " }\n");
  540. }
  541. fprintf(f, " }\n");
  542. }
  543. fprintf(f, "}\n");
  544. fclose(f);
  545. }
  546. #endif
  547. static void sum_probs_over_prev_coef_context(
  548. const unsigned int probs[PREV_COEF_CONTEXTS][MAX_ENTROPY_TOKENS],
  549. unsigned int *out) {
  550. int i, j;
  551. for (i = 0; i < MAX_ENTROPY_TOKENS; ++i) {
  552. for (j = 0; j < PREV_COEF_CONTEXTS; ++j) {
  553. const unsigned int tmp = out[i];
  554. out[i] += probs[j][i];
  555. /* check for wrap */
  556. if (out[i] < tmp) out[i] = UINT_MAX;
  557. }
  558. }
  559. }
  560. static int prob_update_savings(const unsigned int *ct, const vp8_prob oldp,
  561. const vp8_prob newp, const vp8_prob upd) {
  562. const int old_b = vp8_cost_branch(ct, oldp);
  563. const int new_b = vp8_cost_branch(ct, newp);
  564. const int update_b = 8 + ((vp8_cost_one(upd) - vp8_cost_zero(upd)) >> 8);
  565. return old_b - new_b - update_b;
  566. }
  567. static int independent_coef_context_savings(VP8_COMP *cpi) {
  568. MACROBLOCK *const x = &cpi->mb;
  569. int savings = 0;
  570. int i = 0;
  571. do {
  572. int j = 0;
  573. do {
  574. int k = 0;
  575. unsigned int prev_coef_count_sum[MAX_ENTROPY_TOKENS] = { 0 };
  576. int prev_coef_savings[MAX_ENTROPY_TOKENS] = { 0 };
  577. const unsigned int(*probs)[MAX_ENTROPY_TOKENS];
  578. /* Calculate new probabilities given the constraint that
  579. * they must be equal over the prev coef contexts
  580. */
  581. probs = (const unsigned int(*)[MAX_ENTROPY_TOKENS])x->coef_counts[i][j];
  582. /* Reset to default probabilities at key frames */
  583. if (cpi->common.frame_type == KEY_FRAME) {
  584. probs = default_coef_counts[i][j];
  585. }
  586. sum_probs_over_prev_coef_context(probs, prev_coef_count_sum);
  587. do {
  588. /* at every context */
  589. /* calc probs and branch cts for this frame only */
  590. int t = 0; /* token/prob index */
  591. vp8_tree_probs_from_distribution(
  592. MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
  593. cpi->frame_coef_probs[i][j][k], cpi->frame_branch_ct[i][j][k],
  594. prev_coef_count_sum, 256, 1);
  595. do {
  596. const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
  597. const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
  598. const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
  599. const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  600. const int s = prob_update_savings(ct, oldp, newp, upd);
  601. if (cpi->common.frame_type != KEY_FRAME ||
  602. (cpi->common.frame_type == KEY_FRAME && newp != oldp)) {
  603. prev_coef_savings[t] += s;
  604. }
  605. } while (++t < ENTROPY_NODES);
  606. } while (++k < PREV_COEF_CONTEXTS);
  607. k = 0;
  608. do {
  609. /* We only update probabilities if we can save bits, except
  610. * for key frames where we have to update all probabilities
  611. * to get the equal probabilities across the prev coef
  612. * contexts.
  613. */
  614. if (prev_coef_savings[k] > 0 || cpi->common.frame_type == KEY_FRAME) {
  615. savings += prev_coef_savings[k];
  616. }
  617. } while (++k < ENTROPY_NODES);
  618. } while (++j < COEF_BANDS);
  619. } while (++i < BLOCK_TYPES);
  620. return savings;
  621. }
  622. static int default_coef_context_savings(VP8_COMP *cpi) {
  623. MACROBLOCK *const x = &cpi->mb;
  624. int savings = 0;
  625. int i = 0;
  626. do {
  627. int j = 0;
  628. do {
  629. int k = 0;
  630. do {
  631. /* at every context */
  632. /* calc probs and branch cts for this frame only */
  633. int t = 0; /* token/prob index */
  634. vp8_tree_probs_from_distribution(
  635. MAX_ENTROPY_TOKENS, vp8_coef_encodings, vp8_coef_tree,
  636. cpi->frame_coef_probs[i][j][k], cpi->frame_branch_ct[i][j][k],
  637. x->coef_counts[i][j][k], 256, 1);
  638. do {
  639. const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
  640. const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
  641. const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
  642. const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  643. const int s = prob_update_savings(ct, oldp, newp, upd);
  644. if (s > 0) {
  645. savings += s;
  646. }
  647. } while (++t < ENTROPY_NODES);
  648. } while (++k < PREV_COEF_CONTEXTS);
  649. } while (++j < COEF_BANDS);
  650. } while (++i < BLOCK_TYPES);
  651. return savings;
  652. }
  653. void vp8_calc_ref_frame_costs(int *ref_frame_cost, int prob_intra,
  654. int prob_last, int prob_garf) {
  655. assert(prob_intra >= 0);
  656. assert(prob_intra <= 255);
  657. assert(prob_last >= 0);
  658. assert(prob_last <= 255);
  659. assert(prob_garf >= 0);
  660. assert(prob_garf <= 255);
  661. ref_frame_cost[INTRA_FRAME] = vp8_cost_zero(prob_intra);
  662. ref_frame_cost[LAST_FRAME] =
  663. vp8_cost_one(prob_intra) + vp8_cost_zero(prob_last);
  664. ref_frame_cost[GOLDEN_FRAME] = vp8_cost_one(prob_intra) +
  665. vp8_cost_one(prob_last) +
  666. vp8_cost_zero(prob_garf);
  667. ref_frame_cost[ALTREF_FRAME] = vp8_cost_one(prob_intra) +
  668. vp8_cost_one(prob_last) +
  669. vp8_cost_one(prob_garf);
  670. }
  671. int vp8_estimate_entropy_savings(VP8_COMP *cpi) {
  672. int savings = 0;
  673. const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
  674. const int rf_intra = rfct[INTRA_FRAME];
  675. const int rf_inter =
  676. rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
  677. int new_intra, new_last, new_garf, oldtotal, newtotal;
  678. int ref_frame_cost[MAX_REF_FRAMES];
  679. vpx_clear_system_state();
  680. if (cpi->common.frame_type != KEY_FRAME) {
  681. if (!(new_intra = rf_intra * 255 / (rf_intra + rf_inter))) new_intra = 1;
  682. new_last = rf_inter ? (rfct[LAST_FRAME] * 255) / rf_inter : 128;
  683. new_garf = (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
  684. ? (rfct[GOLDEN_FRAME] * 255) /
  685. (rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME])
  686. : 128;
  687. vp8_calc_ref_frame_costs(ref_frame_cost, new_intra, new_last, new_garf);
  688. newtotal = rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
  689. rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
  690. rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
  691. rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
  692. /* old costs */
  693. vp8_calc_ref_frame_costs(ref_frame_cost, cpi->prob_intra_coded,
  694. cpi->prob_last_coded, cpi->prob_gf_coded);
  695. oldtotal = rfct[INTRA_FRAME] * ref_frame_cost[INTRA_FRAME] +
  696. rfct[LAST_FRAME] * ref_frame_cost[LAST_FRAME] +
  697. rfct[GOLDEN_FRAME] * ref_frame_cost[GOLDEN_FRAME] +
  698. rfct[ALTREF_FRAME] * ref_frame_cost[ALTREF_FRAME];
  699. savings += (oldtotal - newtotal) / 256;
  700. }
  701. if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
  702. savings += independent_coef_context_savings(cpi);
  703. } else {
  704. savings += default_coef_context_savings(cpi);
  705. }
  706. return savings;
  707. }
  708. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  709. int vp8_update_coef_context(VP8_COMP *cpi) {
  710. int savings = 0;
  711. if (cpi->common.frame_type == KEY_FRAME) {
  712. /* Reset to default counts/probabilities at key frames */
  713. vp8_copy(cpi->mb.coef_counts, default_coef_counts);
  714. }
  715. if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
  716. savings += independent_coef_context_savings(cpi);
  717. else
  718. savings += default_coef_context_savings(cpi);
  719. return savings;
  720. }
  721. #endif
  722. void vp8_update_coef_probs(VP8_COMP *cpi) {
  723. int i = 0;
  724. #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  725. vp8_writer *const w = cpi->bc;
  726. #endif
  727. int savings = 0;
  728. vpx_clear_system_state();
  729. do {
  730. int j = 0;
  731. do {
  732. int k = 0;
  733. int prev_coef_savings[ENTROPY_NODES] = { 0 };
  734. if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
  735. for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
  736. int t; /* token/prob index */
  737. for (t = 0; t < ENTROPY_NODES; ++t) {
  738. const unsigned int *ct = cpi->frame_branch_ct[i][j][k][t];
  739. const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
  740. const vp8_prob oldp = cpi->common.fc.coef_probs[i][j][k][t];
  741. const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  742. prev_coef_savings[t] += prob_update_savings(ct, oldp, newp, upd);
  743. }
  744. }
  745. k = 0;
  746. }
  747. do {
  748. /* note: use result from vp8_estimate_entropy_savings, so no
  749. * need to call vp8_tree_probs_from_distribution here.
  750. */
  751. /* at every context */
  752. /* calc probs and branch cts for this frame only */
  753. int t = 0; /* token/prob index */
  754. do {
  755. const vp8_prob newp = cpi->frame_coef_probs[i][j][k][t];
  756. vp8_prob *Pold = cpi->common.fc.coef_probs[i][j][k] + t;
  757. const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  758. int s = prev_coef_savings[t];
  759. int u = 0;
  760. if (!(cpi->oxcf.error_resilient_mode &
  761. VPX_ERROR_RESILIENT_PARTITIONS)) {
  762. s = prob_update_savings(cpi->frame_branch_ct[i][j][k][t], *Pold,
  763. newp, upd);
  764. }
  765. if (s > 0) u = 1;
  766. /* Force updates on key frames if the new is different,
  767. * so that we can be sure we end up with equal probabilities
  768. * over the prev coef contexts.
  769. */
  770. if ((cpi->oxcf.error_resilient_mode &
  771. VPX_ERROR_RESILIENT_PARTITIONS) &&
  772. cpi->common.frame_type == KEY_FRAME && newp != *Pold) {
  773. u = 1;
  774. }
  775. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  776. cpi->update_probs[i][j][k][t] = u;
  777. #else
  778. vp8_write(w, u, upd);
  779. #endif
  780. #ifdef VP8_ENTROPY_STATS
  781. ++tree_update_hist[i][j][k][t][u];
  782. #endif
  783. if (u) {
  784. /* send/use new probability */
  785. *Pold = newp;
  786. #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  787. vp8_write_literal(w, newp, 8);
  788. #endif
  789. savings += s;
  790. }
  791. } while (++t < ENTROPY_NODES);
  792. /* Accum token counts for generation of default statistics */
  793. #ifdef VP8_ENTROPY_STATS
  794. t = 0;
  795. do {
  796. context_counters[i][j][k][t] += cpi->coef_counts[i][j][k][t];
  797. } while (++t < MAX_ENTROPY_TOKENS);
  798. #endif
  799. } while (++k < PREV_COEF_CONTEXTS);
  800. } while (++j < COEF_BANDS);
  801. } while (++i < BLOCK_TYPES);
  802. }
  803. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  804. static void pack_coef_probs(VP8_COMP *cpi) {
  805. int i = 0;
  806. vp8_writer *const w = cpi->bc;
  807. do {
  808. int j = 0;
  809. do {
  810. int k = 0;
  811. do {
  812. int t = 0; /* token/prob index */
  813. do {
  814. const vp8_prob newp = cpi->common.fc.coef_probs[i][j][k][t];
  815. const vp8_prob upd = vp8_coef_update_probs[i][j][k][t];
  816. const char u = cpi->update_probs[i][j][k][t];
  817. vp8_write(w, u, upd);
  818. if (u) {
  819. /* send/use new probability */
  820. vp8_write_literal(w, newp, 8);
  821. }
  822. } while (++t < ENTROPY_NODES);
  823. } while (++k < PREV_COEF_CONTEXTS);
  824. } while (++j < COEF_BANDS);
  825. } while (++i < BLOCK_TYPES);
  826. }
  827. #endif
  828. #ifdef PACKET_TESTING
  829. FILE *vpxlogc = 0;
  830. #endif
  831. static void put_delta_q(vp8_writer *bc, int delta_q) {
  832. if (delta_q != 0) {
  833. vp8_write_bit(bc, 1);
  834. vp8_write_literal(bc, abs(delta_q), 4);
  835. if (delta_q < 0)
  836. vp8_write_bit(bc, 1);
  837. else
  838. vp8_write_bit(bc, 0);
  839. } else
  840. vp8_write_bit(bc, 0);
  841. }
  842. void vp8_pack_bitstream(VP8_COMP *cpi, unsigned char *dest,
  843. unsigned char *dest_end, size_t *size) {
  844. int i, j;
  845. VP8_HEADER oh;
  846. VP8_COMMON *const pc = &cpi->common;
  847. vp8_writer *const bc = cpi->bc;
  848. MACROBLOCKD *const xd = &cpi->mb.e_mbd;
  849. int extra_bytes_packed = 0;
  850. unsigned char *cx_data = dest;
  851. unsigned char *cx_data_end = dest_end;
  852. const int *mb_feature_data_bits;
  853. oh.show_frame = (int)pc->show_frame;
  854. oh.type = (int)pc->frame_type;
  855. oh.version = pc->version;
  856. oh.first_partition_length_in_bytes = 0;
  857. mb_feature_data_bits = vp8_mb_feature_data_bits;
  858. bc[0].error = &pc->error;
  859. validate_buffer(cx_data, 3, cx_data_end, &cpi->common.error);
  860. cx_data += 3;
  861. #if defined(SECTIONBITS_OUTPUT)
  862. Sectionbits[active_section = 1] += sizeof(VP8_HEADER) * 8 * 256;
  863. #endif
  864. /* every keyframe send startcode, width, height, scale factor, clamp
  865. * and color type
  866. */
  867. if (oh.type == KEY_FRAME) {
  868. int v;
  869. validate_buffer(cx_data, 7, cx_data_end, &cpi->common.error);
  870. /* Start / synch code */
  871. cx_data[0] = 0x9D;
  872. cx_data[1] = 0x01;
  873. cx_data[2] = 0x2a;
  874. v = (pc->horiz_scale << 14) | pc->Width;
  875. cx_data[3] = v;
  876. cx_data[4] = v >> 8;
  877. v = (pc->vert_scale << 14) | pc->Height;
  878. cx_data[5] = v;
  879. cx_data[6] = v >> 8;
  880. extra_bytes_packed = 7;
  881. cx_data += extra_bytes_packed;
  882. vp8_start_encode(bc, cx_data, cx_data_end);
  883. /* signal clr type */
  884. vp8_write_bit(bc, 0);
  885. vp8_write_bit(bc, pc->clamp_type);
  886. } else {
  887. vp8_start_encode(bc, cx_data, cx_data_end);
  888. }
  889. /* Signal whether or not Segmentation is enabled */
  890. vp8_write_bit(bc, xd->segmentation_enabled);
  891. /* Indicate which features are enabled */
  892. if (xd->segmentation_enabled) {
  893. /* Signal whether or not the segmentation map is being updated. */
  894. vp8_write_bit(bc, xd->update_mb_segmentation_map);
  895. vp8_write_bit(bc, xd->update_mb_segmentation_data);
  896. if (xd->update_mb_segmentation_data) {
  897. signed char Data;
  898. vp8_write_bit(bc, xd->mb_segement_abs_delta);
  899. /* For each segmentation feature (Quant and loop filter level) */
  900. for (i = 0; i < MB_LVL_MAX; ++i) {
  901. /* For each of the segments */
  902. for (j = 0; j < MAX_MB_SEGMENTS; ++j) {
  903. Data = xd->segment_feature_data[i][j];
  904. /* Frame level data */
  905. if (Data) {
  906. vp8_write_bit(bc, 1);
  907. if (Data < 0) {
  908. Data = -Data;
  909. vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
  910. vp8_write_bit(bc, 1);
  911. } else {
  912. vp8_write_literal(bc, Data, mb_feature_data_bits[i]);
  913. vp8_write_bit(bc, 0);
  914. }
  915. } else
  916. vp8_write_bit(bc, 0);
  917. }
  918. }
  919. }
  920. if (xd->update_mb_segmentation_map) {
  921. /* Write the probs used to decode the segment id for each mb */
  922. for (i = 0; i < MB_FEATURE_TREE_PROBS; ++i) {
  923. int Data = xd->mb_segment_tree_probs[i];
  924. if (Data != 255) {
  925. vp8_write_bit(bc, 1);
  926. vp8_write_literal(bc, Data, 8);
  927. } else
  928. vp8_write_bit(bc, 0);
  929. }
  930. }
  931. }
  932. vp8_write_bit(bc, pc->filter_type);
  933. vp8_write_literal(bc, pc->filter_level, 6);
  934. vp8_write_literal(bc, pc->sharpness_level, 3);
  935. /* Write out loop filter deltas applied at the MB level based on mode
  936. * or ref frame (if they are enabled).
  937. */
  938. vp8_write_bit(bc, xd->mode_ref_lf_delta_enabled);
  939. if (xd->mode_ref_lf_delta_enabled) {
  940. /* Do the deltas need to be updated */
  941. int send_update =
  942. xd->mode_ref_lf_delta_update || cpi->oxcf.error_resilient_mode;
  943. vp8_write_bit(bc, send_update);
  944. if (send_update) {
  945. int Data;
  946. /* Send update */
  947. for (i = 0; i < MAX_REF_LF_DELTAS; ++i) {
  948. Data = xd->ref_lf_deltas[i];
  949. /* Frame level data */
  950. if (xd->ref_lf_deltas[i] != xd->last_ref_lf_deltas[i] ||
  951. cpi->oxcf.error_resilient_mode) {
  952. xd->last_ref_lf_deltas[i] = xd->ref_lf_deltas[i];
  953. vp8_write_bit(bc, 1);
  954. if (Data > 0) {
  955. vp8_write_literal(bc, (Data & 0x3F), 6);
  956. vp8_write_bit(bc, 0); /* sign */
  957. } else {
  958. Data = -Data;
  959. vp8_write_literal(bc, (Data & 0x3F), 6);
  960. vp8_write_bit(bc, 1); /* sign */
  961. }
  962. } else
  963. vp8_write_bit(bc, 0);
  964. }
  965. /* Send update */
  966. for (i = 0; i < MAX_MODE_LF_DELTAS; ++i) {
  967. Data = xd->mode_lf_deltas[i];
  968. if (xd->mode_lf_deltas[i] != xd->last_mode_lf_deltas[i] ||
  969. cpi->oxcf.error_resilient_mode) {
  970. xd->last_mode_lf_deltas[i] = xd->mode_lf_deltas[i];
  971. vp8_write_bit(bc, 1);
  972. if (Data > 0) {
  973. vp8_write_literal(bc, (Data & 0x3F), 6);
  974. vp8_write_bit(bc, 0); /* sign */
  975. } else {
  976. Data = -Data;
  977. vp8_write_literal(bc, (Data & 0x3F), 6);
  978. vp8_write_bit(bc, 1); /* sign */
  979. }
  980. } else
  981. vp8_write_bit(bc, 0);
  982. }
  983. }
  984. }
  985. /* signal here is multi token partition is enabled */
  986. vp8_write_literal(bc, pc->multi_token_partition, 2);
  987. /* Frame Qbaseline quantizer index */
  988. vp8_write_literal(bc, pc->base_qindex, 7);
  989. /* Transmit Dc, Second order and Uv quantizer delta information */
  990. put_delta_q(bc, pc->y1dc_delta_q);
  991. put_delta_q(bc, pc->y2dc_delta_q);
  992. put_delta_q(bc, pc->y2ac_delta_q);
  993. put_delta_q(bc, pc->uvdc_delta_q);
  994. put_delta_q(bc, pc->uvac_delta_q);
  995. /* When there is a key frame all reference buffers are updated using
  996. * the new key frame
  997. */
  998. if (pc->frame_type != KEY_FRAME) {
  999. /* Should the GF or ARF be updated using the transmitted frame
  1000. * or buffer
  1001. */
  1002. vp8_write_bit(bc, pc->refresh_golden_frame);
  1003. vp8_write_bit(bc, pc->refresh_alt_ref_frame);
  1004. /* If not being updated from current frame should either GF or ARF
  1005. * be updated from another buffer
  1006. */
  1007. if (!pc->refresh_golden_frame)
  1008. vp8_write_literal(bc, pc->copy_buffer_to_gf, 2);
  1009. if (!pc->refresh_alt_ref_frame)
  1010. vp8_write_literal(bc, pc->copy_buffer_to_arf, 2);
  1011. /* Indicate reference frame sign bias for Golden and ARF frames
  1012. * (always 0 for last frame buffer)
  1013. */
  1014. vp8_write_bit(bc, pc->ref_frame_sign_bias[GOLDEN_FRAME]);
  1015. vp8_write_bit(bc, pc->ref_frame_sign_bias[ALTREF_FRAME]);
  1016. }
  1017. #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
  1018. if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) {
  1019. if (pc->frame_type == KEY_FRAME) {
  1020. pc->refresh_entropy_probs = 1;
  1021. } else {
  1022. pc->refresh_entropy_probs = 0;
  1023. }
  1024. }
  1025. #endif
  1026. vp8_write_bit(bc, pc->refresh_entropy_probs);
  1027. if (pc->frame_type != KEY_FRAME) vp8_write_bit(bc, pc->refresh_last_frame);
  1028. #ifdef VP8_ENTROPY_STATS
  1029. if (pc->frame_type == INTER_FRAME)
  1030. active_section = 0;
  1031. else
  1032. active_section = 7;
  1033. #endif
  1034. vpx_clear_system_state();
  1035. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  1036. pack_coef_probs(cpi);
  1037. #else
  1038. if (pc->refresh_entropy_probs == 0) {
  1039. /* save a copy for later refresh */
  1040. memcpy(&cpi->common.lfc, &cpi->common.fc, sizeof(cpi->common.fc));
  1041. }
  1042. vp8_update_coef_probs(cpi);
  1043. #endif
  1044. #ifdef VP8_ENTROPY_STATS
  1045. active_section = 2;
  1046. #endif
  1047. /* Write out the mb_no_coeff_skip flag */
  1048. vp8_write_bit(bc, pc->mb_no_coeff_skip);
  1049. if (pc->frame_type == KEY_FRAME) {
  1050. write_kfmodes(cpi);
  1051. #ifdef VP8_ENTROPY_STATS
  1052. active_section = 8;
  1053. #endif
  1054. } else {
  1055. pack_inter_mode_mvs(cpi);
  1056. #ifdef VP8_ENTROPY_STATS
  1057. active_section = 1;
  1058. #endif
  1059. }
  1060. vp8_stop_encode(bc);
  1061. cx_data += bc->pos;
  1062. oh.first_partition_length_in_bytes = cpi->bc->pos;
  1063. /* update frame tag */
  1064. {
  1065. int v = (oh.first_partition_length_in_bytes << 5) | (oh.show_frame << 4) |
  1066. (oh.version << 1) | oh.type;
  1067. dest[0] = v;
  1068. dest[1] = v >> 8;
  1069. dest[2] = v >> 16;
  1070. }
  1071. *size = VP8_HEADER_SIZE + extra_bytes_packed + cpi->bc->pos;
  1072. cpi->partition_sz[0] = (unsigned int)*size;
  1073. #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
  1074. {
  1075. const int num_part = (1 << pc->multi_token_partition);
  1076. unsigned char *dp = cpi->partition_d[0] + cpi->partition_sz[0];
  1077. if (num_part > 1) {
  1078. /* write token part sizes (all but last) if more than 1 */
  1079. validate_buffer(dp, 3 * (num_part - 1), cpi->partition_d_end[0],
  1080. &pc->error);
  1081. cpi->partition_sz[0] += 3 * (num_part - 1);
  1082. for (i = 1; i < num_part; ++i) {
  1083. write_partition_size(dp, cpi->partition_sz[i]);
  1084. dp += 3;
  1085. }
  1086. }
  1087. if (!cpi->output_partition) {
  1088. /* concatenate partition buffers */
  1089. for (i = 0; i < num_part; ++i) {
  1090. memmove(dp, cpi->partition_d[i + 1], cpi->partition_sz[i + 1]);
  1091. cpi->partition_d[i + 1] = dp;
  1092. dp += cpi->partition_sz[i + 1];
  1093. }
  1094. }
  1095. /* update total size */
  1096. *size = 0;
  1097. for (i = 0; i < num_part + 1; ++i) {
  1098. *size += cpi->partition_sz[i];
  1099. }
  1100. }
  1101. #else
  1102. if (pc->multi_token_partition != ONE_PARTITION) {
  1103. int num_part = 1 << pc->multi_token_partition;
  1104. /* partition size table at the end of first partition */
  1105. cpi->partition_sz[0] += 3 * (num_part - 1);
  1106. *size += 3 * (num_part - 1);
  1107. validate_buffer(cx_data, 3 * (num_part - 1), cx_data_end, &pc->error);
  1108. for (i = 1; i < num_part + 1; ++i) {
  1109. cpi->bc[i].error = &pc->error;
  1110. }
  1111. pack_tokens_into_partitions(cpi, cx_data + 3 * (num_part - 1), cx_data_end,
  1112. num_part);
  1113. for (i = 1; i < num_part; ++i) {
  1114. cpi->partition_sz[i] = cpi->bc[i].pos;
  1115. write_partition_size(cx_data, cpi->partition_sz[i]);
  1116. cx_data += 3;
  1117. *size += cpi->partition_sz[i]; /* add to total */
  1118. }
  1119. /* add last partition to total size */
  1120. cpi->partition_sz[i] = cpi->bc[i].pos;
  1121. *size += cpi->partition_sz[i];
  1122. } else {
  1123. bc[1].error = &pc->error;
  1124. vp8_start_encode(&cpi->bc[1], cx_data, cx_data_end);
  1125. #if CONFIG_MULTITHREAD
  1126. if (vpx_atomic_load_acquire(&cpi->b_multi_threaded)) {
  1127. pack_mb_row_tokens(cpi, &cpi->bc[1]);
  1128. } else {
  1129. vp8_pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
  1130. }
  1131. #else
  1132. vp8_pack_tokens(&cpi->bc[1], cpi->tok, cpi->tok_count);
  1133. #endif // CONFIG_MULTITHREAD
  1134. vp8_stop_encode(&cpi->bc[1]);
  1135. *size += cpi->bc[1].pos;
  1136. cpi->partition_sz[1] = cpi->bc[1].pos;
  1137. }
  1138. #endif
  1139. }
  1140. #ifdef VP8_ENTROPY_STATS
  1141. void print_tree_update_probs() {
  1142. int i, j, k, l;
  1143. FILE *f = fopen("context.c", "a");
  1144. int Sum;
  1145. fprintf(f, "\n/* Update probabilities for token entropy tree. */\n\n");
  1146. fprintf(f,
  1147. "const vp8_prob tree_update_probs[BLOCK_TYPES] [COEF_BANDS] "
  1148. "[PREV_COEF_CONTEXTS] [ENTROPY_NODES] = {\n");
  1149. for (i = 0; i < BLOCK_TYPES; ++i) {
  1150. fprintf(f, " { \n");
  1151. for (j = 0; j < COEF_BANDS; ++j) {
  1152. fprintf(f, " {\n");
  1153. for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
  1154. fprintf(f, " {");
  1155. for (l = 0; l < ENTROPY_NODES; ++l) {
  1156. Sum =
  1157. tree_update_hist[i][j][k][l][0] + tree_update_hist[i][j][k][l][1];
  1158. if (Sum > 0) {
  1159. if (((tree_update_hist[i][j][k][l][0] * 255) / Sum) > 0)
  1160. fprintf(f, "%3ld, ",
  1161. (tree_update_hist[i][j][k][l][0] * 255) / Sum);
  1162. else
  1163. fprintf(f, "%3ld, ", 1);
  1164. } else
  1165. fprintf(f, "%3ld, ", 128);
  1166. }
  1167. fprintf(f, "},\n");
  1168. }
  1169. fprintf(f, " },\n");
  1170. }
  1171. fprintf(f, " },\n");
  1172. }
  1173. fprintf(f, "};\n");
  1174. fclose(f);
  1175. }
  1176. #endif