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.

455 lines
15KB

  1. /*
  2. * lossless JPEG shared bits
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2003 Alex Beregszaszi
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include "libavutil/pixdesc.h"
  25. #include "libavutil/pixfmt.h"
  26. #include "avcodec.h"
  27. #include "idctdsp.h"
  28. #include "jpegtables.h"
  29. #include "put_bits.h"
  30. #include "mjpegenc.h"
  31. #include "mjpegenc_common.h"
  32. #include "mjpeg.h"
  33. /* table_class: 0 = DC coef, 1 = AC coefs */
  34. static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
  35. const uint8_t *bits_table, const uint8_t *value_table)
  36. {
  37. int n, i;
  38. put_bits(p, 4, table_class);
  39. put_bits(p, 4, table_id);
  40. n = 0;
  41. for(i=1;i<=16;i++) {
  42. n += bits_table[i];
  43. put_bits(p, 8, bits_table[i]);
  44. }
  45. for(i=0;i<n;i++)
  46. put_bits(p, 8, value_table[i]);
  47. return n + 17;
  48. }
  49. static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
  50. ScanTable *intra_scantable,
  51. uint16_t luma_intra_matrix[64],
  52. uint16_t chroma_intra_matrix[64],
  53. int hsample[3])
  54. {
  55. int i, j, size;
  56. uint8_t *ptr;
  57. MpegEncContext *s = NULL;
  58. /* Since avctx->priv_data will point to LJpegEncContext in this case */
  59. if (avctx->codec_id != AV_CODEC_ID_LJPEG)
  60. s = avctx->priv_data;
  61. if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
  62. int matrix_count = 1 + !!memcmp(luma_intra_matrix,
  63. chroma_intra_matrix,
  64. sizeof(luma_intra_matrix[0]) * 64);
  65. if (s && s->force_duplicated_matrix)
  66. matrix_count = 2;
  67. /* quant matrixes */
  68. put_marker(p, DQT);
  69. put_bits(p, 16, 2 + matrix_count * (1 + 64));
  70. put_bits(p, 4, 0); /* 8 bit precision */
  71. put_bits(p, 4, 0); /* table 0 */
  72. for(i=0;i<64;i++) {
  73. j = intra_scantable->permutated[i];
  74. put_bits(p, 8, luma_intra_matrix[j]);
  75. }
  76. if (matrix_count > 1) {
  77. put_bits(p, 4, 0); /* 8 bit precision */
  78. put_bits(p, 4, 1); /* table 1 */
  79. for(i=0;i<64;i++) {
  80. j = intra_scantable->permutated[i];
  81. put_bits(p, 8, chroma_intra_matrix[j]);
  82. }
  83. }
  84. }
  85. if(avctx->active_thread_type & FF_THREAD_SLICE){
  86. put_marker(p, DRI);
  87. put_bits(p, 16, 4);
  88. put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
  89. }
  90. /* huffman table */
  91. put_marker(p, DHT);
  92. flush_put_bits(p);
  93. ptr = put_bits_ptr(p);
  94. put_bits(p, 16, 0); /* patched later */
  95. size = 2;
  96. // Only MJPEG can have a variable Huffman variable. All other
  97. // formats use the default Huffman table.
  98. if (s && s->huffman == HUFFMAN_TABLE_OPTIMAL) {
  99. size += put_huffman_table(p, 0, 0, s->mjpeg_ctx->bits_dc_luminance,
  100. s->mjpeg_ctx->val_dc_luminance);
  101. size += put_huffman_table(p, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,
  102. s->mjpeg_ctx->val_dc_chrominance);
  103. size += put_huffman_table(p, 1, 0, s->mjpeg_ctx->bits_ac_luminance,
  104. s->mjpeg_ctx->val_ac_luminance);
  105. size += put_huffman_table(p, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,
  106. s->mjpeg_ctx->val_ac_chrominance);
  107. } else {
  108. size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
  109. avpriv_mjpeg_val_dc);
  110. size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  111. avpriv_mjpeg_val_dc);
  112. size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  113. avpriv_mjpeg_val_ac_luminance);
  114. size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  115. avpriv_mjpeg_val_ac_chrominance);
  116. }
  117. AV_WB16(ptr, size);
  118. }
  119. static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
  120. {
  121. int size;
  122. uint8_t *ptr;
  123. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
  124. AVRational sar = avctx->sample_aspect_ratio;
  125. if (sar.num > 65535 || sar.den > 65535) {
  126. if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
  127. av_log(avctx, AV_LOG_WARNING,
  128. "Cannot store exact aspect ratio %d:%d\n",
  129. avctx->sample_aspect_ratio.num,
  130. avctx->sample_aspect_ratio.den);
  131. }
  132. /* JFIF header */
  133. put_marker(p, APP0);
  134. put_bits(p, 16, 16);
  135. ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
  136. /* The most significant byte is used for major revisions, the least
  137. * significant byte for minor revisions. Version 1.02 is the current
  138. * released revision. */
  139. put_bits(p, 16, 0x0102);
  140. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  141. put_bits(p, 16, sar.num);
  142. put_bits(p, 16, sar.den);
  143. put_bits(p, 8, 0); /* thumbnail width */
  144. put_bits(p, 8, 0); /* thumbnail height */
  145. }
  146. /* comment */
  147. if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
  148. put_marker(p, COM);
  149. flush_put_bits(p);
  150. ptr = put_bits_ptr(p);
  151. put_bits(p, 16, 0); /* patched later */
  152. ff_put_string(p, LIBAVCODEC_IDENT, 1);
  153. size = strlen(LIBAVCODEC_IDENT)+3;
  154. AV_WB16(ptr, size);
  155. }
  156. if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  157. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  158. avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
  159. || avctx->color_range == AVCOL_RANGE_MPEG) {
  160. put_marker(p, COM);
  161. flush_put_bits(p);
  162. ptr = put_bits_ptr(p);
  163. put_bits(p, 16, 0); /* patched later */
  164. ff_put_string(p, "CS=ITU601", 1);
  165. size = strlen("CS=ITU601")+3;
  166. AV_WB16(ptr, size);
  167. }
  168. }
  169. void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
  170. {
  171. int chroma_h_shift, chroma_v_shift;
  172. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
  173. &chroma_v_shift);
  174. if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
  175. ( avctx->pix_fmt == AV_PIX_FMT_BGR0
  176. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  177. || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
  178. vsample[0] = hsample[0] =
  179. vsample[1] = hsample[1] =
  180. vsample[2] = hsample[2] =
  181. vsample[3] = hsample[3] = 1;
  182. } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
  183. vsample[0] = vsample[1] = vsample[2] = 2;
  184. hsample[0] = hsample[1] = hsample[2] = 1;
  185. } else {
  186. vsample[0] = 2;
  187. vsample[1] = 2 >> chroma_v_shift;
  188. vsample[2] = 2 >> chroma_v_shift;
  189. hsample[0] = 2;
  190. hsample[1] = 2 >> chroma_h_shift;
  191. hsample[2] = 2 >> chroma_h_shift;
  192. }
  193. }
  194. void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
  195. ScanTable *intra_scantable, int pred,
  196. uint16_t luma_intra_matrix[64],
  197. uint16_t chroma_intra_matrix[64])
  198. {
  199. const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
  200. int hsample[4], vsample[4];
  201. int i;
  202. int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
  203. int chroma_matrix = !!memcmp(luma_intra_matrix,
  204. chroma_intra_matrix,
  205. sizeof(luma_intra_matrix[0])*64);
  206. ff_mjpeg_init_hvsample(avctx, hsample, vsample);
  207. put_marker(pb, SOI);
  208. // hack for AMV mjpeg format
  209. if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
  210. jpeg_put_comments(avctx, pb);
  211. jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
  212. switch (avctx->codec_id) {
  213. case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
  214. case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
  215. default: av_assert0(0);
  216. }
  217. put_bits(pb, 16, 17);
  218. if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
  219. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  220. || avctx->pix_fmt == AV_PIX_FMT_BGR24))
  221. put_bits(pb, 8, 9); /* 9 bits/component RCT */
  222. else
  223. put_bits(pb, 8, 8); /* 8 bits/component */
  224. put_bits(pb, 16, avctx->height);
  225. put_bits(pb, 16, avctx->width);
  226. put_bits(pb, 8, components); /* 3 or 4 components */
  227. /* Y component */
  228. put_bits(pb, 8, 1); /* component number */
  229. put_bits(pb, 4, hsample[0]); /* H factor */
  230. put_bits(pb, 4, vsample[0]); /* V factor */
  231. put_bits(pb, 8, 0); /* select matrix */
  232. /* Cb component */
  233. put_bits(pb, 8, 2); /* component number */
  234. put_bits(pb, 4, hsample[1]); /* H factor */
  235. put_bits(pb, 4, vsample[1]); /* V factor */
  236. put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
  237. /* Cr component */
  238. put_bits(pb, 8, 3); /* component number */
  239. put_bits(pb, 4, hsample[2]); /* H factor */
  240. put_bits(pb, 4, vsample[2]); /* V factor */
  241. put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
  242. if (components == 4) {
  243. put_bits(pb, 8, 4); /* component number */
  244. put_bits(pb, 4, hsample[3]); /* H factor */
  245. put_bits(pb, 4, vsample[3]); /* V factor */
  246. put_bits(pb, 8, 0); /* select matrix */
  247. }
  248. /* scan header */
  249. put_marker(pb, SOS);
  250. put_bits(pb, 16, 6 + 2*components); /* length */
  251. put_bits(pb, 8, components); /* 3 components */
  252. /* Y component */
  253. put_bits(pb, 8, 1); /* index */
  254. put_bits(pb, 4, 0); /* DC huffman table index */
  255. put_bits(pb, 4, 0); /* AC huffman table index */
  256. /* Cb component */
  257. put_bits(pb, 8, 2); /* index */
  258. put_bits(pb, 4, 1); /* DC huffman table index */
  259. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  260. /* Cr component */
  261. put_bits(pb, 8, 3); /* index */
  262. put_bits(pb, 4, 1); /* DC huffman table index */
  263. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  264. if (components == 4) {
  265. /* Alpha component */
  266. put_bits(pb, 8, 4); /* index */
  267. put_bits(pb, 4, 0); /* DC huffman table index */
  268. put_bits(pb, 4, 0); /* AC huffman table index */
  269. }
  270. put_bits(pb, 8, lossless ? pred : 0); /* Ss (not used) */
  271. switch (avctx->codec_id) {
  272. case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
  273. case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
  274. default: av_assert0(0);
  275. }
  276. put_bits(pb, 8, 0); /* Ah/Al (not used) */
  277. end:
  278. if (!lossless) {
  279. MpegEncContext *s = avctx->priv_data;
  280. av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
  281. s->esc_pos = put_bytes_count(pb, 0);
  282. for(i=1; i<s->slice_context_count; i++)
  283. s->thread_context[i]->esc_pos = 0;
  284. }
  285. }
  286. void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
  287. {
  288. int size;
  289. int i, ff_count;
  290. uint8_t *buf = pb->buf + start;
  291. int align= (-(size_t)(buf))&3;
  292. int pad = (-put_bits_count(pb))&7;
  293. if (pad)
  294. put_bits(pb, pad, (1<<pad)-1);
  295. flush_put_bits(pb);
  296. size = put_bytes_output(pb) - start;
  297. ff_count=0;
  298. for(i=0; i<size && i<align; i++){
  299. if(buf[i]==0xFF) ff_count++;
  300. }
  301. for(; i<size-15; i+=16){
  302. int acc, v;
  303. v= *(uint32_t*)(&buf[i]);
  304. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  305. v= *(uint32_t*)(&buf[i+4]);
  306. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  307. v= *(uint32_t*)(&buf[i+8]);
  308. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  309. v= *(uint32_t*)(&buf[i+12]);
  310. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  311. acc>>=4;
  312. acc+= (acc>>16);
  313. acc+= (acc>>8);
  314. ff_count+= acc&0xFF;
  315. }
  316. for(; i<size; i++){
  317. if(buf[i]==0xFF) ff_count++;
  318. }
  319. if(ff_count==0) return;
  320. flush_put_bits(pb);
  321. skip_put_bytes(pb, ff_count);
  322. for(i=size-1; ff_count; i--){
  323. int v= buf[i];
  324. if(v==0xFF){
  325. buf[i+ff_count]= 0;
  326. ff_count--;
  327. }
  328. buf[i+ff_count]= v;
  329. }
  330. }
  331. /* isn't this function nicer than the one in the libjpeg ? */
  332. void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
  333. const uint8_t *bits_table,
  334. const uint8_t *val_table)
  335. {
  336. int k, code;
  337. k = 0;
  338. code = 0;
  339. for (int i = 1; i <= 16; i++) {
  340. int nb = bits_table[i];
  341. for (int j = 0; j < nb; j++) {
  342. int sym = val_table[k++];
  343. huff_size[sym] = i;
  344. huff_code[sym] = code;
  345. code++;
  346. }
  347. code <<= 1;
  348. }
  349. }
  350. void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
  351. {
  352. av_assert1((header_bits & 7) == 0);
  353. put_marker(pb, EOI);
  354. }
  355. void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
  356. uint8_t *huff_size, uint16_t *huff_code)
  357. {
  358. int mant, nbits;
  359. if (val == 0) {
  360. put_bits(pb, huff_size[0], huff_code[0]);
  361. } else {
  362. mant = val;
  363. if (val < 0) {
  364. val = -val;
  365. mant--;
  366. }
  367. nbits= av_log2_16bit(val) + 1;
  368. put_bits(pb, huff_size[nbits], huff_code[nbits]);
  369. put_sbits(pb, nbits, mant);
  370. }
  371. }
  372. int ff_mjpeg_encode_check_pix_fmt(AVCodecContext *avctx)
  373. {
  374. if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
  375. avctx->color_range != AVCOL_RANGE_JPEG &&
  376. (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  377. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  378. avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
  379. avctx->color_range == AVCOL_RANGE_MPEG)) {
  380. av_log(avctx, AV_LOG_ERROR,
  381. "Non full-range YUV is non-standard, set strict_std_compliance "
  382. "to at most unofficial to use it.\n");
  383. return AVERROR(EINVAL);
  384. }
  385. return 0;
  386. }