You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
9.3KB

  1. /*
  2. * lossless JPEG shared bits
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include "libavutil/common.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libavutil/pixfmt.h"
  25. #include "avcodec.h"
  26. #include "idctdsp.h"
  27. #include "put_bits.h"
  28. #include "mjpegenc_common.h"
  29. #include "mjpeg.h"
  30. /* table_class: 0 = DC coef, 1 = AC coefs */
  31. static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
  32. const uint8_t *bits_table, const uint8_t *value_table)
  33. {
  34. int n, i;
  35. put_bits(p, 4, table_class);
  36. put_bits(p, 4, table_id);
  37. n = 0;
  38. for(i=1;i<=16;i++) {
  39. n += bits_table[i];
  40. put_bits(p, 8, bits_table[i]);
  41. }
  42. for(i=0;i<n;i++)
  43. put_bits(p, 8, value_table[i]);
  44. return n + 17;
  45. }
  46. static void jpeg_table_header(PutBitContext *p, ScanTable *intra_scantable,
  47. uint16_t intra_matrix[64])
  48. {
  49. int i, j, size;
  50. uint8_t *ptr;
  51. /* quant matrixes */
  52. put_marker(p, DQT);
  53. put_bits(p, 16, 2 + 1 * (1 + 64));
  54. put_bits(p, 4, 0); /* 8 bit precision */
  55. put_bits(p, 4, 0); /* table 0 */
  56. for(i=0;i<64;i++) {
  57. j = intra_scantable->permutated[i];
  58. put_bits(p, 8, intra_matrix[j]);
  59. }
  60. /* huffman table */
  61. put_marker(p, DHT);
  62. flush_put_bits(p);
  63. ptr = put_bits_ptr(p);
  64. put_bits(p, 16, 0); /* patched later */
  65. size = 2;
  66. size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
  67. avpriv_mjpeg_val_dc);
  68. size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  69. avpriv_mjpeg_val_dc);
  70. size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  71. avpriv_mjpeg_val_ac_luminance);
  72. size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  73. avpriv_mjpeg_val_ac_chrominance);
  74. AV_WB16(ptr, size);
  75. }
  76. static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
  77. {
  78. int size;
  79. uint8_t *ptr;
  80. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
  81. /* JFIF header */
  82. put_marker(p, APP0);
  83. put_bits(p, 16, 16);
  84. avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
  85. /* The most significant byte is used for major revisions, the least
  86. * significant byte for minor revisions. Version 1.02 is the current
  87. * released revision. */
  88. put_bits(p, 16, 0x0102);
  89. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  90. put_bits(p, 16, avctx->sample_aspect_ratio.num);
  91. put_bits(p, 16, avctx->sample_aspect_ratio.den);
  92. put_bits(p, 8, 0); /* thumbnail width */
  93. put_bits(p, 8, 0); /* thumbnail height */
  94. }
  95. /* comment */
  96. if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
  97. put_marker(p, COM);
  98. flush_put_bits(p);
  99. ptr = put_bits_ptr(p);
  100. put_bits(p, 16, 0); /* patched later */
  101. avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
  102. size = strlen(LIBAVCODEC_IDENT)+3;
  103. AV_WB16(ptr, size);
  104. }
  105. if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  106. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  107. avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
  108. put_marker(p, COM);
  109. flush_put_bits(p);
  110. ptr = put_bits_ptr(p);
  111. put_bits(p, 16, 0); /* patched later */
  112. avpriv_put_string(p, "CS=ITU601", 1);
  113. size = strlen("CS=ITU601")+3;
  114. AV_WB16(ptr, size);
  115. }
  116. }
  117. void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
  118. ScanTable *intra_scantable,
  119. uint16_t intra_matrix[64])
  120. {
  121. int chroma_h_shift, chroma_v_shift;
  122. const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG;
  123. int hsample[3], vsample[3];
  124. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
  125. &chroma_v_shift);
  126. if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
  127. avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  128. vsample[0] = hsample[0] =
  129. vsample[1] = hsample[1] =
  130. vsample[2] = hsample[2] = 1;
  131. } else {
  132. vsample[0] = 2;
  133. vsample[1] = 2 >> chroma_v_shift;
  134. vsample[2] = 2 >> chroma_v_shift;
  135. hsample[0] = 2;
  136. hsample[1] = 2 >> chroma_h_shift;
  137. hsample[2] = 2 >> chroma_h_shift;
  138. }
  139. put_marker(pb, SOI);
  140. jpeg_put_comments(avctx, pb);
  141. jpeg_table_header(pb, intra_scantable, intra_matrix);
  142. switch (avctx->codec_id) {
  143. case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
  144. case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
  145. default: assert(0);
  146. }
  147. put_bits(pb, 16, 17);
  148. if (lossless && avctx->pix_fmt == AV_PIX_FMT_BGR24)
  149. put_bits(pb, 8, 9); /* 9 bits/component RCT */
  150. else
  151. put_bits(pb, 8, 8); /* 8 bits/component */
  152. put_bits(pb, 16, avctx->height);
  153. put_bits(pb, 16, avctx->width);
  154. put_bits(pb, 8, 3); /* 3 components */
  155. /* Y component */
  156. put_bits(pb, 8, 1); /* component number */
  157. put_bits(pb, 4, hsample[0]); /* H factor */
  158. put_bits(pb, 4, vsample[0]); /* V factor */
  159. put_bits(pb, 8, 0); /* select matrix */
  160. /* Cb component */
  161. put_bits(pb, 8, 2); /* component number */
  162. put_bits(pb, 4, hsample[1]); /* H factor */
  163. put_bits(pb, 4, vsample[1]); /* V factor */
  164. put_bits(pb, 8, 0); /* select matrix */
  165. /* Cr component */
  166. put_bits(pb, 8, 3); /* component number */
  167. put_bits(pb, 4, hsample[2]); /* H factor */
  168. put_bits(pb, 4, vsample[2]); /* V factor */
  169. put_bits(pb, 8, 0); /* select matrix */
  170. /* scan header */
  171. put_marker(pb, SOS);
  172. put_bits(pb, 16, 12); /* length */
  173. put_bits(pb, 8, 3); /* 3 components */
  174. /* Y component */
  175. put_bits(pb, 8, 1); /* index */
  176. put_bits(pb, 4, 0); /* DC huffman table index */
  177. put_bits(pb, 4, 0); /* AC huffman table index */
  178. /* Cb component */
  179. put_bits(pb, 8, 2); /* index */
  180. put_bits(pb, 4, 1); /* DC huffman table index */
  181. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  182. /* Cr component */
  183. put_bits(pb, 8, 3); /* index */
  184. put_bits(pb, 4, 1); /* DC huffman table index */
  185. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  186. put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
  187. switch (avctx->codec_id) {
  188. case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
  189. case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
  190. default: assert(0);
  191. }
  192. put_bits(pb, 8, 0); /* Ah/Al (not used) */
  193. }
  194. static void escape_FF(PutBitContext *pb, int start)
  195. {
  196. int size = put_bits_count(pb) - start * 8;
  197. int i, ff_count;
  198. uint8_t *buf = pb->buf + start;
  199. int align= (-(size_t)(buf))&3;
  200. assert((size&7) == 0);
  201. size >>= 3;
  202. ff_count=0;
  203. for(i=0; i<size && i<align; i++){
  204. if(buf[i]==0xFF) ff_count++;
  205. }
  206. for(; i<size-15; i+=16){
  207. int acc, v;
  208. v= *(uint32_t*)(&buf[i]);
  209. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  210. v= *(uint32_t*)(&buf[i+4]);
  211. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  212. v= *(uint32_t*)(&buf[i+8]);
  213. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  214. v= *(uint32_t*)(&buf[i+12]);
  215. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  216. acc>>=4;
  217. acc+= (acc>>16);
  218. acc+= (acc>>8);
  219. ff_count+= acc&0xFF;
  220. }
  221. for(; i<size; i++){
  222. if(buf[i]==0xFF) ff_count++;
  223. }
  224. if(ff_count==0) return;
  225. flush_put_bits(pb);
  226. skip_put_bytes(pb, ff_count);
  227. for(i=size-1; ff_count; i--){
  228. int v= buf[i];
  229. if(v==0xFF){
  230. buf[i+ff_count]= 0;
  231. ff_count--;
  232. }
  233. buf[i+ff_count]= v;
  234. }
  235. }
  236. void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
  237. {
  238. int length;
  239. length= (-put_bits_count(pbc))&7;
  240. if(length) put_bits(pbc, length, (1<<length)-1);
  241. }
  242. void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
  243. {
  244. ff_mjpeg_encode_stuffing(pb);
  245. flush_put_bits(pb);
  246. assert((header_bits & 7) == 0);
  247. escape_FF(pb, header_bits >> 3);
  248. put_marker(pb, EOI);
  249. }
  250. void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
  251. uint8_t *huff_size, uint16_t *huff_code)
  252. {
  253. int mant, nbits;
  254. if (val == 0) {
  255. put_bits(pb, huff_size[0], huff_code[0]);
  256. } else {
  257. mant = val;
  258. if (val < 0) {
  259. val = -val;
  260. mant--;
  261. }
  262. nbits= av_log2_16bit(val) + 1;
  263. put_bits(pb, huff_size[nbits], huff_code[nbits]);
  264. put_sbits(pb, nbits, mant);
  265. }
  266. }