vp9_bitstream.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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 <assert.h>
  11. #include <stdio.h>
  12. #include <limits.h>
  13. #include "vpx/vpx_encoder.h"
  14. #include "vpx_mem/vpx_mem.h"
  15. #include "vpx_ports/mem_ops.h"
  16. #include "vp9/common/vp9_entropy.h"
  17. #include "vp9/common/vp9_entropymode.h"
  18. #include "vp9/common/vp9_entropymv.h"
  19. #include "vp9/common/vp9_mvref_common.h"
  20. #include "vp9/common/vp9_pred_common.h"
  21. #include "vp9/common/vp9_seg_common.h"
  22. #include "vp9/common/vp9_systemdependent.h"
  23. #include "vp9/common/vp9_tile_common.h"
  24. #include "vp9/encoder/vp9_cost.h"
  25. #include "vp9/encoder/vp9_bitstream.h"
  26. #include "vp9/encoder/vp9_encodemv.h"
  27. #include "vp9/encoder/vp9_mcomp.h"
  28. #include "vp9/encoder/vp9_segmentation.h"
  29. #include "vp9/encoder/vp9_subexp.h"
  30. #include "vp9/encoder/vp9_tokenize.h"
  31. #include "vp9/encoder/vp9_write_bit_buffer.h"
  32. static const struct vp9_token intra_mode_encodings[INTRA_MODES] = {
  33. {0, 1}, {6, 3}, {28, 5}, {30, 5}, {58, 6}, {59, 6}, {126, 7}, {127, 7},
  34. {62, 6}, {2, 2}};
  35. static const struct vp9_token switchable_interp_encodings[SWITCHABLE_FILTERS] =
  36. {{0, 1}, {2, 2}, {3, 2}};
  37. static const struct vp9_token partition_encodings[PARTITION_TYPES] =
  38. {{0, 1}, {2, 2}, {6, 3}, {7, 3}};
  39. static const struct vp9_token inter_mode_encodings[INTER_MODES] =
  40. {{2, 2}, {6, 3}, {0, 1}, {7, 3}};
  41. static void write_intra_mode(vp9_writer *w, PREDICTION_MODE mode,
  42. const vp9_prob *probs) {
  43. vp9_write_token(w, vp9_intra_mode_tree, probs, &intra_mode_encodings[mode]);
  44. }
  45. static void write_inter_mode(vp9_writer *w, PREDICTION_MODE mode,
  46. const vp9_prob *probs) {
  47. assert(is_inter_mode(mode));
  48. vp9_write_token(w, vp9_inter_mode_tree, probs,
  49. &inter_mode_encodings[INTER_OFFSET(mode)]);
  50. }
  51. static void encode_unsigned_max(struct vp9_write_bit_buffer *wb,
  52. int data, int max) {
  53. vp9_wb_write_literal(wb, data, get_unsigned_bits(max));
  54. }
  55. static void prob_diff_update(const vp9_tree_index *tree,
  56. vp9_prob probs[/*n - 1*/],
  57. const unsigned int counts[/*n - 1*/],
  58. int n, vp9_writer *w) {
  59. int i;
  60. unsigned int branch_ct[32][2];
  61. // Assuming max number of probabilities <= 32
  62. assert(n <= 32);
  63. vp9_tree_probs_from_distribution(tree, branch_ct, counts);
  64. for (i = 0; i < n - 1; ++i)
  65. vp9_cond_prob_diff_update(w, &probs[i], branch_ct[i]);
  66. }
  67. static void write_selected_tx_size(const VP9_COMMON *cm,
  68. const MACROBLOCKD *xd, vp9_writer *w) {
  69. TX_SIZE tx_size = xd->mi[0]->mbmi.tx_size;
  70. BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
  71. const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
  72. const vp9_prob *const tx_probs = get_tx_probs2(max_tx_size, xd,
  73. &cm->fc->tx_probs);
  74. vp9_write(w, tx_size != TX_4X4, tx_probs[0]);
  75. if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
  76. vp9_write(w, tx_size != TX_8X8, tx_probs[1]);
  77. if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
  78. vp9_write(w, tx_size != TX_16X16, tx_probs[2]);
  79. }
  80. }
  81. static int write_skip(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  82. int segment_id, const MODE_INFO *mi, vp9_writer *w) {
  83. if (vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
  84. return 1;
  85. } else {
  86. const int skip = mi->mbmi.skip;
  87. vp9_write(w, skip, vp9_get_skip_prob(cm, xd));
  88. return skip;
  89. }
  90. }
  91. static void update_skip_probs(VP9_COMMON *cm, vp9_writer *w,
  92. FRAME_COUNTS *counts) {
  93. int k;
  94. for (k = 0; k < SKIP_CONTEXTS; ++k)
  95. vp9_cond_prob_diff_update(w, &cm->fc->skip_probs[k], counts->skip[k]);
  96. }
  97. static void update_switchable_interp_probs(VP9_COMMON *cm, vp9_writer *w,
  98. FRAME_COUNTS *counts) {
  99. int j;
  100. for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
  101. prob_diff_update(vp9_switchable_interp_tree,
  102. cm->fc->switchable_interp_prob[j],
  103. counts->switchable_interp[j], SWITCHABLE_FILTERS, w);
  104. }
  105. static void pack_mb_tokens(vp9_writer *w,
  106. TOKENEXTRA **tp, const TOKENEXTRA *const stop,
  107. vpx_bit_depth_t bit_depth) {
  108. TOKENEXTRA *p = *tp;
  109. while (p < stop && p->token != EOSB_TOKEN) {
  110. const int t = p->token;
  111. const struct vp9_token *const a = &vp9_coef_encodings[t];
  112. int i = 0;
  113. int v = a->value;
  114. int n = a->len;
  115. #if CONFIG_VP9_HIGHBITDEPTH
  116. const vp9_extra_bit *b;
  117. if (bit_depth == VPX_BITS_12)
  118. b = &vp9_extra_bits_high12[t];
  119. else if (bit_depth == VPX_BITS_10)
  120. b = &vp9_extra_bits_high10[t];
  121. else
  122. b = &vp9_extra_bits[t];
  123. #else
  124. const vp9_extra_bit *const b = &vp9_extra_bits[t];
  125. (void) bit_depth;
  126. #endif // CONFIG_VP9_HIGHBITDEPTH
  127. /* skip one or two nodes */
  128. if (p->skip_eob_node) {
  129. n -= p->skip_eob_node;
  130. i = 2 * p->skip_eob_node;
  131. }
  132. // TODO(jbb): expanding this can lead to big gains. It allows
  133. // much better branch prediction and would enable us to avoid numerous
  134. // lookups and compares.
  135. // If we have a token that's in the constrained set, the coefficient tree
  136. // is split into two treed writes. The first treed write takes care of the
  137. // unconstrained nodes. The second treed write takes care of the
  138. // constrained nodes.
  139. if (t >= TWO_TOKEN && t < EOB_TOKEN) {
  140. int len = UNCONSTRAINED_NODES - p->skip_eob_node;
  141. int bits = v >> (n - len);
  142. vp9_write_tree(w, vp9_coef_tree, p->context_tree, bits, len, i);
  143. vp9_write_tree(w, vp9_coef_con_tree,
  144. vp9_pareto8_full[p->context_tree[PIVOT_NODE] - 1],
  145. v, n - len, 0);
  146. } else {
  147. vp9_write_tree(w, vp9_coef_tree, p->context_tree, v, n, i);
  148. }
  149. if (b->base_val) {
  150. const int e = p->extra, l = b->len;
  151. if (l) {
  152. const unsigned char *pb = b->prob;
  153. int v = e >> 1;
  154. int n = l; /* number of bits in v, assumed nonzero */
  155. int i = 0;
  156. do {
  157. const int bb = (v >> --n) & 1;
  158. vp9_write(w, bb, pb[i >> 1]);
  159. i = b->tree[i + bb];
  160. } while (n);
  161. }
  162. vp9_write_bit(w, e & 1);
  163. }
  164. ++p;
  165. }
  166. *tp = p + (p->token == EOSB_TOKEN);
  167. }
  168. static void write_segment_id(vp9_writer *w, const struct segmentation *seg,
  169. int segment_id) {
  170. if (seg->enabled && seg->update_map)
  171. vp9_write_tree(w, vp9_segment_tree, seg->tree_probs, segment_id, 3, 0);
  172. }
  173. // This function encodes the reference frame
  174. static void write_ref_frames(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  175. vp9_writer *w) {
  176. const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  177. const int is_compound = has_second_ref(mbmi);
  178. const int segment_id = mbmi->segment_id;
  179. // If segment level coding of this signal is disabled...
  180. // or the segment allows multiple reference frame options
  181. if (vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
  182. assert(!is_compound);
  183. assert(mbmi->ref_frame[0] ==
  184. vp9_get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME));
  185. } else {
  186. // does the feature use compound prediction or not
  187. // (if not specified at the frame/segment level)
  188. if (cm->reference_mode == REFERENCE_MODE_SELECT) {
  189. vp9_write(w, is_compound, vp9_get_reference_mode_prob(cm, xd));
  190. } else {
  191. assert(!is_compound == (cm->reference_mode == SINGLE_REFERENCE));
  192. }
  193. if (is_compound) {
  194. vp9_write(w, mbmi->ref_frame[0] == GOLDEN_FRAME,
  195. vp9_get_pred_prob_comp_ref_p(cm, xd));
  196. } else {
  197. const int bit0 = mbmi->ref_frame[0] != LAST_FRAME;
  198. vp9_write(w, bit0, vp9_get_pred_prob_single_ref_p1(cm, xd));
  199. if (bit0) {
  200. const int bit1 = mbmi->ref_frame[0] != GOLDEN_FRAME;
  201. vp9_write(w, bit1, vp9_get_pred_prob_single_ref_p2(cm, xd));
  202. }
  203. }
  204. }
  205. }
  206. static void pack_inter_mode_mvs(VP9_COMP *cpi, const MODE_INFO *mi,
  207. vp9_writer *w) {
  208. VP9_COMMON *const cm = &cpi->common;
  209. const nmv_context *nmvc = &cm->fc->nmvc;
  210. const MACROBLOCK *const x = &cpi->td.mb;
  211. const MACROBLOCKD *const xd = &x->e_mbd;
  212. const struct segmentation *const seg = &cm->seg;
  213. const MB_MODE_INFO *const mbmi = &mi->mbmi;
  214. const PREDICTION_MODE mode = mbmi->mode;
  215. const int segment_id = mbmi->segment_id;
  216. const BLOCK_SIZE bsize = mbmi->sb_type;
  217. const int allow_hp = cm->allow_high_precision_mv;
  218. const int is_inter = is_inter_block(mbmi);
  219. const int is_compound = has_second_ref(mbmi);
  220. int skip, ref;
  221. if (seg->update_map) {
  222. if (seg->temporal_update) {
  223. const int pred_flag = mbmi->seg_id_predicted;
  224. vp9_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
  225. vp9_write(w, pred_flag, pred_prob);
  226. if (!pred_flag)
  227. write_segment_id(w, seg, segment_id);
  228. } else {
  229. write_segment_id(w, seg, segment_id);
  230. }
  231. }
  232. skip = write_skip(cm, xd, segment_id, mi, w);
  233. if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_REF_FRAME))
  234. vp9_write(w, is_inter, vp9_get_intra_inter_prob(cm, xd));
  235. if (bsize >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT &&
  236. !(is_inter && skip)) {
  237. write_selected_tx_size(cm, xd, w);
  238. }
  239. if (!is_inter) {
  240. if (bsize >= BLOCK_8X8) {
  241. write_intra_mode(w, mode, cm->fc->y_mode_prob[size_group_lookup[bsize]]);
  242. } else {
  243. int idx, idy;
  244. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
  245. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
  246. for (idy = 0; idy < 2; idy += num_4x4_h) {
  247. for (idx = 0; idx < 2; idx += num_4x4_w) {
  248. const PREDICTION_MODE b_mode = mi->bmi[idy * 2 + idx].as_mode;
  249. write_intra_mode(w, b_mode, cm->fc->y_mode_prob[0]);
  250. }
  251. }
  252. }
  253. write_intra_mode(w, mbmi->uv_mode, cm->fc->uv_mode_prob[mode]);
  254. } else {
  255. const int mode_ctx = mbmi->mode_context[mbmi->ref_frame[0]];
  256. const vp9_prob *const inter_probs = cm->fc->inter_mode_probs[mode_ctx];
  257. write_ref_frames(cm, xd, w);
  258. // If segment skip is not enabled code the mode.
  259. if (!vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP)) {
  260. if (bsize >= BLOCK_8X8) {
  261. write_inter_mode(w, mode, inter_probs);
  262. }
  263. }
  264. if (cm->interp_filter == SWITCHABLE) {
  265. const int ctx = vp9_get_pred_context_switchable_interp(xd);
  266. vp9_write_token(w, vp9_switchable_interp_tree,
  267. cm->fc->switchable_interp_prob[ctx],
  268. &switchable_interp_encodings[mbmi->interp_filter]);
  269. ++cpi->interp_filter_selected[0][mbmi->interp_filter];
  270. } else {
  271. assert(mbmi->interp_filter == cm->interp_filter);
  272. }
  273. if (bsize < BLOCK_8X8) {
  274. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
  275. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
  276. int idx, idy;
  277. for (idy = 0; idy < 2; idy += num_4x4_h) {
  278. for (idx = 0; idx < 2; idx += num_4x4_w) {
  279. const int j = idy * 2 + idx;
  280. const PREDICTION_MODE b_mode = mi->bmi[j].as_mode;
  281. write_inter_mode(w, b_mode, inter_probs);
  282. if (b_mode == NEWMV) {
  283. for (ref = 0; ref < 1 + is_compound; ++ref)
  284. vp9_encode_mv(cpi, w, &mi->bmi[j].as_mv[ref].as_mv,
  285. &mbmi->ref_mvs[mbmi->ref_frame[ref]][0].as_mv,
  286. nmvc, allow_hp);
  287. }
  288. }
  289. }
  290. } else {
  291. if (mode == NEWMV) {
  292. for (ref = 0; ref < 1 + is_compound; ++ref)
  293. vp9_encode_mv(cpi, w, &mbmi->mv[ref].as_mv,
  294. &mbmi->ref_mvs[mbmi->ref_frame[ref]][0].as_mv, nmvc,
  295. allow_hp);
  296. }
  297. }
  298. }
  299. }
  300. static void write_mb_modes_kf(const VP9_COMMON *cm, const MACROBLOCKD *xd,
  301. MODE_INFO **mi_8x8, vp9_writer *w) {
  302. const struct segmentation *const seg = &cm->seg;
  303. const MODE_INFO *const mi = mi_8x8[0];
  304. const MODE_INFO *const above_mi = xd->above_mi;
  305. const MODE_INFO *const left_mi = xd->left_mi;
  306. const MB_MODE_INFO *const mbmi = &mi->mbmi;
  307. const BLOCK_SIZE bsize = mbmi->sb_type;
  308. if (seg->update_map)
  309. write_segment_id(w, seg, mbmi->segment_id);
  310. write_skip(cm, xd, mbmi->segment_id, mi, w);
  311. if (bsize >= BLOCK_8X8 && cm->tx_mode == TX_MODE_SELECT)
  312. write_selected_tx_size(cm, xd, w);
  313. if (bsize >= BLOCK_8X8) {
  314. write_intra_mode(w, mbmi->mode, get_y_mode_probs(mi, above_mi, left_mi, 0));
  315. } else {
  316. const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];
  317. const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];
  318. int idx, idy;
  319. for (idy = 0; idy < 2; idy += num_4x4_h) {
  320. for (idx = 0; idx < 2; idx += num_4x4_w) {
  321. const int block = idy * 2 + idx;
  322. write_intra_mode(w, mi->bmi[block].as_mode,
  323. get_y_mode_probs(mi, above_mi, left_mi, block));
  324. }
  325. }
  326. }
  327. write_intra_mode(w, mbmi->uv_mode, vp9_kf_uv_mode_prob[mbmi->mode]);
  328. }
  329. static void write_modes_b(VP9_COMP *cpi, const TileInfo *const tile,
  330. vp9_writer *w, TOKENEXTRA **tok,
  331. const TOKENEXTRA *const tok_end,
  332. int mi_row, int mi_col) {
  333. const VP9_COMMON *const cm = &cpi->common;
  334. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  335. MODE_INFO *m;
  336. xd->mi = cm->mi_grid_visible + (mi_row * cm->mi_stride + mi_col);
  337. m = xd->mi[0];
  338. set_mi_row_col(xd, tile,
  339. mi_row, num_8x8_blocks_high_lookup[m->mbmi.sb_type],
  340. mi_col, num_8x8_blocks_wide_lookup[m->mbmi.sb_type],
  341. cm->mi_rows, cm->mi_cols);
  342. if (frame_is_intra_only(cm)) {
  343. write_mb_modes_kf(cm, xd, xd->mi, w);
  344. } else {
  345. pack_inter_mode_mvs(cpi, m, w);
  346. }
  347. assert(*tok < tok_end);
  348. pack_mb_tokens(w, tok, tok_end, cm->bit_depth);
  349. }
  350. static void write_partition(const VP9_COMMON *const cm,
  351. const MACROBLOCKD *const xd,
  352. int hbs, int mi_row, int mi_col,
  353. PARTITION_TYPE p, BLOCK_SIZE bsize, vp9_writer *w) {
  354. const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
  355. const vp9_prob *const probs = get_partition_probs(cm, ctx);
  356. const int has_rows = (mi_row + hbs) < cm->mi_rows;
  357. const int has_cols = (mi_col + hbs) < cm->mi_cols;
  358. if (has_rows && has_cols) {
  359. vp9_write_token(w, vp9_partition_tree, probs, &partition_encodings[p]);
  360. } else if (!has_rows && has_cols) {
  361. assert(p == PARTITION_SPLIT || p == PARTITION_HORZ);
  362. vp9_write(w, p == PARTITION_SPLIT, probs[1]);
  363. } else if (has_rows && !has_cols) {
  364. assert(p == PARTITION_SPLIT || p == PARTITION_VERT);
  365. vp9_write(w, p == PARTITION_SPLIT, probs[2]);
  366. } else {
  367. assert(p == PARTITION_SPLIT);
  368. }
  369. }
  370. static void write_modes_sb(VP9_COMP *cpi,
  371. const TileInfo *const tile, vp9_writer *w,
  372. TOKENEXTRA **tok, const TOKENEXTRA *const tok_end,
  373. int mi_row, int mi_col, BLOCK_SIZE bsize) {
  374. const VP9_COMMON *const cm = &cpi->common;
  375. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  376. const int bsl = b_width_log2_lookup[bsize];
  377. const int bs = (1 << bsl) / 4;
  378. PARTITION_TYPE partition;
  379. BLOCK_SIZE subsize;
  380. const MODE_INFO *m = NULL;
  381. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
  382. return;
  383. m = cm->mi_grid_visible[mi_row * cm->mi_stride + mi_col];
  384. partition = partition_lookup[bsl][m->mbmi.sb_type];
  385. write_partition(cm, xd, bs, mi_row, mi_col, partition, bsize, w);
  386. subsize = get_subsize(bsize, partition);
  387. if (subsize < BLOCK_8X8) {
  388. write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
  389. } else {
  390. switch (partition) {
  391. case PARTITION_NONE:
  392. write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
  393. break;
  394. case PARTITION_HORZ:
  395. write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
  396. if (mi_row + bs < cm->mi_rows)
  397. write_modes_b(cpi, tile, w, tok, tok_end, mi_row + bs, mi_col);
  398. break;
  399. case PARTITION_VERT:
  400. write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col);
  401. if (mi_col + bs < cm->mi_cols)
  402. write_modes_b(cpi, tile, w, tok, tok_end, mi_row, mi_col + bs);
  403. break;
  404. case PARTITION_SPLIT:
  405. write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col, subsize);
  406. write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col + bs,
  407. subsize);
  408. write_modes_sb(cpi, tile, w, tok, tok_end, mi_row + bs, mi_col,
  409. subsize);
  410. write_modes_sb(cpi, tile, w, tok, tok_end, mi_row + bs, mi_col + bs,
  411. subsize);
  412. break;
  413. default:
  414. assert(0);
  415. }
  416. }
  417. // update partition context
  418. if (bsize >= BLOCK_8X8 &&
  419. (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
  420. update_partition_context(xd, mi_row, mi_col, subsize, bsize);
  421. }
  422. static void write_modes(VP9_COMP *cpi,
  423. const TileInfo *const tile, vp9_writer *w,
  424. TOKENEXTRA **tok, const TOKENEXTRA *const tok_end) {
  425. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  426. int mi_row, mi_col;
  427. for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
  428. mi_row += MI_BLOCK_SIZE) {
  429. vp9_zero(xd->left_seg_context);
  430. for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
  431. mi_col += MI_BLOCK_SIZE)
  432. write_modes_sb(cpi, tile, w, tok, tok_end, mi_row, mi_col,
  433. BLOCK_64X64);
  434. }
  435. }
  436. static void build_tree_distribution(VP9_COMP *cpi, TX_SIZE tx_size,
  437. vp9_coeff_stats *coef_branch_ct,
  438. vp9_coeff_probs_model *coef_probs) {
  439. vp9_coeff_count *coef_counts = cpi->td.rd_counts.coef_counts[tx_size];
  440. unsigned int (*eob_branch_ct)[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS] =
  441. cpi->common.counts.eob_branch[tx_size];
  442. int i, j, k, l, m;
  443. for (i = 0; i < PLANE_TYPES; ++i) {
  444. for (j = 0; j < REF_TYPES; ++j) {
  445. for (k = 0; k < COEF_BANDS; ++k) {
  446. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
  447. vp9_tree_probs_from_distribution(vp9_coef_tree,
  448. coef_branch_ct[i][j][k][l],
  449. coef_counts[i][j][k][l]);
  450. coef_branch_ct[i][j][k][l][0][1] = eob_branch_ct[i][j][k][l] -
  451. coef_branch_ct[i][j][k][l][0][0];
  452. for (m = 0; m < UNCONSTRAINED_NODES; ++m)
  453. coef_probs[i][j][k][l][m] = get_binary_prob(
  454. coef_branch_ct[i][j][k][l][m][0],
  455. coef_branch_ct[i][j][k][l][m][1]);
  456. }
  457. }
  458. }
  459. }
  460. }
  461. static void update_coef_probs_common(vp9_writer* const bc, VP9_COMP *cpi,
  462. TX_SIZE tx_size,
  463. vp9_coeff_stats *frame_branch_ct,
  464. vp9_coeff_probs_model *new_coef_probs) {
  465. vp9_coeff_probs_model *old_coef_probs = cpi->common.fc->coef_probs[tx_size];
  466. const vp9_prob upd = DIFF_UPDATE_PROB;
  467. const int entropy_nodes_update = UNCONSTRAINED_NODES;
  468. int i, j, k, l, t;
  469. int stepsize = cpi->sf.coeff_prob_appx_step;
  470. switch (cpi->sf.use_fast_coef_updates) {
  471. case TWO_LOOP: {
  472. /* dry run to see if there is any update at all needed */
  473. int savings = 0;
  474. int update[2] = {0, 0};
  475. for (i = 0; i < PLANE_TYPES; ++i) {
  476. for (j = 0; j < REF_TYPES; ++j) {
  477. for (k = 0; k < COEF_BANDS; ++k) {
  478. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
  479. for (t = 0; t < entropy_nodes_update; ++t) {
  480. vp9_prob newp = new_coef_probs[i][j][k][l][t];
  481. const vp9_prob oldp = old_coef_probs[i][j][k][l][t];
  482. int s;
  483. int u = 0;
  484. if (t == PIVOT_NODE)
  485. s = vp9_prob_diff_update_savings_search_model(
  486. frame_branch_ct[i][j][k][l][0],
  487. old_coef_probs[i][j][k][l], &newp, upd, stepsize);
  488. else
  489. s = vp9_prob_diff_update_savings_search(
  490. frame_branch_ct[i][j][k][l][t], oldp, &newp, upd);
  491. if (s > 0 && newp != oldp)
  492. u = 1;
  493. if (u)
  494. savings += s - (int)(vp9_cost_zero(upd));
  495. else
  496. savings -= (int)(vp9_cost_zero(upd));
  497. update[u]++;
  498. }
  499. }
  500. }
  501. }
  502. }
  503. // printf("Update %d %d, savings %d\n", update[0], update[1], savings);
  504. /* Is coef updated at all */
  505. if (update[1] == 0 || savings < 0) {
  506. vp9_write_bit(bc, 0);
  507. return;
  508. }
  509. vp9_write_bit(bc, 1);
  510. for (i = 0; i < PLANE_TYPES; ++i) {
  511. for (j = 0; j < REF_TYPES; ++j) {
  512. for (k = 0; k < COEF_BANDS; ++k) {
  513. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
  514. // calc probs and branch cts for this frame only
  515. for (t = 0; t < entropy_nodes_update; ++t) {
  516. vp9_prob newp = new_coef_probs[i][j][k][l][t];
  517. vp9_prob *oldp = old_coef_probs[i][j][k][l] + t;
  518. const vp9_prob upd = DIFF_UPDATE_PROB;
  519. int s;
  520. int u = 0;
  521. if (t == PIVOT_NODE)
  522. s = vp9_prob_diff_update_savings_search_model(
  523. frame_branch_ct[i][j][k][l][0],
  524. old_coef_probs[i][j][k][l], &newp, upd, stepsize);
  525. else
  526. s = vp9_prob_diff_update_savings_search(
  527. frame_branch_ct[i][j][k][l][t],
  528. *oldp, &newp, upd);
  529. if (s > 0 && newp != *oldp)
  530. u = 1;
  531. vp9_write(bc, u, upd);
  532. if (u) {
  533. /* send/use new probability */
  534. vp9_write_prob_diff_update(bc, newp, *oldp);
  535. *oldp = newp;
  536. }
  537. }
  538. }
  539. }
  540. }
  541. }
  542. return;
  543. }
  544. case ONE_LOOP_REDUCED: {
  545. int updates = 0;
  546. int noupdates_before_first = 0;
  547. for (i = 0; i < PLANE_TYPES; ++i) {
  548. for (j = 0; j < REF_TYPES; ++j) {
  549. for (k = 0; k < COEF_BANDS; ++k) {
  550. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) {
  551. // calc probs and branch cts for this frame only
  552. for (t = 0; t < entropy_nodes_update; ++t) {
  553. vp9_prob newp = new_coef_probs[i][j][k][l][t];
  554. vp9_prob *oldp = old_coef_probs[i][j][k][l] + t;
  555. int s;
  556. int u = 0;
  557. if (t == PIVOT_NODE) {
  558. s = vp9_prob_diff_update_savings_search_model(
  559. frame_branch_ct[i][j][k][l][0],
  560. old_coef_probs[i][j][k][l], &newp, upd, stepsize);
  561. } else {
  562. s = vp9_prob_diff_update_savings_search(
  563. frame_branch_ct[i][j][k][l][t],
  564. *oldp, &newp, upd);
  565. }
  566. if (s > 0 && newp != *oldp)
  567. u = 1;
  568. updates += u;
  569. if (u == 0 && updates == 0) {
  570. noupdates_before_first++;
  571. continue;
  572. }
  573. if (u == 1 && updates == 1) {
  574. int v;
  575. // first update
  576. vp9_write_bit(bc, 1);
  577. for (v = 0; v < noupdates_before_first; ++v)
  578. vp9_write(bc, 0, upd);
  579. }
  580. vp9_write(bc, u, upd);
  581. if (u) {
  582. /* send/use new probability */
  583. vp9_write_prob_diff_update(bc, newp, *oldp);
  584. *oldp = newp;
  585. }
  586. }
  587. }
  588. }
  589. }
  590. }
  591. if (updates == 0) {
  592. vp9_write_bit(bc, 0); // no updates
  593. }
  594. return;
  595. }
  596. default:
  597. assert(0);
  598. }
  599. }
  600. static void update_coef_probs(VP9_COMP *cpi, vp9_writer* w) {
  601. const TX_MODE tx_mode = cpi->common.tx_mode;
  602. const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
  603. TX_SIZE tx_size;
  604. for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size) {
  605. vp9_coeff_stats frame_branch_ct[PLANE_TYPES];
  606. vp9_coeff_probs_model frame_coef_probs[PLANE_TYPES];
  607. if (cpi->td.counts->tx.tx_totals[tx_size] <= 20 ||
  608. (tx_size >= TX_16X16 && cpi->sf.tx_size_search_method == USE_TX_8X8)) {
  609. vp9_write_bit(w, 0);
  610. } else {
  611. build_tree_distribution(cpi, tx_size, frame_branch_ct,
  612. frame_coef_probs);
  613. update_coef_probs_common(w, cpi, tx_size, frame_branch_ct,
  614. frame_coef_probs);
  615. }
  616. }
  617. }
  618. static void encode_loopfilter(struct loopfilter *lf,
  619. struct vp9_write_bit_buffer *wb) {
  620. int i;
  621. // Encode the loop filter level and type
  622. vp9_wb_write_literal(wb, lf->filter_level, 6);
  623. vp9_wb_write_literal(wb, lf->sharpness_level, 3);
  624. // Write out loop filter deltas applied at the MB level based on mode or
  625. // ref frame (if they are enabled).
  626. vp9_wb_write_bit(wb, lf->mode_ref_delta_enabled);
  627. if (lf->mode_ref_delta_enabled) {
  628. vp9_wb_write_bit(wb, lf->mode_ref_delta_update);
  629. if (lf->mode_ref_delta_update) {
  630. for (i = 0; i < MAX_REF_LF_DELTAS; i++) {
  631. const int delta = lf->ref_deltas[i];
  632. const int changed = delta != lf->last_ref_deltas[i];
  633. vp9_wb_write_bit(wb, changed);
  634. if (changed) {
  635. lf->last_ref_deltas[i] = delta;
  636. vp9_wb_write_literal(wb, abs(delta) & 0x3F, 6);
  637. vp9_wb_write_bit(wb, delta < 0);
  638. }
  639. }
  640. for (i = 0; i < MAX_MODE_LF_DELTAS; i++) {
  641. const int delta = lf->mode_deltas[i];
  642. const int changed = delta != lf->last_mode_deltas[i];
  643. vp9_wb_write_bit(wb, changed);
  644. if (changed) {
  645. lf->last_mode_deltas[i] = delta;
  646. vp9_wb_write_literal(wb, abs(delta) & 0x3F, 6);
  647. vp9_wb_write_bit(wb, delta < 0);
  648. }
  649. }
  650. }
  651. }
  652. }
  653. static void write_delta_q(struct vp9_write_bit_buffer *wb, int delta_q) {
  654. if (delta_q != 0) {
  655. vp9_wb_write_bit(wb, 1);
  656. vp9_wb_write_literal(wb, abs(delta_q), 4);
  657. vp9_wb_write_bit(wb, delta_q < 0);
  658. } else {
  659. vp9_wb_write_bit(wb, 0);
  660. }
  661. }
  662. static void encode_quantization(const VP9_COMMON *const cm,
  663. struct vp9_write_bit_buffer *wb) {
  664. vp9_wb_write_literal(wb, cm->base_qindex, QINDEX_BITS);
  665. write_delta_q(wb, cm->y_dc_delta_q);
  666. write_delta_q(wb, cm->uv_dc_delta_q);
  667. write_delta_q(wb, cm->uv_ac_delta_q);
  668. }
  669. static void encode_segmentation(VP9_COMMON *cm, MACROBLOCKD *xd,
  670. struct vp9_write_bit_buffer *wb) {
  671. int i, j;
  672. const struct segmentation *seg = &cm->seg;
  673. vp9_wb_write_bit(wb, seg->enabled);
  674. if (!seg->enabled)
  675. return;
  676. // Segmentation map
  677. vp9_wb_write_bit(wb, seg->update_map);
  678. if (seg->update_map) {
  679. // Select the coding strategy (temporal or spatial)
  680. vp9_choose_segmap_coding_method(cm, xd);
  681. // Write out probabilities used to decode unpredicted macro-block segments
  682. for (i = 0; i < SEG_TREE_PROBS; i++) {
  683. const int prob = seg->tree_probs[i];
  684. const int update = prob != MAX_PROB;
  685. vp9_wb_write_bit(wb, update);
  686. if (update)
  687. vp9_wb_write_literal(wb, prob, 8);
  688. }
  689. // Write out the chosen coding method.
  690. vp9_wb_write_bit(wb, seg->temporal_update);
  691. if (seg->temporal_update) {
  692. for (i = 0; i < PREDICTION_PROBS; i++) {
  693. const int prob = seg->pred_probs[i];
  694. const int update = prob != MAX_PROB;
  695. vp9_wb_write_bit(wb, update);
  696. if (update)
  697. vp9_wb_write_literal(wb, prob, 8);
  698. }
  699. }
  700. }
  701. // Segmentation data
  702. vp9_wb_write_bit(wb, seg->update_data);
  703. if (seg->update_data) {
  704. vp9_wb_write_bit(wb, seg->abs_delta);
  705. for (i = 0; i < MAX_SEGMENTS; i++) {
  706. for (j = 0; j < SEG_LVL_MAX; j++) {
  707. const int active = vp9_segfeature_active(seg, i, j);
  708. vp9_wb_write_bit(wb, active);
  709. if (active) {
  710. const int data = vp9_get_segdata(seg, i, j);
  711. const int data_max = vp9_seg_feature_data_max(j);
  712. if (vp9_is_segfeature_signed(j)) {
  713. encode_unsigned_max(wb, abs(data), data_max);
  714. vp9_wb_write_bit(wb, data < 0);
  715. } else {
  716. encode_unsigned_max(wb, data, data_max);
  717. }
  718. }
  719. }
  720. }
  721. }
  722. }
  723. static void encode_txfm_probs(VP9_COMMON *cm, vp9_writer *w,
  724. FRAME_COUNTS *counts) {
  725. // Mode
  726. vp9_write_literal(w, MIN(cm->tx_mode, ALLOW_32X32), 2);
  727. if (cm->tx_mode >= ALLOW_32X32)
  728. vp9_write_bit(w, cm->tx_mode == TX_MODE_SELECT);
  729. // Probabilities
  730. if (cm->tx_mode == TX_MODE_SELECT) {
  731. int i, j;
  732. unsigned int ct_8x8p[TX_SIZES - 3][2];
  733. unsigned int ct_16x16p[TX_SIZES - 2][2];
  734. unsigned int ct_32x32p[TX_SIZES - 1][2];
  735. for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
  736. tx_counts_to_branch_counts_8x8(counts->tx.p8x8[i], ct_8x8p);
  737. for (j = 0; j < TX_SIZES - 3; j++)
  738. vp9_cond_prob_diff_update(w, &cm->fc->tx_probs.p8x8[i][j], ct_8x8p[j]);
  739. }
  740. for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
  741. tx_counts_to_branch_counts_16x16(counts->tx.p16x16[i], ct_16x16p);
  742. for (j = 0; j < TX_SIZES - 2; j++)
  743. vp9_cond_prob_diff_update(w, &cm->fc->tx_probs.p16x16[i][j],
  744. ct_16x16p[j]);
  745. }
  746. for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
  747. tx_counts_to_branch_counts_32x32(counts->tx.p32x32[i], ct_32x32p);
  748. for (j = 0; j < TX_SIZES - 1; j++)
  749. vp9_cond_prob_diff_update(w, &cm->fc->tx_probs.p32x32[i][j],
  750. ct_32x32p[j]);
  751. }
  752. }
  753. }
  754. static void write_interp_filter(INTERP_FILTER filter,
  755. struct vp9_write_bit_buffer *wb) {
  756. const int filter_to_literal[] = { 1, 0, 2, 3 };
  757. vp9_wb_write_bit(wb, filter == SWITCHABLE);
  758. if (filter != SWITCHABLE)
  759. vp9_wb_write_literal(wb, filter_to_literal[filter], 2);
  760. }
  761. static void fix_interp_filter(VP9_COMMON *cm, FRAME_COUNTS *counts) {
  762. if (cm->interp_filter == SWITCHABLE) {
  763. // Check to see if only one of the filters is actually used
  764. int count[SWITCHABLE_FILTERS];
  765. int i, j, c = 0;
  766. for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
  767. count[i] = 0;
  768. for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
  769. count[i] += counts->switchable_interp[j][i];
  770. c += (count[i] > 0);
  771. }
  772. if (c == 1) {
  773. // Only one filter is used. So set the filter at frame level
  774. for (i = 0; i < SWITCHABLE_FILTERS; ++i) {
  775. if (count[i]) {
  776. cm->interp_filter = i;
  777. break;
  778. }
  779. }
  780. }
  781. }
  782. }
  783. static void write_tile_info(const VP9_COMMON *const cm,
  784. struct vp9_write_bit_buffer *wb) {
  785. int min_log2_tile_cols, max_log2_tile_cols, ones;
  786. vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
  787. // columns
  788. ones = cm->log2_tile_cols - min_log2_tile_cols;
  789. while (ones--)
  790. vp9_wb_write_bit(wb, 1);
  791. if (cm->log2_tile_cols < max_log2_tile_cols)
  792. vp9_wb_write_bit(wb, 0);
  793. // rows
  794. vp9_wb_write_bit(wb, cm->log2_tile_rows != 0);
  795. if (cm->log2_tile_rows != 0)
  796. vp9_wb_write_bit(wb, cm->log2_tile_rows != 1);
  797. }
  798. static int get_refresh_mask(VP9_COMP *cpi) {
  799. if (vp9_preserve_existing_gf(cpi)) {
  800. // We have decided to preserve the previously existing golden frame as our
  801. // new ARF frame. However, in the short term we leave it in the GF slot and,
  802. // if we're updating the GF with the current decoded frame, we save it
  803. // instead to the ARF slot.
  804. // Later, in the function vp9_encoder.c:vp9_update_reference_frames() we
  805. // will swap gld_fb_idx and alt_fb_idx to achieve our objective. We do it
  806. // there so that it can be done outside of the recode loop.
  807. // Note: This is highly specific to the use of ARF as a forward reference,
  808. // and this needs to be generalized as other uses are implemented
  809. // (like RTC/temporal scalability).
  810. return (cpi->refresh_last_frame << cpi->lst_fb_idx) |
  811. (cpi->refresh_golden_frame << cpi->alt_fb_idx);
  812. } else {
  813. int arf_idx = cpi->alt_fb_idx;
  814. if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
  815. const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
  816. arf_idx = gf_group->arf_update_idx[gf_group->index];
  817. }
  818. return (cpi->refresh_last_frame << cpi->lst_fb_idx) |
  819. (cpi->refresh_golden_frame << cpi->gld_fb_idx) |
  820. (cpi->refresh_alt_ref_frame << arf_idx);
  821. }
  822. }
  823. static size_t encode_tiles(VP9_COMP *cpi, uint8_t *data_ptr) {
  824. VP9_COMMON *const cm = &cpi->common;
  825. vp9_writer residual_bc;
  826. int tile_row, tile_col;
  827. TOKENEXTRA *tok_end;
  828. size_t total_size = 0;
  829. const int tile_cols = 1 << cm->log2_tile_cols;
  830. const int tile_rows = 1 << cm->log2_tile_rows;
  831. memset(cm->above_seg_context, 0,
  832. sizeof(*cm->above_seg_context) * mi_cols_aligned_to_sb(cm->mi_cols));
  833. for (tile_row = 0; tile_row < tile_rows; tile_row++) {
  834. for (tile_col = 0; tile_col < tile_cols; tile_col++) {
  835. int tile_idx = tile_row * tile_cols + tile_col;
  836. TOKENEXTRA *tok = cpi->tile_tok[tile_row][tile_col];
  837. tok_end = cpi->tile_tok[tile_row][tile_col] +
  838. cpi->tok_count[tile_row][tile_col];
  839. if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1)
  840. vp9_start_encode(&residual_bc, data_ptr + total_size + 4);
  841. else
  842. vp9_start_encode(&residual_bc, data_ptr + total_size);
  843. write_modes(cpi, &cpi->tile_data[tile_idx].tile_info,
  844. &residual_bc, &tok, tok_end);
  845. assert(tok == tok_end);
  846. vp9_stop_encode(&residual_bc);
  847. if (tile_col < tile_cols - 1 || tile_row < tile_rows - 1) {
  848. // size of this tile
  849. mem_put_be32(data_ptr + total_size, residual_bc.pos);
  850. total_size += 4;
  851. }
  852. total_size += residual_bc.pos;
  853. }
  854. }
  855. return total_size;
  856. }
  857. static void write_display_size(const VP9_COMMON *cm,
  858. struct vp9_write_bit_buffer *wb) {
  859. const int scaling_active = cm->width != cm->display_width ||
  860. cm->height != cm->display_height;
  861. vp9_wb_write_bit(wb, scaling_active);
  862. if (scaling_active) {
  863. vp9_wb_write_literal(wb, cm->display_width - 1, 16);
  864. vp9_wb_write_literal(wb, cm->display_height - 1, 16);
  865. }
  866. }
  867. static void write_frame_size(const VP9_COMMON *cm,
  868. struct vp9_write_bit_buffer *wb) {
  869. vp9_wb_write_literal(wb, cm->width - 1, 16);
  870. vp9_wb_write_literal(wb, cm->height - 1, 16);
  871. write_display_size(cm, wb);
  872. }
  873. static void write_frame_size_with_refs(VP9_COMP *cpi,
  874. struct vp9_write_bit_buffer *wb) {
  875. VP9_COMMON *const cm = &cpi->common;
  876. int found = 0;
  877. MV_REFERENCE_FRAME ref_frame;
  878. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  879. YV12_BUFFER_CONFIG *cfg = get_ref_frame_buffer(cpi, ref_frame);
  880. // Set "found" to 0 for temporal svc and for spatial svc key frame
  881. if (cpi->use_svc &&
  882. ((cpi->svc.number_temporal_layers > 1 &&
  883. cpi->oxcf.rc_mode == VPX_CBR) ||
  884. (cpi->svc.number_spatial_layers > 1 &&
  885. cpi->svc.layer_context[cpi->svc.spatial_layer_id].is_key_frame) ||
  886. (is_two_pass_svc(cpi) &&
  887. cpi->svc.encode_empty_frame_state == ENCODING &&
  888. cpi->svc.layer_context[0].frames_from_key_frame <
  889. cpi->svc.number_temporal_layers + 1))) {
  890. found = 0;
  891. } else if (cfg != NULL) {
  892. found = cm->width == cfg->y_crop_width &&
  893. cm->height == cfg->y_crop_height;
  894. }
  895. vp9_wb_write_bit(wb, found);
  896. if (found) {
  897. break;
  898. }
  899. }
  900. if (!found) {
  901. vp9_wb_write_literal(wb, cm->width - 1, 16);
  902. vp9_wb_write_literal(wb, cm->height - 1, 16);
  903. }
  904. write_display_size(cm, wb);
  905. }
  906. static void write_sync_code(struct vp9_write_bit_buffer *wb) {
  907. vp9_wb_write_literal(wb, VP9_SYNC_CODE_0, 8);
  908. vp9_wb_write_literal(wb, VP9_SYNC_CODE_1, 8);
  909. vp9_wb_write_literal(wb, VP9_SYNC_CODE_2, 8);
  910. }
  911. static void write_profile(BITSTREAM_PROFILE profile,
  912. struct vp9_write_bit_buffer *wb) {
  913. switch (profile) {
  914. case PROFILE_0:
  915. vp9_wb_write_literal(wb, 0, 2);
  916. break;
  917. case PROFILE_1:
  918. vp9_wb_write_literal(wb, 2, 2);
  919. break;
  920. case PROFILE_2:
  921. vp9_wb_write_literal(wb, 1, 2);
  922. break;
  923. case PROFILE_3:
  924. vp9_wb_write_literal(wb, 6, 3);
  925. break;
  926. default:
  927. assert(0);
  928. }
  929. }
  930. static void write_bitdepth_colorspace_sampling(
  931. VP9_COMMON *const cm, struct vp9_write_bit_buffer *wb) {
  932. if (cm->profile >= PROFILE_2) {
  933. assert(cm->bit_depth > VPX_BITS_8);
  934. vp9_wb_write_bit(wb, cm->bit_depth == VPX_BITS_10 ? 0 : 1);
  935. }
  936. vp9_wb_write_literal(wb, cm->color_space, 3);
  937. if (cm->color_space != VPX_CS_SRGB) {
  938. vp9_wb_write_bit(wb, 0); // 0: [16, 235] (i.e. xvYCC), 1: [0, 255]
  939. if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
  940. assert(cm->subsampling_x != 1 || cm->subsampling_y != 1);
  941. vp9_wb_write_bit(wb, cm->subsampling_x);
  942. vp9_wb_write_bit(wb, cm->subsampling_y);
  943. vp9_wb_write_bit(wb, 0); // unused
  944. } else {
  945. assert(cm->subsampling_x == 1 && cm->subsampling_y == 1);
  946. }
  947. } else {
  948. assert(cm->profile == PROFILE_1 || cm->profile == PROFILE_3);
  949. vp9_wb_write_bit(wb, 0); // unused
  950. }
  951. }
  952. static void write_uncompressed_header(VP9_COMP *cpi,
  953. struct vp9_write_bit_buffer *wb) {
  954. VP9_COMMON *const cm = &cpi->common;
  955. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  956. vp9_wb_write_literal(wb, VP9_FRAME_MARKER, 2);
  957. write_profile(cm->profile, wb);
  958. vp9_wb_write_bit(wb, 0); // show_existing_frame
  959. vp9_wb_write_bit(wb, cm->frame_type);
  960. vp9_wb_write_bit(wb, cm->show_frame);
  961. vp9_wb_write_bit(wb, cm->error_resilient_mode);
  962. if (cm->frame_type == KEY_FRAME) {
  963. write_sync_code(wb);
  964. write_bitdepth_colorspace_sampling(cm, wb);
  965. write_frame_size(cm, wb);
  966. } else {
  967. // In spatial svc if it's not error_resilient_mode then we need to code all
  968. // visible frames as invisible. But we need to keep the show_frame flag so
  969. // that the publisher could know whether it is supposed to be visible.
  970. // So we will code the show_frame flag as it is. Then code the intra_only
  971. // bit here. This will make the bitstream incompatible. In the player we
  972. // will change to show_frame flag to 0, then add an one byte frame with
  973. // show_existing_frame flag which tells the decoder which frame we want to
  974. // show.
  975. if (!cm->show_frame)
  976. vp9_wb_write_bit(wb, cm->intra_only);
  977. if (!cm->error_resilient_mode)
  978. vp9_wb_write_literal(wb, cm->reset_frame_context, 2);
  979. if (cm->intra_only) {
  980. write_sync_code(wb);
  981. // Note for profile 0, 420 8bpp is assumed.
  982. if (cm->profile > PROFILE_0) {
  983. write_bitdepth_colorspace_sampling(cm, wb);
  984. }
  985. vp9_wb_write_literal(wb, get_refresh_mask(cpi), REF_FRAMES);
  986. write_frame_size(cm, wb);
  987. } else {
  988. MV_REFERENCE_FRAME ref_frame;
  989. vp9_wb_write_literal(wb, get_refresh_mask(cpi), REF_FRAMES);
  990. for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
  991. assert(get_ref_frame_map_idx(cpi, ref_frame) != INVALID_IDX);
  992. vp9_wb_write_literal(wb, get_ref_frame_map_idx(cpi, ref_frame),
  993. REF_FRAMES_LOG2);
  994. vp9_wb_write_bit(wb, cm->ref_frame_sign_bias[ref_frame]);
  995. }
  996. write_frame_size_with_refs(cpi, wb);
  997. vp9_wb_write_bit(wb, cm->allow_high_precision_mv);
  998. fix_interp_filter(cm, cpi->td.counts);
  999. write_interp_filter(cm->interp_filter, wb);
  1000. }
  1001. }
  1002. if (!cm->error_resilient_mode) {
  1003. vp9_wb_write_bit(wb, cm->refresh_frame_context);
  1004. vp9_wb_write_bit(wb, cm->frame_parallel_decoding_mode);
  1005. }
  1006. vp9_wb_write_literal(wb, cm->frame_context_idx, FRAME_CONTEXTS_LOG2);
  1007. encode_loopfilter(&cm->lf, wb);
  1008. encode_quantization(cm, wb);
  1009. encode_segmentation(cm, xd, wb);
  1010. write_tile_info(cm, wb);
  1011. }
  1012. static size_t write_compressed_header(VP9_COMP *cpi, uint8_t *data) {
  1013. VP9_COMMON *const cm = &cpi->common;
  1014. MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
  1015. FRAME_CONTEXT *const fc = cm->fc;
  1016. FRAME_COUNTS *counts = cpi->td.counts;
  1017. vp9_writer header_bc;
  1018. vp9_start_encode(&header_bc, data);
  1019. if (xd->lossless)
  1020. cm->tx_mode = ONLY_4X4;
  1021. else
  1022. encode_txfm_probs(cm, &header_bc, counts);
  1023. update_coef_probs(cpi, &header_bc);
  1024. update_skip_probs(cm, &header_bc, counts);
  1025. if (!frame_is_intra_only(cm)) {
  1026. int i;
  1027. for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
  1028. prob_diff_update(vp9_inter_mode_tree, cm->fc->inter_mode_probs[i],
  1029. counts->inter_mode[i], INTER_MODES, &header_bc);
  1030. if (cm->interp_filter == SWITCHABLE)
  1031. update_switchable_interp_probs(cm, &header_bc, counts);
  1032. for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
  1033. vp9_cond_prob_diff_update(&header_bc, &fc->intra_inter_prob[i],
  1034. counts->intra_inter[i]);
  1035. if (cpi->allow_comp_inter_inter) {
  1036. const int use_compound_pred = cm->reference_mode != SINGLE_REFERENCE;
  1037. const int use_hybrid_pred = cm->reference_mode == REFERENCE_MODE_SELECT;
  1038. vp9_write_bit(&header_bc, use_compound_pred);
  1039. if (use_compound_pred) {
  1040. vp9_write_bit(&header_bc, use_hybrid_pred);
  1041. if (use_hybrid_pred)
  1042. for (i = 0; i < COMP_INTER_CONTEXTS; i++)
  1043. vp9_cond_prob_diff_update(&header_bc, &fc->comp_inter_prob[i],
  1044. counts->comp_inter[i]);
  1045. }
  1046. }
  1047. if (cm->reference_mode != COMPOUND_REFERENCE) {
  1048. for (i = 0; i < REF_CONTEXTS; i++) {
  1049. vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][0],
  1050. counts->single_ref[i][0]);
  1051. vp9_cond_prob_diff_update(&header_bc, &fc->single_ref_prob[i][1],
  1052. counts->single_ref[i][1]);
  1053. }
  1054. }
  1055. if (cm->reference_mode != SINGLE_REFERENCE)
  1056. for (i = 0; i < REF_CONTEXTS; i++)
  1057. vp9_cond_prob_diff_update(&header_bc, &fc->comp_ref_prob[i],
  1058. counts->comp_ref[i]);
  1059. for (i = 0; i < BLOCK_SIZE_GROUPS; ++i)
  1060. prob_diff_update(vp9_intra_mode_tree, cm->fc->y_mode_prob[i],
  1061. counts->y_mode[i], INTRA_MODES, &header_bc);
  1062. for (i = 0; i < PARTITION_CONTEXTS; ++i)
  1063. prob_diff_update(vp9_partition_tree, fc->partition_prob[i],
  1064. counts->partition[i], PARTITION_TYPES, &header_bc);
  1065. vp9_write_nmv_probs(cm, cm->allow_high_precision_mv, &header_bc,
  1066. &counts->mv);
  1067. }
  1068. vp9_stop_encode(&header_bc);
  1069. assert(header_bc.pos <= 0xffff);
  1070. return header_bc.pos;
  1071. }
  1072. void vp9_pack_bitstream(VP9_COMP *cpi, uint8_t *dest, size_t *size) {
  1073. uint8_t *data = dest;
  1074. size_t first_part_size, uncompressed_hdr_size;
  1075. struct vp9_write_bit_buffer wb = {data, 0};
  1076. struct vp9_write_bit_buffer saved_wb;
  1077. write_uncompressed_header(cpi, &wb);
  1078. saved_wb = wb;
  1079. vp9_wb_write_literal(&wb, 0, 16); // don't know in advance first part. size
  1080. uncompressed_hdr_size = vp9_wb_bytes_written(&wb);
  1081. data += uncompressed_hdr_size;
  1082. vp9_clear_system_state();
  1083. first_part_size = write_compressed_header(cpi, data);
  1084. data += first_part_size;
  1085. // TODO(jbb): Figure out what to do if first_part_size > 16 bits.
  1086. vp9_wb_write_literal(&saved_wb, (int)first_part_size, 16);
  1087. data += encode_tiles(cpi, data);
  1088. *size = data - dest;
  1089. }