mjpegenc_common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. #include "version.h"
  34. /* table_class: 0 = DC coef, 1 = AC coefs */
  35. static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
  36. const uint8_t *bits_table, const uint8_t *value_table)
  37. {
  38. int n = 0;
  39. put_bits(p, 4, table_class);
  40. put_bits(p, 4, table_id);
  41. for (int i = 1; i <= 16; i++) {
  42. n += bits_table[i];
  43. put_bits(p, 8, bits_table[i]);
  44. }
  45. for (int 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. const MJpegContext *m,
  51. const uint8_t intra_matrix_permutation[64],
  52. const uint16_t luma_intra_matrix[64],
  53. const uint16_t chroma_intra_matrix[64],
  54. int hsample[3], int use_slices, int matrices_differ)
  55. {
  56. int size;
  57. uint8_t *ptr;
  58. if (m) {
  59. int matrix_count = 1 + matrices_differ;
  60. if (m->force_duplicated_matrix)
  61. matrix_count = 2;
  62. /* quant matrixes */
  63. put_marker(p, DQT);
  64. put_bits(p, 16, 2 + matrix_count * (1 + 64));
  65. put_bits(p, 4, 0); /* 8 bit precision */
  66. put_bits(p, 4, 0); /* table 0 */
  67. for (int i = 0; i < 64; i++) {
  68. uint8_t j = intra_matrix_permutation[i];
  69. put_bits(p, 8, luma_intra_matrix[j]);
  70. }
  71. if (matrix_count > 1) {
  72. put_bits(p, 4, 0); /* 8 bit precision */
  73. put_bits(p, 4, 1); /* table 1 */
  74. for (int i = 0; i < 64; i++) {
  75. uint8_t j = intra_matrix_permutation[i];
  76. put_bits(p, 8, chroma_intra_matrix[j]);
  77. }
  78. }
  79. }
  80. if (use_slices) {
  81. put_marker(p, DRI);
  82. put_bits(p, 16, 4);
  83. put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
  84. }
  85. /* huffman table */
  86. put_marker(p, DHT);
  87. flush_put_bits(p);
  88. ptr = put_bits_ptr(p);
  89. put_bits(p, 16, 0); /* patched later */
  90. size = 2;
  91. // Only MJPEG can have a variable Huffman variable. All other
  92. // formats use the default Huffman table.
  93. if (m && m->huffman == HUFFMAN_TABLE_OPTIMAL) {
  94. size += put_huffman_table(p, 0, 0, m->bits_dc_luminance,
  95. m->val_dc_luminance);
  96. size += put_huffman_table(p, 0, 1, m->bits_dc_chrominance,
  97. m->val_dc_chrominance);
  98. size += put_huffman_table(p, 1, 0, m->bits_ac_luminance,
  99. m->val_ac_luminance);
  100. size += put_huffman_table(p, 1, 1, m->bits_ac_chrominance,
  101. m->val_ac_chrominance);
  102. } else {
  103. size += put_huffman_table(p, 0, 0, ff_mjpeg_bits_dc_luminance,
  104. ff_mjpeg_val_dc);
  105. size += put_huffman_table(p, 0, 1, ff_mjpeg_bits_dc_chrominance,
  106. ff_mjpeg_val_dc);
  107. size += put_huffman_table(p, 1, 0, ff_mjpeg_bits_ac_luminance,
  108. ff_mjpeg_val_ac_luminance);
  109. size += put_huffman_table(p, 1, 1, ff_mjpeg_bits_ac_chrominance,
  110. ff_mjpeg_val_ac_chrominance);
  111. }
  112. AV_WB16(ptr, size);
  113. }
  114. enum {
  115. ICC_HDR_SIZE = 16, /* ICC_PROFILE\0 tag + 4 bytes */
  116. ICC_CHUNK_SIZE = UINT16_MAX - ICC_HDR_SIZE,
  117. ICC_MAX_CHUNKS = UINT8_MAX,
  118. };
  119. int ff_mjpeg_add_icc_profile_size(AVCodecContext *avctx, const AVFrame *frame,
  120. size_t *max_pkt_size)
  121. {
  122. const AVFrameSideData *sd;
  123. size_t new_pkt_size;
  124. int nb_chunks;
  125. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
  126. if (!sd || !sd->size)
  127. return 0;
  128. if (sd->size > ICC_MAX_CHUNKS * ICC_CHUNK_SIZE) {
  129. av_log(avctx, AV_LOG_ERROR, "Cannot store %"SIZE_SPECIFIER" byte ICC "
  130. "profile: too large for JPEG\n",
  131. sd->size);
  132. return AVERROR_INVALIDDATA;
  133. }
  134. nb_chunks = (sd->size + ICC_CHUNK_SIZE - 1) / ICC_CHUNK_SIZE;
  135. new_pkt_size = *max_pkt_size + nb_chunks * (UINT16_MAX + 2 /* APP2 marker */);
  136. if (new_pkt_size < *max_pkt_size) /* overflow */
  137. return AVERROR_INVALIDDATA;
  138. *max_pkt_size = new_pkt_size;
  139. return 0;
  140. }
  141. static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p,
  142. const AVFrame *frame)
  143. {
  144. const AVFrameSideData *sd = NULL;
  145. int size;
  146. uint8_t *ptr;
  147. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
  148. AVRational sar = avctx->sample_aspect_ratio;
  149. if (sar.num > 65535 || sar.den > 65535) {
  150. if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
  151. av_log(avctx, AV_LOG_WARNING,
  152. "Cannot store exact aspect ratio %d:%d\n",
  153. avctx->sample_aspect_ratio.num,
  154. avctx->sample_aspect_ratio.den);
  155. }
  156. /* JFIF header */
  157. put_marker(p, APP0);
  158. put_bits(p, 16, 16);
  159. ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
  160. /* The most significant byte is used for major revisions, the least
  161. * significant byte for minor revisions. Version 1.02 is the current
  162. * released revision. */
  163. put_bits(p, 16, 0x0102);
  164. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  165. put_bits(p, 16, sar.num);
  166. put_bits(p, 16, sar.den);
  167. put_bits(p, 8, 0); /* thumbnail width */
  168. put_bits(p, 8, 0); /* thumbnail height */
  169. }
  170. /* ICC profile */
  171. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
  172. if (sd && sd->size) {
  173. const int nb_chunks = (sd->size + ICC_CHUNK_SIZE - 1) / ICC_CHUNK_SIZE;
  174. const uint8_t *data = sd->data;
  175. size_t remaining = sd->size;
  176. /* must already be checked by the packat allocation code */
  177. av_assert0(remaining <= ICC_MAX_CHUNKS * ICC_CHUNK_SIZE);
  178. flush_put_bits(p);
  179. for (int i = 0; i < nb_chunks; i++) {
  180. size = FFMIN(remaining, ICC_CHUNK_SIZE);
  181. av_assert1(size > 0);
  182. ptr = put_bits_ptr(p);
  183. ptr[0] = 0xff; /* chunk marker, not part of ICC_HDR_SIZE */
  184. ptr[1] = APP2;
  185. AV_WB16(ptr+2, size + ICC_HDR_SIZE);
  186. AV_WL32(ptr+4, MKTAG('I','C','C','_'));
  187. AV_WL32(ptr+8, MKTAG('P','R','O','F'));
  188. AV_WL32(ptr+12, MKTAG('I','L','E','\0'));
  189. ptr[16] = i+1;
  190. ptr[17] = nb_chunks;
  191. memcpy(&ptr[18], data, size);
  192. skip_put_bytes(p, size + ICC_HDR_SIZE + 2);
  193. remaining -= size;
  194. data += size;
  195. }
  196. av_assert1(!remaining);
  197. }
  198. /* comment */
  199. if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
  200. put_marker(p, COM);
  201. flush_put_bits(p);
  202. ptr = put_bits_ptr(p);
  203. put_bits(p, 16, 0); /* patched later */
  204. ff_put_string(p, LIBAVCODEC_IDENT, 1);
  205. size = strlen(LIBAVCODEC_IDENT)+3;
  206. AV_WB16(ptr, size);
  207. }
  208. if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  209. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  210. avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
  211. || avctx->color_range == AVCOL_RANGE_MPEG) {
  212. put_marker(p, COM);
  213. flush_put_bits(p);
  214. ptr = put_bits_ptr(p);
  215. put_bits(p, 16, 0); /* patched later */
  216. ff_put_string(p, "CS=ITU601", 1);
  217. size = strlen("CS=ITU601")+3;
  218. AV_WB16(ptr, size);
  219. }
  220. }
  221. void ff_mjpeg_init_hvsample(const AVCodecContext *avctx, int hsample[4], int vsample[4])
  222. {
  223. if (avctx->codec_id == AV_CODEC_ID_LJPEG &&
  224. ( avctx->pix_fmt == AV_PIX_FMT_BGR0
  225. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  226. || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
  227. vsample[0] = hsample[0] =
  228. vsample[1] = hsample[1] =
  229. vsample[2] = hsample[2] =
  230. vsample[3] = hsample[3] = 1;
  231. } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
  232. vsample[0] = vsample[1] = vsample[2] = 2;
  233. hsample[0] = hsample[1] = hsample[2] = 1;
  234. } else {
  235. int chroma_h_shift, chroma_v_shift;
  236. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
  237. &chroma_v_shift);
  238. vsample[0] = 2;
  239. vsample[1] = 2 >> chroma_v_shift;
  240. vsample[2] = 2 >> chroma_v_shift;
  241. hsample[0] = 2;
  242. hsample[1] = 2 >> chroma_h_shift;
  243. hsample[2] = 2 >> chroma_h_shift;
  244. }
  245. }
  246. void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
  247. const AVFrame *frame, const struct MJpegContext *m,
  248. const uint8_t intra_matrix_permutation[64], int pred,
  249. const uint16_t luma_intra_matrix[64],
  250. const uint16_t chroma_intra_matrix[64],
  251. int use_slices)
  252. {
  253. const int lossless = !m;
  254. int hsample[4], vsample[4];
  255. int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
  256. int chroma_matrix;
  257. ff_mjpeg_init_hvsample(avctx, hsample, vsample);
  258. put_marker(pb, SOI);
  259. // hack for AMV mjpeg format
  260. if (avctx->codec_id == AV_CODEC_ID_AMV)
  261. return;
  262. jpeg_put_comments(avctx, pb, frame);
  263. chroma_matrix = !lossless && !!memcmp(luma_intra_matrix,
  264. chroma_intra_matrix,
  265. sizeof(luma_intra_matrix[0]) * 64);
  266. jpeg_table_header(avctx, pb, m, intra_matrix_permutation,
  267. luma_intra_matrix, chroma_intra_matrix, hsample,
  268. use_slices, chroma_matrix);
  269. switch (avctx->codec_id) {
  270. case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
  271. case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
  272. default: av_assert0(0);
  273. }
  274. put_bits(pb, 16, 8 + 3 * components);
  275. if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
  276. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  277. || avctx->pix_fmt == AV_PIX_FMT_BGR24))
  278. put_bits(pb, 8, 9); /* 9 bits/component RCT */
  279. else
  280. put_bits(pb, 8, 8); /* 8 bits/component */
  281. put_bits(pb, 16, avctx->height);
  282. put_bits(pb, 16, avctx->width);
  283. put_bits(pb, 8, components); /* 3 or 4 components */
  284. /* Y component */
  285. put_bits(pb, 8, 1); /* component number */
  286. put_bits(pb, 4, hsample[0]); /* H factor */
  287. put_bits(pb, 4, vsample[0]); /* V factor */
  288. put_bits(pb, 8, 0); /* select matrix */
  289. /* Cb component */
  290. put_bits(pb, 8, 2); /* component number */
  291. put_bits(pb, 4, hsample[1]); /* H factor */
  292. put_bits(pb, 4, vsample[1]); /* V factor */
  293. put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
  294. /* Cr component */
  295. put_bits(pb, 8, 3); /* component number */
  296. put_bits(pb, 4, hsample[2]); /* H factor */
  297. put_bits(pb, 4, vsample[2]); /* V factor */
  298. put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
  299. if (components == 4) {
  300. put_bits(pb, 8, 4); /* component number */
  301. put_bits(pb, 4, hsample[3]); /* H factor */
  302. put_bits(pb, 4, vsample[3]); /* V factor */
  303. put_bits(pb, 8, 0); /* select matrix */
  304. }
  305. /* scan header */
  306. put_marker(pb, SOS);
  307. put_bits(pb, 16, 6 + 2*components); /* length */
  308. put_bits(pb, 8, components); /* 3 components */
  309. /* Y component */
  310. put_bits(pb, 8, 1); /* index */
  311. put_bits(pb, 4, 0); /* DC huffman table index */
  312. put_bits(pb, 4, 0); /* AC huffman table index */
  313. /* Cb component */
  314. put_bits(pb, 8, 2); /* index */
  315. put_bits(pb, 4, 1); /* DC huffman table index */
  316. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  317. /* Cr component */
  318. put_bits(pb, 8, 3); /* index */
  319. put_bits(pb, 4, 1); /* DC huffman table index */
  320. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  321. if (components == 4) {
  322. /* Alpha component */
  323. put_bits(pb, 8, 4); /* index */
  324. put_bits(pb, 4, 0); /* DC huffman table index */
  325. put_bits(pb, 4, 0); /* AC huffman table index */
  326. }
  327. put_bits(pb, 8, pred); /* Ss (not used); pred only nonzero for LJPEG */
  328. switch (avctx->codec_id) {
  329. case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
  330. case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
  331. default: av_assert0(0);
  332. }
  333. put_bits(pb, 8, 0); /* Ah/Al (not used) */
  334. }
  335. void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
  336. {
  337. int size;
  338. int i, ff_count;
  339. uint8_t *buf = pb->buf + start;
  340. int align= (-(size_t)(buf))&3;
  341. int pad = (-put_bits_count(pb))&7;
  342. if (pad)
  343. put_bits(pb, pad, (1<<pad)-1);
  344. flush_put_bits(pb);
  345. size = put_bytes_output(pb) - start;
  346. ff_count=0;
  347. for(i=0; i<size && i<align; i++){
  348. if(buf[i]==0xFF) ff_count++;
  349. }
  350. for(; i<size-15; i+=16){
  351. int acc, v;
  352. v= *(uint32_t*)(&buf[i]);
  353. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  354. v= *(uint32_t*)(&buf[i+4]);
  355. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  356. v= *(uint32_t*)(&buf[i+8]);
  357. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  358. v= *(uint32_t*)(&buf[i+12]);
  359. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  360. acc>>=4;
  361. acc+= (acc>>16);
  362. acc+= (acc>>8);
  363. ff_count+= acc&0xFF;
  364. }
  365. for(; i<size; i++){
  366. if(buf[i]==0xFF) ff_count++;
  367. }
  368. if(ff_count==0) return;
  369. skip_put_bytes(pb, ff_count);
  370. for(i=size-1; ff_count; i--){
  371. int v= buf[i];
  372. if(v==0xFF){
  373. buf[i+ff_count]= 0;
  374. ff_count--;
  375. }
  376. buf[i+ff_count]= v;
  377. }
  378. }
  379. /* isn't this function nicer than the one in the libjpeg ? */
  380. void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
  381. const uint8_t *bits_table,
  382. const uint8_t *val_table)
  383. {
  384. int k, code;
  385. k = 0;
  386. code = 0;
  387. for (int i = 1; i <= 16; i++) {
  388. int nb = bits_table[i];
  389. for (int j = 0; j < nb; j++) {
  390. int sym = val_table[k++];
  391. huff_size[sym] = i;
  392. huff_code[sym] = code;
  393. code++;
  394. }
  395. code <<= 1;
  396. }
  397. }
  398. void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
  399. {
  400. av_assert1((header_bits & 7) == 0);
  401. put_marker(pb, EOI);
  402. }
  403. void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
  404. const uint8_t huff_size[], const uint16_t huff_code[])
  405. {
  406. int mant, nbits;
  407. if (val == 0) {
  408. put_bits(pb, huff_size[0], huff_code[0]);
  409. } else {
  410. mant = val;
  411. if (val < 0) {
  412. val = -val;
  413. mant--;
  414. }
  415. nbits= av_log2_16bit(val) + 1;
  416. put_bits(pb, huff_size[nbits], huff_code[nbits]);
  417. put_sbits(pb, nbits, mant);
  418. }
  419. }
  420. int ff_mjpeg_encode_check_pix_fmt(AVCodecContext *avctx)
  421. {
  422. if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
  423. avctx->color_range != AVCOL_RANGE_JPEG &&
  424. (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  425. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  426. avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
  427. avctx->color_range == AVCOL_RANGE_MPEG)) {
  428. av_log(avctx, AV_LOG_ERROR,
  429. "Non full-range YUV is non-standard, set strict_std_compliance "
  430. "to at most unofficial to use it.\n");
  431. return AVERROR(EINVAL);
  432. }
  433. return 0;
  434. }