mjpegenc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * MJPEG encoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. * Copyright (c) 2003 Alex Beregszaszi
  5. * Copyright (c) 2003-2004 Michael Niedermayer
  6. *
  7. * Support for external huffman table, various fixes (AVID workaround),
  8. * aspecting, new decode_frame mechanism and apple mjpeg-b support
  9. * by Alex Beregszaszi
  10. *
  11. * This file is part of FFmpeg.
  12. *
  13. * FFmpeg is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * FFmpeg is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with FFmpeg; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /**
  28. * @file
  29. * MJPEG encoder.
  30. */
  31. #include "config_components.h"
  32. #include "libavutil/mem.h"
  33. #include "avcodec.h"
  34. #include "codec_internal.h"
  35. #include "jpegtables.h"
  36. #include "mjpegenc_common.h"
  37. #include "mjpegenc_huffman.h"
  38. #include "mpegvideo.h"
  39. #include "mjpeg.h"
  40. #include "mjpegenc.h"
  41. #include "mpegvideoenc.h"
  42. #include "profiles.h"
  43. /**
  44. * Buffer of JPEG frame data.
  45. *
  46. * Optimal Huffman table generation requires the frame data to be loaded into
  47. * a buffer so that the tables can be computed.
  48. * There are at most mb_width*mb_height*12*64 of these per frame.
  49. */
  50. typedef struct MJpegHuffmanCode {
  51. // 0=DC lum, 1=DC chrom, 2=AC lum, 3=AC chrom
  52. uint8_t table_id; ///< The Huffman table id associated with the data.
  53. uint8_t code; ///< The exponent.
  54. uint16_t mant; ///< The mantissa.
  55. } MJpegHuffmanCode;
  56. /* The following is the private context of MJPEG/AMV decoder.
  57. * Note that when using slice threading only the main thread's
  58. * MPVEncContext is followed by a MjpegContext; the other threads
  59. * can access this shared context via MPVEncContext.mjpeg. */
  60. typedef struct MJPEGEncContext {
  61. MPVMainEncContext mpeg;
  62. MJpegContext mjpeg;
  63. } MJPEGEncContext;
  64. static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256],
  65. uint8_t *uni_ac_vlc_len)
  66. {
  67. for (int i = 0; i < 128; i++) {
  68. int level = i - 64;
  69. if (!level)
  70. continue;
  71. for (int run = 0; run < 64; run++) {
  72. int len, code, nbits;
  73. int alevel = FFABS(level);
  74. len = (run >> 4) * huff_size_ac[0xf0];
  75. nbits= av_log2_16bit(alevel) + 1;
  76. code = ((15&run) << 4) | nbits;
  77. len += huff_size_ac[code] + nbits;
  78. uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
  79. // We ignore EOB as its just a constant which does not change generally
  80. }
  81. }
  82. }
  83. static void mjpeg_encode_picture_header(MPVEncContext *const s)
  84. {
  85. ff_mjpeg_encode_picture_header(s->c.avctx, &s->pb, s->c.cur_pic.ptr->f, s->mjpeg_ctx,
  86. s->c.intra_scantable.permutated, 0,
  87. s->c.intra_matrix, s->c.chroma_intra_matrix,
  88. s->c.slice_context_count > 1);
  89. s->esc_pos = put_bytes_count(&s->pb, 0);
  90. for (int i = 1; i < s->c.slice_context_count; i++)
  91. s->c.enc_contexts[i]->esc_pos = 0;
  92. }
  93. static int mjpeg_amv_encode_picture_header(MPVMainEncContext *const m)
  94. {
  95. MJPEGEncContext *const m2 = (MJPEGEncContext*)m;
  96. MPVEncContext *const s = &m->s;
  97. av_assert2(s->mjpeg_ctx == &m2->mjpeg);
  98. /* s->huffman == HUFFMAN_TABLE_OPTIMAL can only be true for MJPEG. */
  99. if (!CONFIG_MJPEG_ENCODER || m2->mjpeg.huffman != HUFFMAN_TABLE_OPTIMAL)
  100. mjpeg_encode_picture_header(s);
  101. return 0;
  102. }
  103. #if CONFIG_MJPEG_ENCODER
  104. /**
  105. * Encodes and outputs the entire frame in the JPEG format.
  106. *
  107. * @param main The MPVMainEncContext.
  108. */
  109. static void mjpeg_encode_picture_frame(MPVMainEncContext *const main)
  110. {
  111. MPVEncContext *const s = &main->s;
  112. int nbits, code, table_id;
  113. MJpegContext *m = s->mjpeg_ctx;
  114. uint8_t *huff_size[4] = { m->huff_size_dc_luminance,
  115. m->huff_size_dc_chrominance,
  116. m->huff_size_ac_luminance,
  117. m->huff_size_ac_chrominance };
  118. uint16_t *huff_code[4] = { m->huff_code_dc_luminance,
  119. m->huff_code_dc_chrominance,
  120. m->huff_code_ac_luminance,
  121. m->huff_code_ac_chrominance };
  122. size_t total_bits = 0;
  123. size_t bytes_needed;
  124. main->header_bits = get_bits_diff(s);
  125. // Estimate the total size first
  126. for (int i = 0; i < m->huff_ncode; i++) {
  127. table_id = m->huff_buffer[i].table_id;
  128. code = m->huff_buffer[i].code;
  129. nbits = code & 0xf;
  130. total_bits += huff_size[table_id][code] + nbits;
  131. }
  132. bytes_needed = (total_bits + 7) / 8;
  133. ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
  134. for (int i = 0; i < m->huff_ncode; i++) {
  135. table_id = m->huff_buffer[i].table_id;
  136. code = m->huff_buffer[i].code;
  137. nbits = code & 0xf;
  138. put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
  139. if (nbits != 0) {
  140. put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
  141. }
  142. }
  143. m->huff_ncode = 0;
  144. s->i_tex_bits = get_bits_diff(s);
  145. }
  146. /**
  147. * Builds all 4 optimal Huffman tables.
  148. *
  149. * Uses the data stored in the JPEG buffer to compute the tables.
  150. * Stores the Huffman tables in the bits_* and val_* arrays in the MJpegContext.
  151. *
  152. * @param m MJpegContext containing the JPEG buffer.
  153. */
  154. static void mjpeg_build_optimal_huffman(MJpegContext *m)
  155. {
  156. MJpegEncHuffmanContext dc_luminance_ctx;
  157. MJpegEncHuffmanContext dc_chrominance_ctx;
  158. MJpegEncHuffmanContext ac_luminance_ctx;
  159. MJpegEncHuffmanContext ac_chrominance_ctx;
  160. MJpegEncHuffmanContext *ctx[4] = { &dc_luminance_ctx,
  161. &dc_chrominance_ctx,
  162. &ac_luminance_ctx,
  163. &ac_chrominance_ctx };
  164. for (int i = 0; i < 4; i++)
  165. ff_mjpeg_encode_huffman_init(ctx[i]);
  166. for (int i = 0; i < m->huff_ncode; i++) {
  167. int table_id = m->huff_buffer[i].table_id;
  168. int code = m->huff_buffer[i].code;
  169. ff_mjpeg_encode_huffman_increment(ctx[table_id], code);
  170. }
  171. ff_mjpeg_encode_huffman_close(&dc_luminance_ctx,
  172. m->bits_dc_luminance,
  173. m->val_dc_luminance, 12);
  174. ff_mjpeg_encode_huffman_close(&dc_chrominance_ctx,
  175. m->bits_dc_chrominance,
  176. m->val_dc_chrominance, 12);
  177. ff_mjpeg_encode_huffman_close(&ac_luminance_ctx,
  178. m->bits_ac_luminance,
  179. m->val_ac_luminance, 256);
  180. ff_mjpeg_encode_huffman_close(&ac_chrominance_ctx,
  181. m->bits_ac_chrominance,
  182. m->val_ac_chrominance, 256);
  183. ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
  184. m->huff_code_dc_luminance,
  185. m->bits_dc_luminance,
  186. m->val_dc_luminance);
  187. ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
  188. m->huff_code_dc_chrominance,
  189. m->bits_dc_chrominance,
  190. m->val_dc_chrominance);
  191. ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
  192. m->huff_code_ac_luminance,
  193. m->bits_ac_luminance,
  194. m->val_ac_luminance);
  195. ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
  196. m->huff_code_ac_chrominance,
  197. m->bits_ac_chrominance,
  198. m->val_ac_chrominance);
  199. }
  200. #endif
  201. /**
  202. * Writes the complete JPEG frame when optimal huffman tables are enabled,
  203. * otherwise writes the stuffing.
  204. *
  205. * Header + values + stuffing.
  206. *
  207. * @param s The MPVEncContext.
  208. * @return int Error code, 0 if successful.
  209. */
  210. int ff_mjpeg_encode_stuffing(MPVEncContext *const s)
  211. {
  212. MJpegContext *const m = s->mjpeg_ctx;
  213. PutBitContext *pbc = &s->pb;
  214. int mb_y = s->c.mb_y - !s->c.mb_x;
  215. int ret;
  216. #if CONFIG_MJPEG_ENCODER
  217. if (m->huffman == HUFFMAN_TABLE_OPTIMAL) {
  218. /* HUFFMAN_TABLE_OPTIMAL is incompatible with slice threading,
  219. * therefore the following cast is allowed. */
  220. MPVMainEncContext *const main = (MPVMainEncContext*)s;
  221. mjpeg_build_optimal_huffman(m);
  222. // Replace the VLCs with the optimal ones.
  223. // The default ones may be used for trellis during quantization.
  224. init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
  225. init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
  226. s->intra_ac_vlc_length =
  227. s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
  228. s->intra_chroma_ac_vlc_length =
  229. s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
  230. mjpeg_encode_picture_header(s);
  231. mjpeg_encode_picture_frame(main);
  232. }
  233. #endif
  234. ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
  235. put_bits_count(&s->pb) / 4 + 1000);
  236. if (ret < 0) {
  237. av_log(s->c.avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
  238. goto fail;
  239. }
  240. ff_mjpeg_escape_FF(pbc, s->esc_pos);
  241. if (s->c.slice_context_count > 1 && mb_y < s->c.mb_height - 1)
  242. put_marker(pbc, RST0 + (mb_y&7));
  243. s->esc_pos = put_bytes_count(pbc, 0);
  244. fail:
  245. for (int i = 0; i < 3; i++)
  246. s->last_dc[i] = 128 << s->c.intra_dc_precision;
  247. return ret;
  248. }
  249. static int alloc_huffman(MJPEGEncContext *const m2)
  250. {
  251. MJpegContext *const m = &m2->mjpeg;
  252. MPVEncContext *const s = &m2->mpeg.s;
  253. static const char blocks_per_mb[] = {
  254. [CHROMA_420] = 6, [CHROMA_422] = 8, [CHROMA_444] = 12
  255. };
  256. size_t num_blocks;
  257. // Make sure we have enough space to hold this frame.
  258. num_blocks = s->c.mb_num * blocks_per_mb[s->c.chroma_format];
  259. m->huff_buffer = av_malloc_array(num_blocks,
  260. 64 /* codes per MB */ * sizeof(MJpegHuffmanCode));
  261. if (!m->huff_buffer)
  262. return AVERROR(ENOMEM);
  263. return 0;
  264. }
  265. static av_cold int mjpeg_encode_close(AVCodecContext *avctx)
  266. {
  267. MJPEGEncContext *const mjpeg = avctx->priv_data;
  268. av_freep(&mjpeg->mjpeg.huff_buffer);
  269. ff_mpv_encode_end(avctx);
  270. return 0;
  271. }
  272. /**
  273. * Add code and table_id to the JPEG buffer.
  274. *
  275. * @param s The MJpegContext which contains the JPEG buffer.
  276. * @param table_id Which Huffman table the code belongs to.
  277. * @param code The encoded exponent of the coefficients and the run-bits.
  278. */
  279. static inline void mjpeg_encode_code(MJpegContext *s, uint8_t table_id, int code)
  280. {
  281. MJpegHuffmanCode *c = &s->huff_buffer[s->huff_ncode++];
  282. c->table_id = table_id;
  283. c->code = code;
  284. }
  285. /**
  286. * Add the coefficient's data to the JPEG buffer.
  287. *
  288. * @param s The MJpegContext which contains the JPEG buffer.
  289. * @param table_id Which Huffman table the code belongs to.
  290. * @param val The coefficient.
  291. * @param run The run-bits.
  292. */
  293. static void mjpeg_encode_coef(MJpegContext *s, uint8_t table_id, int val, int run)
  294. {
  295. int mant, code;
  296. if (val == 0) {
  297. av_assert0(run == 0);
  298. mjpeg_encode_code(s, table_id, 0);
  299. } else {
  300. mant = val;
  301. if (val < 0) {
  302. val = -val;
  303. mant--;
  304. }
  305. code = (run << 4) | (av_log2_16bit(val) + 1);
  306. s->huff_buffer[s->huff_ncode].mant = mant;
  307. mjpeg_encode_code(s, table_id, code);
  308. }
  309. }
  310. /**
  311. * Add the block's data into the JPEG buffer.
  312. *
  313. * @param s The MPVEncContext that contains the JPEG buffer.
  314. * @param block The block.
  315. * @param n The block's index or number.
  316. */
  317. static void record_block(MPVEncContext *const s, int16_t block[], int n)
  318. {
  319. int i, j, table_id;
  320. int component, dc, last_index, val, run;
  321. MJpegContext *m = s->mjpeg_ctx;
  322. /* DC coef */
  323. component = (n <= 3 ? 0 : (n&1) + 1);
  324. table_id = (n <= 3 ? 0 : 1);
  325. dc = block[0]; /* overflow is impossible */
  326. val = dc - s->last_dc[component];
  327. mjpeg_encode_coef(m, table_id, val, 0);
  328. s->last_dc[component] = dc;
  329. /* AC coefs */
  330. run = 0;
  331. last_index = s->c.block_last_index[n];
  332. table_id |= 2;
  333. for(i=1;i<=last_index;i++) {
  334. j = s->c.intra_scantable.permutated[i];
  335. val = block[j];
  336. if (val == 0) {
  337. run++;
  338. } else {
  339. while (run >= 16) {
  340. mjpeg_encode_code(m, table_id, 0xf0);
  341. run -= 16;
  342. }
  343. mjpeg_encode_coef(m, table_id, val, run);
  344. run = 0;
  345. }
  346. }
  347. /* output EOB only if not already 64 values */
  348. if (last_index < 63 || run != 0)
  349. mjpeg_encode_code(m, table_id, 0);
  350. }
  351. static void encode_block(MPVEncContext *const s, int16_t block[], int n)
  352. {
  353. int mant, nbits, code, i, j;
  354. int component, dc, run, last_index, val;
  355. const MJpegContext *const m = s->mjpeg_ctx;
  356. const uint16_t *huff_code_ac;
  357. const uint8_t *huff_size_ac;
  358. /* DC coef */
  359. component = (n <= 3 ? 0 : (n&1) + 1);
  360. dc = block[0]; /* overflow is impossible */
  361. val = dc - s->last_dc[component];
  362. if (n < 4) {
  363. ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  364. huff_size_ac = m->huff_size_ac_luminance;
  365. huff_code_ac = m->huff_code_ac_luminance;
  366. } else {
  367. ff_mjpeg_encode_dc(&s->pb, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  368. huff_size_ac = m->huff_size_ac_chrominance;
  369. huff_code_ac = m->huff_code_ac_chrominance;
  370. }
  371. s->last_dc[component] = dc;
  372. /* AC coefs */
  373. run = 0;
  374. last_index = s->c.block_last_index[n];
  375. for(i=1;i<=last_index;i++) {
  376. j = s->c.intra_scantable.permutated[i];
  377. val = block[j];
  378. if (val == 0) {
  379. run++;
  380. } else {
  381. while (run >= 16) {
  382. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  383. run -= 16;
  384. }
  385. mant = val;
  386. if (val < 0) {
  387. val = -val;
  388. mant--;
  389. }
  390. nbits= av_log2_16bit(val) + 1;
  391. code = (run << 4) | nbits;
  392. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  393. put_sbits(&s->pb, nbits, mant);
  394. run = 0;
  395. }
  396. }
  397. /* output EOB only if not already 64 values */
  398. if (last_index < 63 || run != 0)
  399. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  400. }
  401. static void mjpeg_record_mb(MPVEncContext *const s, int16_t block[][64],
  402. int unused_x, int unused_y)
  403. {
  404. if (s->c.chroma_format == CHROMA_444) {
  405. record_block(s, block[0], 0);
  406. record_block(s, block[2], 2);
  407. record_block(s, block[4], 4);
  408. record_block(s, block[8], 8);
  409. record_block(s, block[5], 5);
  410. record_block(s, block[9], 9);
  411. if (16*s->c.mb_x+8 < s->c.width) {
  412. record_block(s, block[1], 1);
  413. record_block(s, block[3], 3);
  414. record_block(s, block[6], 6);
  415. record_block(s, block[10], 10);
  416. record_block(s, block[7], 7);
  417. record_block(s, block[11], 11);
  418. }
  419. } else {
  420. for (int i = 0; i < 5; i++)
  421. record_block(s, block[i], i);
  422. if (s->c.chroma_format == CHROMA_420) {
  423. record_block(s, block[5], 5);
  424. } else {
  425. record_block(s, block[6], 6);
  426. record_block(s, block[5], 5);
  427. record_block(s, block[7], 7);
  428. }
  429. }
  430. }
  431. static void mjpeg_encode_mb(MPVEncContext *const s, int16_t block[][64],
  432. int unused_x, int unused_y)
  433. {
  434. if (s->c.chroma_format == CHROMA_444) {
  435. encode_block(s, block[0], 0);
  436. encode_block(s, block[2], 2);
  437. encode_block(s, block[4], 4);
  438. encode_block(s, block[8], 8);
  439. encode_block(s, block[5], 5);
  440. encode_block(s, block[9], 9);
  441. if (16 * s->c.mb_x + 8 < s->c.width) {
  442. encode_block(s, block[1], 1);
  443. encode_block(s, block[3], 3);
  444. encode_block(s, block[6], 6);
  445. encode_block(s, block[10], 10);
  446. encode_block(s, block[7], 7);
  447. encode_block(s, block[11], 11);
  448. }
  449. } else {
  450. for (int i = 0; i < 5; i++)
  451. encode_block(s, block[i], i);
  452. if (s->c.chroma_format == CHROMA_420) {
  453. encode_block(s, block[5], 5);
  454. } else {
  455. encode_block(s, block[6], 6);
  456. encode_block(s, block[5], 5);
  457. encode_block(s, block[7], 7);
  458. }
  459. }
  460. s->i_tex_bits += get_bits_diff(s);
  461. }
  462. static av_cold int mjpeg_encode_init(AVCodecContext *avctx)
  463. {
  464. MJPEGEncContext *const m2 = avctx->priv_data;
  465. MJpegContext *const m = &m2->mjpeg;
  466. MPVEncContext *const s = &m2->mpeg.s;
  467. int ret;
  468. s->mjpeg_ctx = m;
  469. m2->mpeg.encode_picture_header = mjpeg_amv_encode_picture_header;
  470. // May be overridden below
  471. s->encode_mb = mjpeg_encode_mb;
  472. if (s->mpv_flags & FF_MPV_FLAG_QP_RD) {
  473. // Used to produce garbage with MJPEG.
  474. av_log(avctx, AV_LOG_ERROR,
  475. "QP RD is no longer compatible with MJPEG or AMV\n");
  476. return AVERROR(EINVAL);
  477. }
  478. /* The following check is automatically true for AMV,
  479. * but it doesn't hurt either. */
  480. ret = ff_mjpeg_encode_check_pix_fmt(avctx);
  481. if (ret < 0)
  482. return ret;
  483. if (avctx->width > 65500 || avctx->height > 65500) {
  484. av_log(avctx, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
  485. return AVERROR(EINVAL);
  486. }
  487. // Build default Huffman tables.
  488. // These may be overwritten later with more optimal Huffman tables, but
  489. // they are needed at least right now for some processes like trellis.
  490. ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
  491. m->huff_code_dc_luminance,
  492. ff_mjpeg_bits_dc_luminance,
  493. ff_mjpeg_val_dc);
  494. ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
  495. m->huff_code_dc_chrominance,
  496. ff_mjpeg_bits_dc_chrominance,
  497. ff_mjpeg_val_dc);
  498. ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
  499. m->huff_code_ac_luminance,
  500. ff_mjpeg_bits_ac_luminance,
  501. ff_mjpeg_val_ac_luminance);
  502. ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
  503. m->huff_code_ac_chrominance,
  504. ff_mjpeg_bits_ac_chrominance,
  505. ff_mjpeg_val_ac_chrominance);
  506. init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
  507. init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
  508. s->min_qcoeff = -1023;
  509. s->max_qcoeff = 1023;
  510. s->intra_ac_vlc_length =
  511. s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
  512. s->intra_chroma_ac_vlc_length =
  513. s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
  514. ret = ff_mpv_encode_init(avctx);
  515. if (ret < 0)
  516. return ret;
  517. // Buffers start out empty.
  518. m->huff_ncode = 0;
  519. if (s->c.slice_context_count > 1)
  520. m->huffman = HUFFMAN_TABLE_DEFAULT;
  521. if (m->huffman == HUFFMAN_TABLE_OPTIMAL) {
  522. // If we are here, we have only one slice_context. So no loop necessary.
  523. s->encode_mb = mjpeg_record_mb;
  524. return alloc_huffman(m2);
  525. }
  526. return 0;
  527. }
  528. #if CONFIG_AMV_ENCODER
  529. // maximum over s->mjpeg_vsample[i]
  530. #define V_MAX 2
  531. static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  532. const AVFrame *pic_arg, int *got_packet)
  533. {
  534. MPVEncContext *const s = avctx->priv_data;
  535. AVFrame *pic;
  536. int i, ret;
  537. int chroma_v_shift = 1; /* AMV is 420-only */
  538. if ((avctx->height & 15) && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
  539. av_log(avctx, AV_LOG_ERROR,
  540. "Heights which are not a multiple of 16 might fail with some decoders, "
  541. "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->height);
  542. av_log(avctx, AV_LOG_WARNING, "If you have a device that plays AMV videos, please test if videos "
  543. "with such heights work with it and report your findings to ffmpeg-devel@ffmpeg.org\n");
  544. return AVERROR_EXPERIMENTAL;
  545. }
  546. pic = av_frame_clone(pic_arg);
  547. if (!pic)
  548. return AVERROR(ENOMEM);
  549. //picture should be flipped upside-down
  550. for(i=0; i < 3; i++) {
  551. int vsample = i ? 2 >> chroma_v_shift : 2;
  552. pic->data[i] += pic->linesize[i] * (vsample * s->c.height / V_MAX - 1);
  553. pic->linesize[i] *= -1;
  554. }
  555. ret = ff_mpv_encode_picture(avctx, pkt, pic, got_packet);
  556. av_frame_free(&pic);
  557. return ret;
  558. }
  559. #endif
  560. #define OFFSET(x) offsetof(MJPEGEncContext, mjpeg.x)
  561. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  562. static const AVOption options[] = {
  563. #define AMV_OPTIONS_OFFSET 4
  564. { "huffman", "Huffman table strategy", OFFSET(huffman), AV_OPT_TYPE_INT, { .i64 = HUFFMAN_TABLE_OPTIMAL }, 0, NB_HUFFMAN_TABLE_OPTION - 1, VE, .unit = "huffman" },
  565. { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_DEFAULT }, INT_MIN, INT_MAX, VE, .unit = "huffman" },
  566. { "optimal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = HUFFMAN_TABLE_OPTIMAL }, INT_MIN, INT_MAX, VE, .unit = "huffman" },
  567. { "force_duplicated_matrix", "Always write luma and chroma matrix for mjpeg, useful for rtp streaming.", OFFSET(force_duplicated_matrix), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, VE },
  568. FF_MPV_COMMON_OPTS
  569. { NULL},
  570. };
  571. #if CONFIG_MJPEG_ENCODER
  572. static const AVClass mjpeg_class = {
  573. .class_name = "mjpeg encoder",
  574. .item_name = av_default_item_name,
  575. .option = options,
  576. .version = LIBAVUTIL_VERSION_INT,
  577. };
  578. static int mjpeg_get_supported_config(const AVCodecContext *avctx,
  579. const AVCodec *codec,
  580. enum AVCodecConfig config,
  581. unsigned flags, const void **out,
  582. int *out_num)
  583. {
  584. if (config == AV_CODEC_CONFIG_COLOR_RANGE) {
  585. static const enum AVColorRange mjpeg_ranges[] = {
  586. AVCOL_RANGE_MPEG, AVCOL_RANGE_JPEG, AVCOL_RANGE_UNSPECIFIED,
  587. };
  588. int strict = avctx ? avctx->strict_std_compliance : 0;
  589. int index = strict > FF_COMPLIANCE_UNOFFICIAL ? 1 : 0;
  590. *out = &mjpeg_ranges[index];
  591. *out_num = FF_ARRAY_ELEMS(mjpeg_ranges) - index - 1;
  592. return 0;
  593. }
  594. return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
  595. }
  596. const FFCodec ff_mjpeg_encoder = {
  597. .p.name = "mjpeg",
  598. CODEC_LONG_NAME("MJPEG (Motion JPEG)"),
  599. .p.type = AVMEDIA_TYPE_VIDEO,
  600. .p.id = AV_CODEC_ID_MJPEG,
  601. .priv_data_size = sizeof(MJPEGEncContext),
  602. .init = mjpeg_encode_init,
  603. FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
  604. .close = mjpeg_encode_close,
  605. .p.capabilities = AV_CODEC_CAP_DR1 |
  606. AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS |
  607. AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
  608. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_ICC_PROFILES,
  609. CODEC_PIXFMTS(AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
  610. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P),
  611. .p.priv_class = &mjpeg_class,
  612. .p.profiles = NULL_IF_CONFIG_SMALL(ff_mjpeg_profiles),
  613. .get_supported_config = mjpeg_get_supported_config,
  614. };
  615. #endif
  616. #if CONFIG_AMV_ENCODER
  617. static const AVClass amv_class = {
  618. .class_name = "amv encoder",
  619. .item_name = av_default_item_name,
  620. .option = options + AMV_OPTIONS_OFFSET,
  621. .version = LIBAVUTIL_VERSION_INT,
  622. };
  623. const FFCodec ff_amv_encoder = {
  624. .p.name = "amv",
  625. CODEC_LONG_NAME("AMV Video"),
  626. .p.type = AVMEDIA_TYPE_VIDEO,
  627. .p.id = AV_CODEC_ID_AMV,
  628. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
  629. .priv_data_size = sizeof(MJPEGEncContext),
  630. .init = mjpeg_encode_init,
  631. FF_CODEC_ENCODE_CB(amv_encode_picture),
  632. .close = mjpeg_encode_close,
  633. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  634. CODEC_PIXFMTS(AV_PIX_FMT_YUVJ420P),
  635. .color_ranges = AVCOL_RANGE_JPEG,
  636. .p.priv_class = &amv_class,
  637. };
  638. #endif