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.

323 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 "jpegtables.h"
  28. #include "put_bits.h"
  29. #include "mjpegenc.h"
  30. #include "mjpegenc_common.h"
  31. #include "mjpeg.h"
  32. /* table_class: 0 = DC coef, 1 = AC coefs */
  33. static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
  34. const uint8_t *bits_table, const uint8_t *value_table)
  35. {
  36. int n, i;
  37. put_bits(p, 4, table_class);
  38. put_bits(p, 4, table_id);
  39. n = 0;
  40. for(i=1;i<=16;i++) {
  41. n += bits_table[i];
  42. put_bits(p, 8, bits_table[i]);
  43. }
  44. for(i=0;i<n;i++)
  45. put_bits(p, 8, value_table[i]);
  46. return n + 17;
  47. }
  48. static void jpeg_table_header(PutBitContext *p, ScanTable *intra_scantable,
  49. uint16_t intra_matrix[64])
  50. {
  51. int i, j, size;
  52. uint8_t *ptr;
  53. /* quant matrixes */
  54. put_marker(p, DQT);
  55. put_bits(p, 16, 2 + 1 * (1 + 64));
  56. put_bits(p, 4, 0); /* 8 bit precision */
  57. put_bits(p, 4, 0); /* table 0 */
  58. for(i=0;i<64;i++) {
  59. j = intra_scantable->permutated[i];
  60. put_bits(p, 8, intra_matrix[j]);
  61. }
  62. /* huffman table */
  63. put_marker(p, DHT);
  64. flush_put_bits(p);
  65. ptr = put_bits_ptr(p);
  66. put_bits(p, 16, 0); /* patched later */
  67. size = 2;
  68. size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
  69. avpriv_mjpeg_val_dc);
  70. size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  71. avpriv_mjpeg_val_dc);
  72. size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  73. avpriv_mjpeg_val_ac_luminance);
  74. size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  75. avpriv_mjpeg_val_ac_chrominance);
  76. AV_WB16(ptr, size);
  77. }
  78. static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
  79. {
  80. int size;
  81. uint8_t *ptr;
  82. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
  83. /* JFIF header */
  84. put_marker(p, APP0);
  85. put_bits(p, 16, 16);
  86. avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
  87. /* The most significant byte is used for major revisions, the least
  88. * significant byte for minor revisions. Version 1.02 is the current
  89. * released revision. */
  90. put_bits(p, 16, 0x0102);
  91. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  92. put_bits(p, 16, avctx->sample_aspect_ratio.num);
  93. put_bits(p, 16, avctx->sample_aspect_ratio.den);
  94. put_bits(p, 8, 0); /* thumbnail width */
  95. put_bits(p, 8, 0); /* thumbnail height */
  96. }
  97. /* comment */
  98. if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
  99. put_marker(p, COM);
  100. flush_put_bits(p);
  101. ptr = put_bits_ptr(p);
  102. put_bits(p, 16, 0); /* patched later */
  103. avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
  104. size = strlen(LIBAVCODEC_IDENT)+3;
  105. AV_WB16(ptr, size);
  106. }
  107. if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  108. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  109. avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
  110. put_marker(p, COM);
  111. flush_put_bits(p);
  112. ptr = put_bits_ptr(p);
  113. put_bits(p, 16, 0); /* patched later */
  114. avpriv_put_string(p, "CS=ITU601", 1);
  115. size = strlen("CS=ITU601")+3;
  116. AV_WB16(ptr, size);
  117. }
  118. }
  119. void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
  120. ScanTable *intra_scantable, int pred,
  121. uint16_t intra_matrix[64])
  122. {
  123. int chroma_h_shift, chroma_v_shift;
  124. const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG;
  125. int hsample[3], vsample[3];
  126. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
  127. &chroma_v_shift);
  128. if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
  129. avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  130. vsample[0] = hsample[0] =
  131. vsample[1] = hsample[1] =
  132. vsample[2] = hsample[2] = 1;
  133. } else {
  134. vsample[0] = 2;
  135. vsample[1] = 2 >> chroma_v_shift;
  136. vsample[2] = 2 >> chroma_v_shift;
  137. hsample[0] = 2;
  138. hsample[1] = 2 >> chroma_h_shift;
  139. hsample[2] = 2 >> chroma_h_shift;
  140. }
  141. put_marker(pb, SOI);
  142. jpeg_put_comments(avctx, pb);
  143. jpeg_table_header(pb, intra_scantable, intra_matrix);
  144. switch (avctx->codec_id) {
  145. case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
  146. case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
  147. default: assert(0);
  148. }
  149. put_bits(pb, 16, 17);
  150. if (lossless && avctx->pix_fmt == AV_PIX_FMT_BGR24)
  151. put_bits(pb, 8, 9); /* 9 bits/component RCT */
  152. else
  153. put_bits(pb, 8, 8); /* 8 bits/component */
  154. put_bits(pb, 16, avctx->height);
  155. put_bits(pb, 16, avctx->width);
  156. put_bits(pb, 8, 3); /* 3 components */
  157. /* Y component */
  158. put_bits(pb, 8, 1); /* component number */
  159. put_bits(pb, 4, hsample[0]); /* H factor */
  160. put_bits(pb, 4, vsample[0]); /* V factor */
  161. put_bits(pb, 8, 0); /* select matrix */
  162. /* Cb component */
  163. put_bits(pb, 8, 2); /* component number */
  164. put_bits(pb, 4, hsample[1]); /* H factor */
  165. put_bits(pb, 4, vsample[1]); /* V factor */
  166. put_bits(pb, 8, 0); /* select matrix */
  167. /* Cr component */
  168. put_bits(pb, 8, 3); /* component number */
  169. put_bits(pb, 4, hsample[2]); /* H factor */
  170. put_bits(pb, 4, vsample[2]); /* V factor */
  171. put_bits(pb, 8, 0); /* select matrix */
  172. /* scan header */
  173. put_marker(pb, SOS);
  174. put_bits(pb, 16, 12); /* length */
  175. put_bits(pb, 8, 3); /* 3 components */
  176. /* Y component */
  177. put_bits(pb, 8, 1); /* index */
  178. put_bits(pb, 4, 0); /* DC huffman table index */
  179. put_bits(pb, 4, 0); /* AC huffman table index */
  180. /* Cb component */
  181. put_bits(pb, 8, 2); /* index */
  182. put_bits(pb, 4, 1); /* DC huffman table index */
  183. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  184. /* Cr component */
  185. put_bits(pb, 8, 3); /* index */
  186. put_bits(pb, 4, 1); /* DC huffman table index */
  187. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  188. put_bits(pb, 8, lossless ? pred : 0); /* Ss (not used) */
  189. switch (avctx->codec_id) {
  190. case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
  191. case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
  192. default: assert(0);
  193. }
  194. put_bits(pb, 8, 0); /* Ah/Al (not used) */
  195. }
  196. static void escape_FF(PutBitContext *pb, int start)
  197. {
  198. int size = put_bits_count(pb) - start * 8;
  199. int i, ff_count;
  200. uint8_t *buf = pb->buf + start;
  201. int align= (-(size_t)(buf))&3;
  202. assert((size&7) == 0);
  203. size >>= 3;
  204. ff_count=0;
  205. for(i=0; i<size && i<align; i++){
  206. if(buf[i]==0xFF) ff_count++;
  207. }
  208. for(; i<size-15; i+=16){
  209. int acc, v;
  210. v= *(uint32_t*)(&buf[i]);
  211. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  212. v= *(uint32_t*)(&buf[i+4]);
  213. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  214. v= *(uint32_t*)(&buf[i+8]);
  215. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  216. v= *(uint32_t*)(&buf[i+12]);
  217. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  218. acc>>=4;
  219. acc+= (acc>>16);
  220. acc+= (acc>>8);
  221. ff_count+= acc&0xFF;
  222. }
  223. for(; i<size; i++){
  224. if(buf[i]==0xFF) ff_count++;
  225. }
  226. if(ff_count==0) return;
  227. flush_put_bits(pb);
  228. skip_put_bytes(pb, ff_count);
  229. for(i=size-1; ff_count; i--){
  230. int v= buf[i];
  231. if(v==0xFF){
  232. buf[i+ff_count]= 0;
  233. ff_count--;
  234. }
  235. buf[i+ff_count]= v;
  236. }
  237. }
  238. void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
  239. {
  240. int length;
  241. length= (-put_bits_count(pbc))&7;
  242. if(length) put_bits(pbc, length, (1<<length)-1);
  243. }
  244. void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
  245. {
  246. ff_mjpeg_encode_stuffing(pb);
  247. flush_put_bits(pb);
  248. assert((header_bits & 7) == 0);
  249. escape_FF(pb, header_bits >> 3);
  250. put_marker(pb, EOI);
  251. }
  252. void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
  253. uint8_t *huff_size, uint16_t *huff_code)
  254. {
  255. int mant, nbits;
  256. if (val == 0) {
  257. put_bits(pb, huff_size[0], huff_code[0]);
  258. } else {
  259. mant = val;
  260. if (val < 0) {
  261. val = -val;
  262. mant--;
  263. }
  264. nbits= av_log2_16bit(val) + 1;
  265. put_bits(pb, huff_size[nbits], huff_code[nbits]);
  266. put_sbits(pb, nbits, mant);
  267. }
  268. }