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.

400 lines
12KB

  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/common.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "libavutil/pixfmt.h"
  27. #include "avcodec.h"
  28. #include "idctdsp.h"
  29. #include "jpegtables.h"
  30. #include "put_bits.h"
  31. #include "mjpegenc.h"
  32. #include "mjpegenc_common.h"
  33. #include "mjpeg.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, i;
  39. put_bits(p, 4, table_class);
  40. put_bits(p, 4, table_id);
  41. n = 0;
  42. for(i=1;i<=16;i++) {
  43. n += bits_table[i];
  44. put_bits(p, 8, bits_table[i]);
  45. }
  46. for(i=0;i<n;i++)
  47. put_bits(p, 8, value_table[i]);
  48. return n + 17;
  49. }
  50. static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
  51. ScanTable *intra_scantable,
  52. uint16_t luma_intra_matrix[64],
  53. uint16_t chroma_intra_matrix[64],
  54. int hsample[3])
  55. {
  56. int i, j, size;
  57. uint8_t *ptr;
  58. if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
  59. int matrix_count = 1 + !!memcmp(luma_intra_matrix,
  60. chroma_intra_matrix,
  61. sizeof(luma_intra_matrix[0]) * 64);
  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(i=0;i<64;i++) {
  68. j = intra_scantable->permutated[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(i=0;i<64;i++) {
  75. j = intra_scantable->permutated[i];
  76. put_bits(p, 8, chroma_intra_matrix[j]);
  77. }
  78. }
  79. }
  80. if(avctx->active_thread_type & FF_THREAD_SLICE){
  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. size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
  92. avpriv_mjpeg_val_dc);
  93. size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  94. avpriv_mjpeg_val_dc);
  95. size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  96. avpriv_mjpeg_val_ac_luminance);
  97. size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  98. avpriv_mjpeg_val_ac_chrominance);
  99. AV_WB16(ptr, size);
  100. }
  101. static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
  102. {
  103. int size;
  104. uint8_t *ptr;
  105. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
  106. /* JFIF header */
  107. put_marker(p, APP0);
  108. put_bits(p, 16, 16);
  109. avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
  110. /* The most significant byte is used for major revisions, the least
  111. * significant byte for minor revisions. Version 1.02 is the current
  112. * released revision. */
  113. put_bits(p, 16, 0x0102);
  114. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  115. put_bits(p, 16, avctx->sample_aspect_ratio.num);
  116. put_bits(p, 16, avctx->sample_aspect_ratio.den);
  117. put_bits(p, 8, 0); /* thumbnail width */
  118. put_bits(p, 8, 0); /* thumbnail height */
  119. }
  120. /* comment */
  121. if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
  122. put_marker(p, COM);
  123. flush_put_bits(p);
  124. ptr = put_bits_ptr(p);
  125. put_bits(p, 16, 0); /* patched later */
  126. avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
  127. size = strlen(LIBAVCODEC_IDENT)+3;
  128. AV_WB16(ptr, size);
  129. }
  130. if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  131. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  132. avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
  133. || avctx->color_range == AVCOL_RANGE_MPEG) {
  134. put_marker(p, COM);
  135. flush_put_bits(p);
  136. ptr = put_bits_ptr(p);
  137. put_bits(p, 16, 0); /* patched later */
  138. avpriv_put_string(p, "CS=ITU601", 1);
  139. size = strlen("CS=ITU601")+3;
  140. AV_WB16(ptr, size);
  141. }
  142. }
  143. void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[3], int vsample[3])
  144. {
  145. int chroma_h_shift, chroma_v_shift;
  146. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
  147. &chroma_v_shift);
  148. if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
  149. ( avctx->pix_fmt == AV_PIX_FMT_BGR0
  150. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  151. || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
  152. vsample[0] = hsample[0] =
  153. vsample[1] = hsample[1] =
  154. vsample[2] = hsample[2] = 1;
  155. } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
  156. vsample[0] = vsample[1] = vsample[2] = 2;
  157. hsample[0] = hsample[1] = hsample[2] = 1;
  158. } else {
  159. vsample[0] = 2;
  160. vsample[1] = 2 >> chroma_v_shift;
  161. vsample[2] = 2 >> chroma_v_shift;
  162. hsample[0] = 2;
  163. hsample[1] = 2 >> chroma_h_shift;
  164. hsample[2] = 2 >> chroma_h_shift;
  165. }
  166. }
  167. void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
  168. ScanTable *intra_scantable,
  169. uint16_t luma_intra_matrix[64],
  170. uint16_t chroma_intra_matrix[64])
  171. {
  172. const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
  173. int hsample[3], vsample[3];
  174. int i;
  175. int chroma_matrix = !!memcmp(luma_intra_matrix,
  176. chroma_intra_matrix,
  177. sizeof(luma_intra_matrix[0])*64);
  178. ff_mjpeg_init_hvsample(avctx, hsample, vsample);
  179. put_marker(pb, SOI);
  180. // hack for AMV mjpeg format
  181. if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
  182. jpeg_put_comments(avctx, pb);
  183. jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
  184. switch (avctx->codec_id) {
  185. case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
  186. case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
  187. default: av_assert0(0);
  188. }
  189. put_bits(pb, 16, 17);
  190. if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
  191. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  192. || avctx->pix_fmt == AV_PIX_FMT_BGR24))
  193. put_bits(pb, 8, 9); /* 9 bits/component RCT */
  194. else
  195. put_bits(pb, 8, 8); /* 8 bits/component */
  196. put_bits(pb, 16, avctx->height);
  197. put_bits(pb, 16, avctx->width);
  198. put_bits(pb, 8, 3); /* 3 components */
  199. /* Y component */
  200. put_bits(pb, 8, 1); /* component number */
  201. put_bits(pb, 4, hsample[0]); /* H factor */
  202. put_bits(pb, 4, vsample[0]); /* V factor */
  203. put_bits(pb, 8, 0); /* select matrix */
  204. /* Cb component */
  205. put_bits(pb, 8, 2); /* component number */
  206. put_bits(pb, 4, hsample[1]); /* H factor */
  207. put_bits(pb, 4, vsample[1]); /* V factor */
  208. put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
  209. /* Cr component */
  210. put_bits(pb, 8, 3); /* component number */
  211. put_bits(pb, 4, hsample[2]); /* H factor */
  212. put_bits(pb, 4, vsample[2]); /* V factor */
  213. put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
  214. /* scan header */
  215. put_marker(pb, SOS);
  216. put_bits(pb, 16, 12); /* length */
  217. put_bits(pb, 8, 3); /* 3 components */
  218. /* Y component */
  219. put_bits(pb, 8, 1); /* index */
  220. put_bits(pb, 4, 0); /* DC huffman table index */
  221. put_bits(pb, 4, 0); /* AC huffman table index */
  222. /* Cb component */
  223. put_bits(pb, 8, 2); /* index */
  224. put_bits(pb, 4, 1); /* DC huffman table index */
  225. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  226. /* Cr component */
  227. put_bits(pb, 8, 3); /* index */
  228. put_bits(pb, 4, 1); /* DC huffman table index */
  229. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  230. put_bits(pb, 8, lossless ? avctx->prediction_method + 1 : 0); /* Ss (not used) */
  231. switch (avctx->codec_id) {
  232. case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
  233. case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
  234. default: av_assert0(0);
  235. }
  236. put_bits(pb, 8, 0); /* Ah/Al (not used) */
  237. end:
  238. if (!lossless) {
  239. MpegEncContext *s = avctx->priv_data;
  240. av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
  241. s->esc_pos = put_bits_count(pb) >> 3;
  242. for(i=1; i<s->slice_context_count; i++)
  243. s->thread_context[i]->esc_pos = 0;
  244. }
  245. }
  246. void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
  247. {
  248. int size;
  249. int i, ff_count;
  250. uint8_t *buf = pb->buf + start;
  251. int align= (-(size_t)(buf))&3;
  252. int pad = (-put_bits_count(pb))&7;
  253. if (pad)
  254. put_bits(pb, pad, (1<<pad)-1);
  255. flush_put_bits(pb);
  256. size = put_bits_count(pb) - start * 8;
  257. av_assert1((size&7) == 0);
  258. size >>= 3;
  259. ff_count=0;
  260. for(i=0; i<size && i<align; i++){
  261. if(buf[i]==0xFF) ff_count++;
  262. }
  263. for(; i<size-15; i+=16){
  264. int acc, v;
  265. v= *(uint32_t*)(&buf[i]);
  266. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  267. v= *(uint32_t*)(&buf[i+4]);
  268. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  269. v= *(uint32_t*)(&buf[i+8]);
  270. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  271. v= *(uint32_t*)(&buf[i+12]);
  272. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  273. acc>>=4;
  274. acc+= (acc>>16);
  275. acc+= (acc>>8);
  276. ff_count+= acc&0xFF;
  277. }
  278. for(; i<size; i++){
  279. if(buf[i]==0xFF) ff_count++;
  280. }
  281. if(ff_count==0) return;
  282. flush_put_bits(pb);
  283. skip_put_bytes(pb, ff_count);
  284. for(i=size-1; ff_count; i--){
  285. int v= buf[i];
  286. if(v==0xFF){
  287. buf[i+ff_count]= 0;
  288. ff_count--;
  289. }
  290. buf[i+ff_count]= v;
  291. }
  292. }
  293. int ff_mjpeg_encode_stuffing(MpegEncContext *s)
  294. {
  295. int i;
  296. PutBitContext *pbc = &s->pb;
  297. int mb_y = s->mb_y - !s->mb_x;
  298. int ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
  299. put_bits_count(&s->pb) / 4 + 1000);
  300. if (ret < 0) {
  301. av_log(s->avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
  302. goto fail;
  303. }
  304. ff_mjpeg_escape_FF(pbc, s->esc_pos);
  305. if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
  306. put_marker(pbc, RST0 + (mb_y&7));
  307. s->esc_pos = put_bits_count(pbc) >> 3;
  308. fail:
  309. for(i=0; i<3; i++)
  310. s->last_dc[i] = 128 << s->intra_dc_precision;
  311. return ret;
  312. }
  313. void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
  314. {
  315. av_assert1((header_bits & 7) == 0);
  316. put_marker(pb, EOI);
  317. }
  318. void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
  319. uint8_t *huff_size, uint16_t *huff_code)
  320. {
  321. int mant, nbits;
  322. if (val == 0) {
  323. put_bits(pb, huff_size[0], huff_code[0]);
  324. } else {
  325. mant = val;
  326. if (val < 0) {
  327. val = -val;
  328. mant--;
  329. }
  330. nbits= av_log2_16bit(val) + 1;
  331. put_bits(pb, huff_size[nbits], huff_code[nbits]);
  332. put_sbits(pb, nbits, mant);
  333. }
  334. }