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.

615 lines
20KB

  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 "mjpegenc_huffman.h"
  34. #include "mjpeg.h"
  35. av_cold void ff_init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
  36. {
  37. int i;
  38. for (i = 0; i < 128; i++) {
  39. int level = i - 64;
  40. int run;
  41. if (!level)
  42. continue;
  43. for (run = 0; run < 64; run++) {
  44. int len, code, nbits;
  45. int alevel = FFABS(level);
  46. len = (run >> 4) * huff_size_ac[0xf0];
  47. nbits= av_log2_16bit(alevel) + 1;
  48. code = ((15&run) << 4) | nbits;
  49. len += huff_size_ac[code] + nbits;
  50. uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
  51. // We ignore EOB as its just a constant which does not change generally
  52. }
  53. }
  54. }
  55. /* table_class: 0 = DC coef, 1 = AC coefs */
  56. static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
  57. const uint8_t *bits_table, const uint8_t *value_table)
  58. {
  59. int n, i;
  60. put_bits(p, 4, table_class);
  61. put_bits(p, 4, table_id);
  62. n = 0;
  63. for(i=1;i<=16;i++) {
  64. n += bits_table[i];
  65. put_bits(p, 8, bits_table[i]);
  66. }
  67. for(i=0;i<n;i++)
  68. put_bits(p, 8, value_table[i]);
  69. return n + 17;
  70. }
  71. static void jpeg_table_header(AVCodecContext *avctx, PutBitContext *p,
  72. ScanTable *intra_scantable,
  73. uint16_t luma_intra_matrix[64],
  74. uint16_t chroma_intra_matrix[64],
  75. int hsample[3])
  76. {
  77. int i, j, size;
  78. uint8_t *ptr;
  79. MpegEncContext *s = NULL;
  80. /* Since avctx->priv_data will point to LJpegEncContext in this case */
  81. if (avctx->codec_id != AV_CODEC_ID_LJPEG)
  82. s = avctx->priv_data;
  83. if (avctx->codec_id != AV_CODEC_ID_LJPEG) {
  84. int matrix_count = 1 + !!memcmp(luma_intra_matrix,
  85. chroma_intra_matrix,
  86. sizeof(luma_intra_matrix[0]) * 64);
  87. if (s && s->force_duplicated_matrix)
  88. matrix_count = 2;
  89. /* quant matrixes */
  90. put_marker(p, DQT);
  91. put_bits(p, 16, 2 + matrix_count * (1 + 64));
  92. put_bits(p, 4, 0); /* 8 bit precision */
  93. put_bits(p, 4, 0); /* table 0 */
  94. for(i=0;i<64;i++) {
  95. j = intra_scantable->permutated[i];
  96. put_bits(p, 8, luma_intra_matrix[j]);
  97. }
  98. if (matrix_count > 1) {
  99. put_bits(p, 4, 0); /* 8 bit precision */
  100. put_bits(p, 4, 1); /* table 1 */
  101. for(i=0;i<64;i++) {
  102. j = intra_scantable->permutated[i];
  103. put_bits(p, 8, chroma_intra_matrix[j]);
  104. }
  105. }
  106. }
  107. if(avctx->active_thread_type & FF_THREAD_SLICE){
  108. put_marker(p, DRI);
  109. put_bits(p, 16, 4);
  110. put_bits(p, 16, (avctx->width-1)/(8*hsample[0]) + 1);
  111. }
  112. /* huffman table */
  113. put_marker(p, DHT);
  114. flush_put_bits(p);
  115. ptr = put_bits_ptr(p);
  116. put_bits(p, 16, 0); /* patched later */
  117. size = 2;
  118. // Only MJPEG can have a variable Huffman variable. All other
  119. // formats use the default Huffman table.
  120. if (s && s->huffman == HUFFMAN_TABLE_OPTIMAL) {
  121. size += put_huffman_table(p, 0, 0, s->mjpeg_ctx->bits_dc_luminance,
  122. s->mjpeg_ctx->val_dc_luminance);
  123. size += put_huffman_table(p, 0, 1, s->mjpeg_ctx->bits_dc_chrominance,
  124. s->mjpeg_ctx->val_dc_chrominance);
  125. size += put_huffman_table(p, 1, 0, s->mjpeg_ctx->bits_ac_luminance,
  126. s->mjpeg_ctx->val_ac_luminance);
  127. size += put_huffman_table(p, 1, 1, s->mjpeg_ctx->bits_ac_chrominance,
  128. s->mjpeg_ctx->val_ac_chrominance);
  129. } else {
  130. size += put_huffman_table(p, 0, 0, avpriv_mjpeg_bits_dc_luminance,
  131. avpriv_mjpeg_val_dc);
  132. size += put_huffman_table(p, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  133. avpriv_mjpeg_val_dc);
  134. size += put_huffman_table(p, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  135. avpriv_mjpeg_val_ac_luminance);
  136. size += put_huffman_table(p, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  137. avpriv_mjpeg_val_ac_chrominance);
  138. }
  139. AV_WB16(ptr, size);
  140. }
  141. static void jpeg_put_comments(AVCodecContext *avctx, PutBitContext *p)
  142. {
  143. int size;
  144. uint8_t *ptr;
  145. if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0) {
  146. AVRational sar = avctx->sample_aspect_ratio;
  147. if (sar.num > 65535 || sar.den > 65535) {
  148. if (!av_reduce(&sar.num, &sar.den, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 65535))
  149. av_log(avctx, AV_LOG_WARNING,
  150. "Cannot store exact aspect ratio %d:%d\n",
  151. avctx->sample_aspect_ratio.num,
  152. avctx->sample_aspect_ratio.den);
  153. }
  154. /* JFIF header */
  155. put_marker(p, APP0);
  156. put_bits(p, 16, 16);
  157. avpriv_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
  158. /* The most significant byte is used for major revisions, the least
  159. * significant byte for minor revisions. Version 1.02 is the current
  160. * released revision. */
  161. put_bits(p, 16, 0x0102);
  162. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  163. put_bits(p, 16, sar.num);
  164. put_bits(p, 16, sar.den);
  165. put_bits(p, 8, 0); /* thumbnail width */
  166. put_bits(p, 8, 0); /* thumbnail height */
  167. }
  168. /* comment */
  169. if (!(avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
  170. put_marker(p, COM);
  171. flush_put_bits(p);
  172. ptr = put_bits_ptr(p);
  173. put_bits(p, 16, 0); /* patched later */
  174. avpriv_put_string(p, LIBAVCODEC_IDENT, 1);
  175. size = strlen(LIBAVCODEC_IDENT)+3;
  176. AV_WB16(ptr, size);
  177. }
  178. if (((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
  179. avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
  180. avctx->pix_fmt == AV_PIX_FMT_YUV444P) && avctx->color_range != AVCOL_RANGE_JPEG)
  181. || avctx->color_range == AVCOL_RANGE_MPEG) {
  182. put_marker(p, COM);
  183. flush_put_bits(p);
  184. ptr = put_bits_ptr(p);
  185. put_bits(p, 16, 0); /* patched later */
  186. avpriv_put_string(p, "CS=ITU601", 1);
  187. size = strlen("CS=ITU601")+3;
  188. AV_WB16(ptr, size);
  189. }
  190. }
  191. void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4])
  192. {
  193. int chroma_h_shift, chroma_v_shift;
  194. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
  195. &chroma_v_shift);
  196. if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
  197. ( avctx->pix_fmt == AV_PIX_FMT_BGR0
  198. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  199. || avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
  200. vsample[0] = hsample[0] =
  201. vsample[1] = hsample[1] =
  202. vsample[2] = hsample[2] =
  203. vsample[3] = hsample[3] = 1;
  204. } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
  205. vsample[0] = vsample[1] = vsample[2] = 2;
  206. hsample[0] = hsample[1] = hsample[2] = 1;
  207. } else {
  208. vsample[0] = 2;
  209. vsample[1] = 2 >> chroma_v_shift;
  210. vsample[2] = 2 >> chroma_v_shift;
  211. hsample[0] = 2;
  212. hsample[1] = 2 >> chroma_h_shift;
  213. hsample[2] = 2 >> chroma_h_shift;
  214. }
  215. }
  216. void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
  217. ScanTable *intra_scantable, int pred,
  218. uint16_t luma_intra_matrix[64],
  219. uint16_t chroma_intra_matrix[64])
  220. {
  221. const int lossless = avctx->codec_id != AV_CODEC_ID_MJPEG && avctx->codec_id != AV_CODEC_ID_AMV;
  222. int hsample[4], vsample[4];
  223. int i;
  224. int components = 3 + (avctx->pix_fmt == AV_PIX_FMT_BGRA);
  225. int chroma_matrix = !!memcmp(luma_intra_matrix,
  226. chroma_intra_matrix,
  227. sizeof(luma_intra_matrix[0])*64);
  228. ff_mjpeg_init_hvsample(avctx, hsample, vsample);
  229. put_marker(pb, SOI);
  230. // hack for AMV mjpeg format
  231. if(avctx->codec_id == AV_CODEC_ID_AMV) goto end;
  232. jpeg_put_comments(avctx, pb);
  233. jpeg_table_header(avctx, pb, intra_scantable, luma_intra_matrix, chroma_intra_matrix, hsample);
  234. switch (avctx->codec_id) {
  235. case AV_CODEC_ID_MJPEG: put_marker(pb, SOF0 ); break;
  236. case AV_CODEC_ID_LJPEG: put_marker(pb, SOF3 ); break;
  237. default: av_assert0(0);
  238. }
  239. put_bits(pb, 16, 17);
  240. if (lossless && ( avctx->pix_fmt == AV_PIX_FMT_BGR0
  241. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  242. || avctx->pix_fmt == AV_PIX_FMT_BGR24))
  243. put_bits(pb, 8, 9); /* 9 bits/component RCT */
  244. else
  245. put_bits(pb, 8, 8); /* 8 bits/component */
  246. put_bits(pb, 16, avctx->height);
  247. put_bits(pb, 16, avctx->width);
  248. put_bits(pb, 8, components); /* 3 or 4 components */
  249. /* Y component */
  250. put_bits(pb, 8, 1); /* component number */
  251. put_bits(pb, 4, hsample[0]); /* H factor */
  252. put_bits(pb, 4, vsample[0]); /* V factor */
  253. put_bits(pb, 8, 0); /* select matrix */
  254. /* Cb component */
  255. put_bits(pb, 8, 2); /* component number */
  256. put_bits(pb, 4, hsample[1]); /* H factor */
  257. put_bits(pb, 4, vsample[1]); /* V factor */
  258. put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
  259. /* Cr component */
  260. put_bits(pb, 8, 3); /* component number */
  261. put_bits(pb, 4, hsample[2]); /* H factor */
  262. put_bits(pb, 4, vsample[2]); /* V factor */
  263. put_bits(pb, 8, lossless ? 0 : chroma_matrix); /* select matrix */
  264. if (components == 4) {
  265. put_bits(pb, 8, 4); /* component number */
  266. put_bits(pb, 4, hsample[3]); /* H factor */
  267. put_bits(pb, 4, vsample[3]); /* V factor */
  268. put_bits(pb, 8, 0); /* select matrix */
  269. }
  270. /* scan header */
  271. put_marker(pb, SOS);
  272. put_bits(pb, 16, 6 + 2*components); /* length */
  273. put_bits(pb, 8, components); /* 3 components */
  274. /* Y component */
  275. put_bits(pb, 8, 1); /* index */
  276. put_bits(pb, 4, 0); /* DC huffman table index */
  277. put_bits(pb, 4, 0); /* AC huffman table index */
  278. /* Cb component */
  279. put_bits(pb, 8, 2); /* index */
  280. put_bits(pb, 4, 1); /* DC huffman table index */
  281. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  282. /* Cr component */
  283. put_bits(pb, 8, 3); /* index */
  284. put_bits(pb, 4, 1); /* DC huffman table index */
  285. put_bits(pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  286. if (components == 4) {
  287. /* Alpha component */
  288. put_bits(pb, 8, 4); /* index */
  289. put_bits(pb, 4, 0); /* DC huffman table index */
  290. put_bits(pb, 4, 0); /* AC huffman table index */
  291. }
  292. put_bits(pb, 8, lossless ? pred : 0); /* Ss (not used) */
  293. switch (avctx->codec_id) {
  294. case AV_CODEC_ID_MJPEG: put_bits(pb, 8, 63); break; /* Se (not used) */
  295. case AV_CODEC_ID_LJPEG: put_bits(pb, 8, 0); break; /* not used */
  296. default: av_assert0(0);
  297. }
  298. put_bits(pb, 8, 0); /* Ah/Al (not used) */
  299. end:
  300. if (!lossless) {
  301. MpegEncContext *s = avctx->priv_data;
  302. av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
  303. s->esc_pos = put_bits_count(pb) >> 3;
  304. for(i=1; i<s->slice_context_count; i++)
  305. s->thread_context[i]->esc_pos = 0;
  306. }
  307. }
  308. /**
  309. * Encodes and outputs the entire frame in the JPEG format.
  310. *
  311. * @param s The MpegEncContext.
  312. */
  313. void ff_mjpeg_encode_picture_frame(MpegEncContext *s)
  314. {
  315. int i, nbits, code, table_id;
  316. MJpegContext *m = s->mjpeg_ctx;
  317. uint8_t *huff_size[4] = {m->huff_size_dc_luminance,
  318. m->huff_size_dc_chrominance,
  319. m->huff_size_ac_luminance,
  320. m->huff_size_ac_chrominance};
  321. uint16_t *huff_code[4] = {m->huff_code_dc_luminance,
  322. m->huff_code_dc_chrominance,
  323. m->huff_code_ac_luminance,
  324. m->huff_code_ac_chrominance};
  325. size_t total_bits = 0;
  326. size_t bytes_needed;
  327. s->header_bits = get_bits_diff(s);
  328. // Estimate the total size first
  329. for (i = 0; i < m->huff_ncode; i++) {
  330. table_id = m->huff_buffer[i].table_id;
  331. code = m->huff_buffer[i].code;
  332. nbits = code & 0xf;
  333. total_bits += huff_size[table_id][code] + nbits;
  334. }
  335. bytes_needed = (total_bits + 7) / 8;
  336. ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
  337. for (i = 0; i < m->huff_ncode; i++) {
  338. table_id = m->huff_buffer[i].table_id;
  339. code = m->huff_buffer[i].code;
  340. nbits = code & 0xf;
  341. put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
  342. if (nbits != 0) {
  343. put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
  344. }
  345. }
  346. m->huff_ncode = 0;
  347. s->i_tex_bits = get_bits_diff(s);
  348. }
  349. void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
  350. {
  351. int size;
  352. int i, ff_count;
  353. uint8_t *buf = pb->buf + start;
  354. int align= (-(size_t)(buf))&3;
  355. int pad = (-put_bits_count(pb))&7;
  356. if (pad)
  357. put_bits(pb, pad, (1<<pad)-1);
  358. flush_put_bits(pb);
  359. size = put_bits_count(pb) - start * 8;
  360. av_assert1((size&7) == 0);
  361. size >>= 3;
  362. ff_count=0;
  363. for(i=0; i<size && i<align; i++){
  364. if(buf[i]==0xFF) ff_count++;
  365. }
  366. for(; i<size-15; i+=16){
  367. int acc, v;
  368. v= *(uint32_t*)(&buf[i]);
  369. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  370. v= *(uint32_t*)(&buf[i+4]);
  371. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  372. v= *(uint32_t*)(&buf[i+8]);
  373. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  374. v= *(uint32_t*)(&buf[i+12]);
  375. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  376. acc>>=4;
  377. acc+= (acc>>16);
  378. acc+= (acc>>8);
  379. ff_count+= acc&0xFF;
  380. }
  381. for(; i<size; i++){
  382. if(buf[i]==0xFF) ff_count++;
  383. }
  384. if(ff_count==0) return;
  385. flush_put_bits(pb);
  386. skip_put_bytes(pb, ff_count);
  387. for(i=size-1; ff_count; i--){
  388. int v= buf[i];
  389. if(v==0xFF){
  390. buf[i+ff_count]= 0;
  391. ff_count--;
  392. }
  393. buf[i+ff_count]= v;
  394. }
  395. }
  396. /**
  397. * Builds all 4 optimal Huffman tables.
  398. *
  399. * Uses the data stored in the JPEG buffer to compute the tables.
  400. * Stores the Huffman tables in the bits_* and val_* arrays in the MJpegContext.
  401. *
  402. * @param m MJpegContext containing the JPEG buffer.
  403. */
  404. static void ff_mjpeg_build_optimal_huffman(MJpegContext *m)
  405. {
  406. int i, table_id, code;
  407. MJpegEncHuffmanContext dc_luminance_ctx;
  408. MJpegEncHuffmanContext dc_chrominance_ctx;
  409. MJpegEncHuffmanContext ac_luminance_ctx;
  410. MJpegEncHuffmanContext ac_chrominance_ctx;
  411. MJpegEncHuffmanContext *ctx[4] = {&dc_luminance_ctx,
  412. &dc_chrominance_ctx,
  413. &ac_luminance_ctx,
  414. &ac_chrominance_ctx};
  415. for (i = 0; i < 4; i++) {
  416. ff_mjpeg_encode_huffman_init(ctx[i]);
  417. }
  418. for (i = 0; i < m->huff_ncode; i++) {
  419. table_id = m->huff_buffer[i].table_id;
  420. code = m->huff_buffer[i].code;
  421. ff_mjpeg_encode_huffman_increment(ctx[table_id], code);
  422. }
  423. ff_mjpeg_encode_huffman_close(&dc_luminance_ctx,
  424. m->bits_dc_luminance,
  425. m->val_dc_luminance, 12);
  426. ff_mjpeg_encode_huffman_close(&dc_chrominance_ctx,
  427. m->bits_dc_chrominance,
  428. m->val_dc_chrominance, 12);
  429. ff_mjpeg_encode_huffman_close(&ac_luminance_ctx,
  430. m->bits_ac_luminance,
  431. m->val_ac_luminance, 256);
  432. ff_mjpeg_encode_huffman_close(&ac_chrominance_ctx,
  433. m->bits_ac_chrominance,
  434. m->val_ac_chrominance, 256);
  435. ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
  436. m->huff_code_dc_luminance,
  437. m->bits_dc_luminance,
  438. m->val_dc_luminance);
  439. ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
  440. m->huff_code_dc_chrominance,
  441. m->bits_dc_chrominance,
  442. m->val_dc_chrominance);
  443. ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
  444. m->huff_code_ac_luminance,
  445. m->bits_ac_luminance,
  446. m->val_ac_luminance);
  447. ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
  448. m->huff_code_ac_chrominance,
  449. m->bits_ac_chrominance,
  450. m->val_ac_chrominance);
  451. }
  452. /**
  453. * Writes the complete JPEG frame when optimal huffman tables are enabled,
  454. * otherwise writes the stuffing.
  455. *
  456. * Header + values + stuffing.
  457. *
  458. * @param s The MpegEncContext.
  459. * @return int Error code, 0 if successful.
  460. */
  461. int ff_mjpeg_encode_stuffing(MpegEncContext *s)
  462. {
  463. int i;
  464. PutBitContext *pbc = &s->pb;
  465. int mb_y = s->mb_y - !s->mb_x;
  466. int ret;
  467. MJpegContext *m;
  468. m = s->mjpeg_ctx;
  469. if (s->huffman == HUFFMAN_TABLE_OPTIMAL) {
  470. ff_mjpeg_build_optimal_huffman(m);
  471. // Replace the VLCs with the optimal ones.
  472. // The default ones may be used for trellis during quantization.
  473. ff_init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
  474. ff_init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
  475. s->intra_ac_vlc_length =
  476. s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
  477. s->intra_chroma_ac_vlc_length =
  478. s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
  479. ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
  480. s->pred, s->intra_matrix, s->chroma_intra_matrix);
  481. ff_mjpeg_encode_picture_frame(s);
  482. }
  483. ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
  484. put_bits_count(&s->pb) / 4 + 1000);
  485. if (ret < 0) {
  486. av_log(s->avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
  487. goto fail;
  488. }
  489. ff_mjpeg_escape_FF(pbc, s->esc_pos);
  490. if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height - 1)
  491. put_marker(pbc, RST0 + (mb_y&7));
  492. s->esc_pos = put_bits_count(pbc) >> 3;
  493. fail:
  494. for(i=0; i<3; i++)
  495. s->last_dc[i] = 128 << s->intra_dc_precision;
  496. return ret;
  497. }
  498. void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
  499. {
  500. av_assert1((header_bits & 7) == 0);
  501. put_marker(pb, EOI);
  502. }
  503. void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
  504. uint8_t *huff_size, uint16_t *huff_code)
  505. {
  506. int mant, nbits;
  507. if (val == 0) {
  508. put_bits(pb, huff_size[0], huff_code[0]);
  509. } else {
  510. mant = val;
  511. if (val < 0) {
  512. val = -val;
  513. mant--;
  514. }
  515. nbits= av_log2_16bit(val) + 1;
  516. put_bits(pb, huff_size[nbits], huff_code[nbits]);
  517. put_sbits(pb, nbits, mant);
  518. }
  519. }