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.

318 lines
9.1KB

  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. put_bits(p, 16, 0x0201); /* v 1.02 */
  86. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  87. put_bits(p, 16, avctx->sample_aspect_ratio.num);
  88. put_bits(p, 16, avctx->sample_aspect_ratio.den);
  89. put_bits(p, 8, 0); /* thumbnail width */
  90. put_bits(p, 8, 0); /* thumbnail height */
  91. }
  92. /* comment */
  93. if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
  94. put_marker(p, COM);
  95. flush_put_bits(p);
  96. ptr = put_bits_ptr(p);
  97. put_bits(p, 16, 0); /* patched later */
  98. avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
  99. size = strlen(LIBAVCODEC_IDENT)+3;
  100. AV_WB16(ptr, size);
  101. }
  102. if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  103. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  104. avctx->pix_fmt == AV_PIX_FMT_YUV444P) {
  105. put_marker(p, COM);
  106. flush_put_bits(p);
  107. ptr = put_bits_ptr(p);
  108. put_bits(p, 16, 0); /* patched later */
  109. avpriv_put_string(p, "CS=ITU601", 1);
  110. size = strlen("CS=ITU601")+3;
  111. AV_WB16(ptr, size);
  112. }
  113. }
  114. void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
  115. ScanTable *intra_scantable,
  116. uint16_t intra_matrix[64])
  117. {
  118. int chroma_h_shift, chroma_v_shift;
  119. const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG;
  120. int hsample[3], vsample[3];
  121. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
  122. &chroma_v_shift);
  123. if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
  124. avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  125. vsample[0] = hsample[0] =
  126. vsample[1] = hsample[1] =
  127. vsample[2] = hsample[2] = 1;
  128. } else {
  129. vsample[0] = 2;
  130. vsample[1] = 2 >> chroma_v_shift;
  131. vsample[2] = 2 >> chroma_v_shift;
  132. hsample[0] = 2;
  133. hsample[1] = 2 >> chroma_h_shift;
  134. hsample[2] = 2 >> chroma_h_shift;
  135. }
  136. put_marker(pb, SOI);
  137. jpeg_put_comments(avctx, pb);
  138. jpeg_table_header(pb, intra_scantable, intra_matrix);
  139. switch (avctx->codec_id) {
  140. case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
  141. case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
  142. default: assert(0);
  143. }
  144. put_bits(pb, 16, 17);
  145. if (lossless && avctx->pix_fmt == AV_PIX_FMT_BGR24)
  146. put_bits(pb, 8, 9); /* 9 bits/component RCT */
  147. else
  148. put_bits(pb, 8, 8); /* 8 bits/component */
  149. put_bits(pb, 16, avctx->height);
  150. put_bits(pb, 16, avctx->width);
  151. put_bits(pb, 8, 3); /* 3 components */
  152. /* Y component */
  153. put_bits(pb, 8, 1); /* component number */
  154. put_bits(pb, 4, hsample[0]); /* H factor */
  155. put_bits(pb, 4, vsample[0]); /* V factor */
  156. put_bits(pb, 8, 0); /* select matrix */
  157. /* Cb component */
  158. put_bits(pb, 8, 2); /* component number */
  159. put_bits(pb, 4, hsample[1]); /* H factor */
  160. put_bits(pb, 4, vsample[1]); /* V factor */
  161. put_bits(pb, 8, 0); /* select matrix */
  162. /* Cr component */
  163. put_bits(pb, 8, 3); /* component number */
  164. put_bits(pb, 4, hsample[2]); /* H factor */
  165. put_bits(pb, 4, vsample[2]); /* V factor */
  166. put_bits(pb, 8, 0); /* select matrix */
  167. /* scan header */
  168. put_marker(pb, SOS);
  169. put_bits(pb, 16, 12); /* length */
  170. put_bits(pb, 8, 3); /* 3 components */
  171. /* Y component */
  172. put_bits(pb, 8, 1); /* index */
  173. put_bits(pb, 4, 0); /* DC huffman table index */
  174. put_bits(pb, 4, 0); /* AC huffman table index */
  175. /* Cb component */
  176. put_bits(pb, 8, 2); /* index */
  177. put_bits(pb, 4, 1); /* DC huffman table index */
  178. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  179. /* Cr component */
  180. put_bits(pb, 8, 3); /* index */
  181. put_bits(pb, 4, 1); /* DC huffman table index */
  182. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  183. put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
  184. switch (avctx->codec_id) {
  185. case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
  186. case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
  187. default: assert(0);
  188. }
  189. put_bits(pb, 8, 0); /* Ah/Al (not used) */
  190. }
  191. static void escape_FF(PutBitContext *pb, int start)
  192. {
  193. int size = put_bits_count(pb) - start * 8;
  194. int i, ff_count;
  195. uint8_t *buf = pb->buf + start;
  196. int align= (-(size_t)(buf))&3;
  197. assert((size&7) == 0);
  198. size >>= 3;
  199. ff_count=0;
  200. for(i=0; i<size && i<align; i++){
  201. if(buf[i]==0xFF) ff_count++;
  202. }
  203. for(; i<size-15; i+=16){
  204. int acc, v;
  205. v= *(uint32_t*)(&buf[i]);
  206. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  207. v= *(uint32_t*)(&buf[i+4]);
  208. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  209. v= *(uint32_t*)(&buf[i+8]);
  210. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  211. v= *(uint32_t*)(&buf[i+12]);
  212. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  213. acc>>=4;
  214. acc+= (acc>>16);
  215. acc+= (acc>>8);
  216. ff_count+= acc&0xFF;
  217. }
  218. for(; i<size; i++){
  219. if(buf[i]==0xFF) ff_count++;
  220. }
  221. if(ff_count==0) return;
  222. flush_put_bits(pb);
  223. skip_put_bytes(pb, ff_count);
  224. for(i=size-1; ff_count; i--){
  225. int v= buf[i];
  226. if(v==0xFF){
  227. buf[i+ff_count]= 0;
  228. ff_count--;
  229. }
  230. buf[i+ff_count]= v;
  231. }
  232. }
  233. void ff_mjpeg_encode_stuffing(PutBitContext * pbc)
  234. {
  235. int length;
  236. length= (-put_bits_count(pbc))&7;
  237. if(length) put_bits(pbc, length, (1<<length)-1);
  238. }
  239. void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
  240. {
  241. ff_mjpeg_encode_stuffing(pb);
  242. flush_put_bits(pb);
  243. assert((header_bits & 7) == 0);
  244. escape_FF(pb, header_bits >> 3);
  245. put_marker(pb, EOI);
  246. }
  247. void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
  248. uint8_t *huff_size, uint16_t *huff_code)
  249. {
  250. int mant, nbits;
  251. if (val == 0) {
  252. put_bits(pb, huff_size[0], huff_code[0]);
  253. } else {
  254. mant = val;
  255. if (val < 0) {
  256. val = -val;
  257. mant--;
  258. }
  259. nbits= av_log2_16bit(val) + 1;
  260. put_bits(pb, huff_size[nbits], huff_code[nbits]);
  261. put_sbits(pb, nbits, mant);
  262. }
  263. }