syntax.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Header syntax writing
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <assert.h>
  14. #include "../utils/utils.h"
  15. #include "../webp/format_constants.h" // RIFF constants
  16. #include "../webp/mux_types.h" // ALPHA_FLAG
  17. #include "./vp8enci.h"
  18. //------------------------------------------------------------------------------
  19. // Helper functions
  20. static int IsVP8XNeeded(const VP8Encoder* const enc) {
  21. return !!enc->has_alpha_; // Currently the only case when VP8X is needed.
  22. // This could change in the future.
  23. }
  24. static int PutPaddingByte(const WebPPicture* const pic) {
  25. const uint8_t pad_byte[1] = { 0 };
  26. return !!pic->writer(pad_byte, 1, pic);
  27. }
  28. //------------------------------------------------------------------------------
  29. // Writers for header's various pieces (in order of appearance)
  30. static WebPEncodingError PutRIFFHeader(const VP8Encoder* const enc,
  31. size_t riff_size) {
  32. const WebPPicture* const pic = enc->pic_;
  33. uint8_t riff[RIFF_HEADER_SIZE] = {
  34. 'R', 'I', 'F', 'F', 0, 0, 0, 0, 'W', 'E', 'B', 'P'
  35. };
  36. assert(riff_size == (uint32_t)riff_size);
  37. PutLE32(riff + TAG_SIZE, (uint32_t)riff_size);
  38. if (!pic->writer(riff, sizeof(riff), pic)) {
  39. return VP8_ENC_ERROR_BAD_WRITE;
  40. }
  41. return VP8_ENC_OK;
  42. }
  43. static WebPEncodingError PutVP8XHeader(const VP8Encoder* const enc) {
  44. const WebPPicture* const pic = enc->pic_;
  45. uint8_t vp8x[CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE] = {
  46. 'V', 'P', '8', 'X'
  47. };
  48. uint32_t flags = 0;
  49. assert(IsVP8XNeeded(enc));
  50. assert(pic->width >= 1 && pic->height >= 1);
  51. assert(pic->width <= MAX_CANVAS_SIZE && pic->height <= MAX_CANVAS_SIZE);
  52. if (enc->has_alpha_) {
  53. flags |= ALPHA_FLAG;
  54. }
  55. PutLE32(vp8x + TAG_SIZE, VP8X_CHUNK_SIZE);
  56. PutLE32(vp8x + CHUNK_HEADER_SIZE, flags);
  57. PutLE24(vp8x + CHUNK_HEADER_SIZE + 4, pic->width - 1);
  58. PutLE24(vp8x + CHUNK_HEADER_SIZE + 7, pic->height - 1);
  59. if (!pic->writer(vp8x, sizeof(vp8x), pic)) {
  60. return VP8_ENC_ERROR_BAD_WRITE;
  61. }
  62. return VP8_ENC_OK;
  63. }
  64. static WebPEncodingError PutAlphaChunk(const VP8Encoder* const enc) {
  65. const WebPPicture* const pic = enc->pic_;
  66. uint8_t alpha_chunk_hdr[CHUNK_HEADER_SIZE] = {
  67. 'A', 'L', 'P', 'H'
  68. };
  69. assert(enc->has_alpha_);
  70. // Alpha chunk header.
  71. PutLE32(alpha_chunk_hdr + TAG_SIZE, enc->alpha_data_size_);
  72. if (!pic->writer(alpha_chunk_hdr, sizeof(alpha_chunk_hdr), pic)) {
  73. return VP8_ENC_ERROR_BAD_WRITE;
  74. }
  75. // Alpha chunk data.
  76. if (!pic->writer(enc->alpha_data_, enc->alpha_data_size_, pic)) {
  77. return VP8_ENC_ERROR_BAD_WRITE;
  78. }
  79. // Padding.
  80. if ((enc->alpha_data_size_ & 1) && !PutPaddingByte(pic)) {
  81. return VP8_ENC_ERROR_BAD_WRITE;
  82. }
  83. return VP8_ENC_OK;
  84. }
  85. static WebPEncodingError PutVP8Header(const WebPPicture* const pic,
  86. size_t vp8_size) {
  87. uint8_t vp8_chunk_hdr[CHUNK_HEADER_SIZE] = {
  88. 'V', 'P', '8', ' '
  89. };
  90. assert(vp8_size == (uint32_t)vp8_size);
  91. PutLE32(vp8_chunk_hdr + TAG_SIZE, (uint32_t)vp8_size);
  92. if (!pic->writer(vp8_chunk_hdr, sizeof(vp8_chunk_hdr), pic)) {
  93. return VP8_ENC_ERROR_BAD_WRITE;
  94. }
  95. return VP8_ENC_OK;
  96. }
  97. static WebPEncodingError PutVP8FrameHeader(const WebPPicture* const pic,
  98. int profile, size_t size0) {
  99. uint8_t vp8_frm_hdr[VP8_FRAME_HEADER_SIZE];
  100. uint32_t bits;
  101. if (size0 >= VP8_MAX_PARTITION0_SIZE) { // partition #0 is too big to fit
  102. return VP8_ENC_ERROR_PARTITION0_OVERFLOW;
  103. }
  104. // Paragraph 9.1.
  105. bits = 0 // keyframe (1b)
  106. | (profile << 1) // profile (3b)
  107. | (1 << 4) // visible (1b)
  108. | ((uint32_t)size0 << 5); // partition length (19b)
  109. vp8_frm_hdr[0] = (bits >> 0) & 0xff;
  110. vp8_frm_hdr[1] = (bits >> 8) & 0xff;
  111. vp8_frm_hdr[2] = (bits >> 16) & 0xff;
  112. // signature
  113. vp8_frm_hdr[3] = (VP8_SIGNATURE >> 16) & 0xff;
  114. vp8_frm_hdr[4] = (VP8_SIGNATURE >> 8) & 0xff;
  115. vp8_frm_hdr[5] = (VP8_SIGNATURE >> 0) & 0xff;
  116. // dimensions
  117. vp8_frm_hdr[6] = pic->width & 0xff;
  118. vp8_frm_hdr[7] = pic->width >> 8;
  119. vp8_frm_hdr[8] = pic->height & 0xff;
  120. vp8_frm_hdr[9] = pic->height >> 8;
  121. if (!pic->writer(vp8_frm_hdr, sizeof(vp8_frm_hdr), pic)) {
  122. return VP8_ENC_ERROR_BAD_WRITE;
  123. }
  124. return VP8_ENC_OK;
  125. }
  126. // WebP Headers.
  127. static int PutWebPHeaders(const VP8Encoder* const enc, size_t size0,
  128. size_t vp8_size, size_t riff_size) {
  129. WebPPicture* const pic = enc->pic_;
  130. WebPEncodingError err = VP8_ENC_OK;
  131. // RIFF header.
  132. err = PutRIFFHeader(enc, riff_size);
  133. if (err != VP8_ENC_OK) goto Error;
  134. // VP8X.
  135. if (IsVP8XNeeded(enc)) {
  136. err = PutVP8XHeader(enc);
  137. if (err != VP8_ENC_OK) goto Error;
  138. }
  139. // Alpha.
  140. if (enc->has_alpha_) {
  141. err = PutAlphaChunk(enc);
  142. if (err != VP8_ENC_OK) goto Error;
  143. }
  144. // VP8 header.
  145. err = PutVP8Header(pic, vp8_size);
  146. if (err != VP8_ENC_OK) goto Error;
  147. // VP8 frame header.
  148. err = PutVP8FrameHeader(pic, enc->profile_, size0);
  149. if (err != VP8_ENC_OK) goto Error;
  150. // All OK.
  151. return 1;
  152. // Error.
  153. Error:
  154. return WebPEncodingSetError(pic, err);
  155. }
  156. // Segmentation header
  157. static void PutSegmentHeader(VP8BitWriter* const bw,
  158. const VP8Encoder* const enc) {
  159. const VP8SegmentHeader* const hdr = &enc->segment_hdr_;
  160. const VP8Proba* const proba = &enc->proba_;
  161. if (VP8PutBitUniform(bw, (hdr->num_segments_ > 1))) {
  162. // We always 'update' the quant and filter strength values
  163. const int update_data = 1;
  164. int s;
  165. VP8PutBitUniform(bw, hdr->update_map_);
  166. if (VP8PutBitUniform(bw, update_data)) {
  167. // we always use absolute values, not relative ones
  168. VP8PutBitUniform(bw, 1); // (segment_feature_mode = 1. Paragraph 9.3.)
  169. for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
  170. VP8PutSignedValue(bw, enc->dqm_[s].quant_, 7);
  171. }
  172. for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
  173. VP8PutSignedValue(bw, enc->dqm_[s].fstrength_, 6);
  174. }
  175. }
  176. if (hdr->update_map_) {
  177. for (s = 0; s < 3; ++s) {
  178. if (VP8PutBitUniform(bw, (proba->segments_[s] != 255u))) {
  179. VP8PutValue(bw, proba->segments_[s], 8);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. // Filtering parameters header
  186. static void PutFilterHeader(VP8BitWriter* const bw,
  187. const VP8FilterHeader* const hdr) {
  188. const int use_lf_delta = (hdr->i4x4_lf_delta_ != 0);
  189. VP8PutBitUniform(bw, hdr->simple_);
  190. VP8PutValue(bw, hdr->level_, 6);
  191. VP8PutValue(bw, hdr->sharpness_, 3);
  192. if (VP8PutBitUniform(bw, use_lf_delta)) {
  193. // '0' is the default value for i4x4_lf_delta_ at frame #0.
  194. const int need_update = (hdr->i4x4_lf_delta_ != 0);
  195. if (VP8PutBitUniform(bw, need_update)) {
  196. // we don't use ref_lf_delta => emit four 0 bits
  197. VP8PutValue(bw, 0, 4);
  198. // we use mode_lf_delta for i4x4
  199. VP8PutSignedValue(bw, hdr->i4x4_lf_delta_, 6);
  200. VP8PutValue(bw, 0, 3); // all others unused
  201. }
  202. }
  203. }
  204. // Nominal quantization parameters
  205. static void PutQuant(VP8BitWriter* const bw,
  206. const VP8Encoder* const enc) {
  207. VP8PutValue(bw, enc->base_quant_, 7);
  208. VP8PutSignedValue(bw, enc->dq_y1_dc_, 4);
  209. VP8PutSignedValue(bw, enc->dq_y2_dc_, 4);
  210. VP8PutSignedValue(bw, enc->dq_y2_ac_, 4);
  211. VP8PutSignedValue(bw, enc->dq_uv_dc_, 4);
  212. VP8PutSignedValue(bw, enc->dq_uv_ac_, 4);
  213. }
  214. // Partition sizes
  215. static int EmitPartitionsSize(const VP8Encoder* const enc,
  216. WebPPicture* const pic) {
  217. uint8_t buf[3 * (MAX_NUM_PARTITIONS - 1)];
  218. int p;
  219. for (p = 0; p < enc->num_parts_ - 1; ++p) {
  220. const size_t part_size = VP8BitWriterSize(enc->parts_ + p);
  221. if (part_size >= VP8_MAX_PARTITION_SIZE) {
  222. return WebPEncodingSetError(pic, VP8_ENC_ERROR_PARTITION_OVERFLOW);
  223. }
  224. buf[3 * p + 0] = (part_size >> 0) & 0xff;
  225. buf[3 * p + 1] = (part_size >> 8) & 0xff;
  226. buf[3 * p + 2] = (part_size >> 16) & 0xff;
  227. }
  228. return p ? pic->writer(buf, 3 * p, pic) : 1;
  229. }
  230. //------------------------------------------------------------------------------
  231. static int GeneratePartition0(VP8Encoder* const enc) {
  232. VP8BitWriter* const bw = &enc->bw_;
  233. const int mb_size = enc->mb_w_ * enc->mb_h_;
  234. uint64_t pos1, pos2, pos3;
  235. pos1 = VP8BitWriterPos(bw);
  236. if (!VP8BitWriterInit(bw, mb_size * 7 / 8)) { // ~7 bits per macroblock
  237. return WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY);
  238. }
  239. VP8PutBitUniform(bw, 0); // colorspace
  240. VP8PutBitUniform(bw, 0); // clamp type
  241. PutSegmentHeader(bw, enc);
  242. PutFilterHeader(bw, &enc->filter_hdr_);
  243. VP8PutValue(bw, enc->num_parts_ == 8 ? 3 :
  244. enc->num_parts_ == 4 ? 2 :
  245. enc->num_parts_ == 2 ? 1 : 0, 2);
  246. PutQuant(bw, enc);
  247. VP8PutBitUniform(bw, 0); // no proba update
  248. VP8WriteProbas(bw, &enc->proba_);
  249. pos2 = VP8BitWriterPos(bw);
  250. VP8CodeIntraModes(enc);
  251. VP8BitWriterFinish(bw);
  252. pos3 = VP8BitWriterPos(bw);
  253. if (enc->pic_->stats) {
  254. enc->pic_->stats->header_bytes[0] = (int)((pos2 - pos1 + 7) >> 3);
  255. enc->pic_->stats->header_bytes[1] = (int)((pos3 - pos2 + 7) >> 3);
  256. enc->pic_->stats->alpha_data_size = (int)enc->alpha_data_size_;
  257. }
  258. if (bw->error_) {
  259. return WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY);
  260. }
  261. return 1;
  262. }
  263. void VP8EncFreeBitWriters(VP8Encoder* const enc) {
  264. int p;
  265. VP8BitWriterWipeOut(&enc->bw_);
  266. for (p = 0; p < enc->num_parts_; ++p) {
  267. VP8BitWriterWipeOut(enc->parts_ + p);
  268. }
  269. }
  270. int VP8EncWrite(VP8Encoder* const enc) {
  271. WebPPicture* const pic = enc->pic_;
  272. VP8BitWriter* const bw = &enc->bw_;
  273. const int task_percent = 19;
  274. const int percent_per_part = task_percent / enc->num_parts_;
  275. const int final_percent = enc->percent_ + task_percent;
  276. int ok = 0;
  277. size_t vp8_size, pad, riff_size;
  278. int p;
  279. // Partition #0 with header and partition sizes
  280. ok = GeneratePartition0(enc);
  281. if (!ok) return 0;
  282. // Compute VP8 size
  283. vp8_size = VP8_FRAME_HEADER_SIZE +
  284. VP8BitWriterSize(bw) +
  285. 3 * (enc->num_parts_ - 1);
  286. for (p = 0; p < enc->num_parts_; ++p) {
  287. vp8_size += VP8BitWriterSize(enc->parts_ + p);
  288. }
  289. pad = vp8_size & 1;
  290. vp8_size += pad;
  291. // Compute RIFF size
  292. // At the minimum it is: "WEBPVP8 nnnn" + VP8 data size.
  293. riff_size = TAG_SIZE + CHUNK_HEADER_SIZE + vp8_size;
  294. if (IsVP8XNeeded(enc)) { // Add size for: VP8X header + data.
  295. riff_size += CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
  296. }
  297. if (enc->has_alpha_) { // Add size for: ALPH header + data.
  298. const uint32_t padded_alpha_size = enc->alpha_data_size_ +
  299. (enc->alpha_data_size_ & 1);
  300. riff_size += CHUNK_HEADER_SIZE + padded_alpha_size;
  301. }
  302. // Sanity check.
  303. if (riff_size > 0xfffffffeU) {
  304. return WebPEncodingSetError(pic, VP8_ENC_ERROR_FILE_TOO_BIG);
  305. }
  306. // Emit headers and partition #0
  307. {
  308. const uint8_t* const part0 = VP8BitWriterBuf(bw);
  309. const size_t size0 = VP8BitWriterSize(bw);
  310. ok = ok && PutWebPHeaders(enc, size0, vp8_size, riff_size)
  311. && pic->writer(part0, size0, pic)
  312. && EmitPartitionsSize(enc, pic);
  313. VP8BitWriterWipeOut(bw); // will free the internal buffer.
  314. }
  315. // Token partitions
  316. for (p = 0; p < enc->num_parts_; ++p) {
  317. const uint8_t* const buf = VP8BitWriterBuf(enc->parts_ + p);
  318. const size_t size = VP8BitWriterSize(enc->parts_ + p);
  319. if (size)
  320. ok = ok && pic->writer(buf, size, pic);
  321. VP8BitWriterWipeOut(enc->parts_ + p); // will free the internal buffer.
  322. ok = ok && WebPReportProgress(pic, enc->percent_ + percent_per_part,
  323. &enc->percent_);
  324. }
  325. // Padding byte
  326. if (ok && pad) {
  327. ok = PutPaddingByte(pic);
  328. }
  329. enc->coded_size_ = (int)(CHUNK_HEADER_SIZE + riff_size);
  330. ok = ok && WebPReportProgress(pic, final_percent, &enc->percent_);
  331. return ok;
  332. }
  333. //------------------------------------------------------------------------------