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.

529 lines
15KB

  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. //#define DEBUG
  32. #include <assert.h>
  33. #include "avcodec.h"
  34. #include "dsputil.h"
  35. #include "mpegvideo.h"
  36. #include "mjpeg.h"
  37. #include "mjpegenc.h"
  38. /* use two quantizer tables (one for luminance and one for chrominance) */
  39. /* not yet working */
  40. #undef TWOMATRIXES
  41. av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
  42. {
  43. MJpegContext *m;
  44. if (s->width > 65500 || s->height > 65500) {
  45. av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
  46. return -1;
  47. }
  48. m = av_malloc(sizeof(MJpegContext));
  49. if (!m)
  50. return -1;
  51. s->min_qcoeff=-1023;
  52. s->max_qcoeff= 1023;
  53. /* build all the huffman tables */
  54. ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
  55. m->huff_code_dc_luminance,
  56. ff_mjpeg_bits_dc_luminance,
  57. ff_mjpeg_val_dc);
  58. ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
  59. m->huff_code_dc_chrominance,
  60. ff_mjpeg_bits_dc_chrominance,
  61. ff_mjpeg_val_dc);
  62. ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
  63. m->huff_code_ac_luminance,
  64. ff_mjpeg_bits_ac_luminance,
  65. ff_mjpeg_val_ac_luminance);
  66. ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
  67. m->huff_code_ac_chrominance,
  68. ff_mjpeg_bits_ac_chrominance,
  69. ff_mjpeg_val_ac_chrominance);
  70. s->mjpeg_ctx = m;
  71. return 0;
  72. }
  73. void ff_mjpeg_encode_close(MpegEncContext *s)
  74. {
  75. av_free(s->mjpeg_ctx);
  76. }
  77. /* table_class: 0 = DC coef, 1 = AC coefs */
  78. static int put_huffman_table(MpegEncContext *s, int table_class, int table_id,
  79. const uint8_t *bits_table, const uint8_t *value_table)
  80. {
  81. PutBitContext *p = &s->pb;
  82. int n, i;
  83. put_bits(p, 4, table_class);
  84. put_bits(p, 4, table_id);
  85. n = 0;
  86. for(i=1;i<=16;i++) {
  87. n += bits_table[i];
  88. put_bits(p, 8, bits_table[i]);
  89. }
  90. for(i=0;i<n;i++)
  91. put_bits(p, 8, value_table[i]);
  92. return n + 17;
  93. }
  94. static void jpeg_table_header(MpegEncContext *s)
  95. {
  96. PutBitContext *p = &s->pb;
  97. int i, j, size;
  98. uint8_t *ptr;
  99. /* quant matrixes */
  100. put_marker(p, DQT);
  101. #ifdef TWOMATRIXES
  102. put_bits(p, 16, 2 + 2 * (1 + 64));
  103. #else
  104. put_bits(p, 16, 2 + 1 * (1 + 64));
  105. #endif
  106. put_bits(p, 4, 0); /* 8 bit precision */
  107. put_bits(p, 4, 0); /* table 0 */
  108. for(i=0;i<64;i++) {
  109. j = s->intra_scantable.permutated[i];
  110. put_bits(p, 8, s->intra_matrix[j]);
  111. }
  112. #ifdef TWOMATRIXES
  113. put_bits(p, 4, 0); /* 8 bit precision */
  114. put_bits(p, 4, 1); /* table 1 */
  115. for(i=0;i<64;i++) {
  116. j = s->intra_scantable.permutated[i];
  117. put_bits(p, 8, s->chroma_intra_matrix[j]);
  118. }
  119. #endif
  120. if(s->avctx->active_thread_type & FF_THREAD_SLICE){
  121. put_marker(p, DRI);
  122. put_bits(p, 16, 4);
  123. put_bits(p, 16, s->mb_width);
  124. }
  125. /* huffman table */
  126. put_marker(p, DHT);
  127. flush_put_bits(p);
  128. ptr = put_bits_ptr(p);
  129. put_bits(p, 16, 0); /* patched later */
  130. size = 2;
  131. size += put_huffman_table(s, 0, 0, ff_mjpeg_bits_dc_luminance,
  132. ff_mjpeg_val_dc);
  133. size += put_huffman_table(s, 0, 1, ff_mjpeg_bits_dc_chrominance,
  134. ff_mjpeg_val_dc);
  135. size += put_huffman_table(s, 1, 0, ff_mjpeg_bits_ac_luminance,
  136. ff_mjpeg_val_ac_luminance);
  137. size += put_huffman_table(s, 1, 1, ff_mjpeg_bits_ac_chrominance,
  138. ff_mjpeg_val_ac_chrominance);
  139. AV_WB16(ptr, size);
  140. }
  141. static void jpeg_put_comments(MpegEncContext *s)
  142. {
  143. PutBitContext *p = &s->pb;
  144. int size;
  145. uint8_t *ptr;
  146. if (s->avctx->sample_aspect_ratio.num /* && !lossless */)
  147. {
  148. /* JFIF header */
  149. put_marker(p, APP0);
  150. put_bits(p, 16, 16);
  151. ff_put_string(p, "JFIF", 1); /* this puts the trailing zero-byte too */
  152. put_bits(p, 16, 0x0102); /* v 1.02 */
  153. put_bits(p, 8, 0); /* units type: 0 - aspect ratio */
  154. put_bits(p, 16, s->avctx->sample_aspect_ratio.num);
  155. put_bits(p, 16, s->avctx->sample_aspect_ratio.den);
  156. put_bits(p, 8, 0); /* thumbnail width */
  157. put_bits(p, 8, 0); /* thumbnail height */
  158. }
  159. /* comment */
  160. if(!(s->flags & CODEC_FLAG_BITEXACT)){
  161. put_marker(p, COM);
  162. flush_put_bits(p);
  163. ptr = put_bits_ptr(p);
  164. put_bits(p, 16, 0); /* patched later */
  165. ff_put_string(p, LIBAVCODEC_IDENT, 1);
  166. size = strlen(LIBAVCODEC_IDENT)+3;
  167. AV_WB16(ptr, size);
  168. }
  169. if( s->avctx->pix_fmt == PIX_FMT_YUV420P
  170. ||s->avctx->pix_fmt == PIX_FMT_YUV422P
  171. ||s->avctx->pix_fmt == PIX_FMT_YUV444P){
  172. put_marker(p, COM);
  173. flush_put_bits(p);
  174. ptr = put_bits_ptr(p);
  175. put_bits(p, 16, 0); /* patched later */
  176. ff_put_string(p, "CS=ITU601", 1);
  177. size = strlen("CS=ITU601")+3;
  178. AV_WB16(ptr, size);
  179. }
  180. }
  181. void ff_mjpeg_encode_picture_header(MpegEncContext *s)
  182. {
  183. const int lossless= s->avctx->codec_id != CODEC_ID_MJPEG;
  184. int i;
  185. put_marker(&s->pb, SOI);
  186. // hack for AMV mjpeg format
  187. if(s->avctx->codec_id == CODEC_ID_AMV) goto end;
  188. jpeg_put_comments(s);
  189. jpeg_table_header(s);
  190. switch(s->avctx->codec_id){
  191. case CODEC_ID_MJPEG: put_marker(&s->pb, SOF0 ); break;
  192. case CODEC_ID_LJPEG: put_marker(&s->pb, SOF3 ); break;
  193. default: assert(0);
  194. }
  195. put_bits(&s->pb, 16, 17);
  196. if(lossless && (s->avctx->pix_fmt == PIX_FMT_BGR0
  197. || s->avctx->pix_fmt == PIX_FMT_BGRA
  198. || s->avctx->pix_fmt == PIX_FMT_BGR24))
  199. put_bits(&s->pb, 8, 9); /* 9 bits/component RCT */
  200. else
  201. put_bits(&s->pb, 8, 8); /* 8 bits/component */
  202. put_bits(&s->pb, 16, s->height);
  203. put_bits(&s->pb, 16, s->width);
  204. put_bits(&s->pb, 8, 3); /* 3 components */
  205. /* Y component */
  206. put_bits(&s->pb, 8, 1); /* component number */
  207. put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */
  208. put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */
  209. put_bits(&s->pb, 8, 0); /* select matrix */
  210. /* Cb component */
  211. put_bits(&s->pb, 8, 2); /* component number */
  212. put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */
  213. put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */
  214. #ifdef TWOMATRIXES
  215. put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
  216. #else
  217. put_bits(&s->pb, 8, 0); /* select matrix */
  218. #endif
  219. /* Cr component */
  220. put_bits(&s->pb, 8, 3); /* component number */
  221. put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */
  222. put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */
  223. #ifdef TWOMATRIXES
  224. put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */
  225. #else
  226. put_bits(&s->pb, 8, 0); /* select matrix */
  227. #endif
  228. /* scan header */
  229. put_marker(&s->pb, SOS);
  230. put_bits(&s->pb, 16, 12); /* length */
  231. put_bits(&s->pb, 8, 3); /* 3 components */
  232. /* Y component */
  233. put_bits(&s->pb, 8, 1); /* index */
  234. put_bits(&s->pb, 4, 0); /* DC huffman table index */
  235. put_bits(&s->pb, 4, 0); /* AC huffman table index */
  236. /* Cb component */
  237. put_bits(&s->pb, 8, 2); /* index */
  238. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  239. put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  240. /* Cr component */
  241. put_bits(&s->pb, 8, 3); /* index */
  242. put_bits(&s->pb, 4, 1); /* DC huffman table index */
  243. put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */
  244. put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */
  245. switch(s->avctx->codec_id){
  246. case CODEC_ID_MJPEG: put_bits(&s->pb, 8, 63); break; /* Se (not used) */
  247. case CODEC_ID_LJPEG: put_bits(&s->pb, 8, 0); break; /* not used */
  248. default: assert(0);
  249. }
  250. put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */
  251. end:
  252. s->esc_pos = put_bits_count(&s->pb) >> 3;
  253. for(i=1; i<s->slice_context_count; i++)
  254. s->thread_context[i]->esc_pos = 0;
  255. }
  256. static void escape_FF(MpegEncContext *s, int start)
  257. {
  258. int size= put_bits_count(&s->pb) - start*8;
  259. int i, ff_count;
  260. uint8_t *buf= s->pb.buf + start;
  261. int align= (-(size_t)(buf))&3;
  262. assert((size&7) == 0);
  263. size >>= 3;
  264. ff_count=0;
  265. for(i=0; i<size && i<align; i++){
  266. if(buf[i]==0xFF) ff_count++;
  267. }
  268. for(; i<size-15; i+=16){
  269. int acc, v;
  270. v= *(uint32_t*)(&buf[i]);
  271. acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  272. v= *(uint32_t*)(&buf[i+4]);
  273. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  274. v= *(uint32_t*)(&buf[i+8]);
  275. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  276. v= *(uint32_t*)(&buf[i+12]);
  277. acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;
  278. acc>>=4;
  279. acc+= (acc>>16);
  280. acc+= (acc>>8);
  281. ff_count+= acc&0xFF;
  282. }
  283. for(; i<size; i++){
  284. if(buf[i]==0xFF) ff_count++;
  285. }
  286. if(ff_count==0) return;
  287. flush_put_bits(&s->pb);
  288. skip_put_bytes(&s->pb, ff_count);
  289. for(i=size-1; ff_count; i--){
  290. int v= buf[i];
  291. if(v==0xFF){
  292. //printf("%d %d\n", i, ff_count);
  293. buf[i+ff_count]= 0;
  294. ff_count--;
  295. }
  296. buf[i+ff_count]= v;
  297. }
  298. }
  299. void ff_mjpeg_encode_stuffing(MpegEncContext *s)
  300. {
  301. int length, i;
  302. PutBitContext *pbc = &s->pb;
  303. int mb_y = s->mb_y - !s->mb_x;
  304. length= (-put_bits_count(pbc))&7;
  305. if(length) put_bits(pbc, length, (1<<length)-1);
  306. flush_put_bits(&s->pb);
  307. escape_FF(s, s->esc_pos);
  308. if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height)
  309. put_marker(pbc, RST0 + (mb_y&7));
  310. s->esc_pos = put_bits_count(pbc) >> 3;
  311. for(i=0; i<3; i++)
  312. s->last_dc[i] = 128 << s->intra_dc_precision;
  313. }
  314. void ff_mjpeg_encode_picture_trailer(MpegEncContext *s)
  315. {
  316. assert((s->header_bits&7)==0);
  317. put_marker(&s->pb, EOI);
  318. }
  319. void ff_mjpeg_encode_dc(MpegEncContext *s, int val,
  320. uint8_t *huff_size, uint16_t *huff_code)
  321. {
  322. int mant, nbits;
  323. if (val == 0) {
  324. put_bits(&s->pb, huff_size[0], huff_code[0]);
  325. } else {
  326. mant = val;
  327. if (val < 0) {
  328. val = -val;
  329. mant--;
  330. }
  331. nbits= av_log2_16bit(val) + 1;
  332. put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);
  333. put_sbits(&s->pb, nbits, mant);
  334. }
  335. }
  336. static void encode_block(MpegEncContext *s, DCTELEM *block, int n)
  337. {
  338. int mant, nbits, code, i, j;
  339. int component, dc, run, last_index, val;
  340. MJpegContext *m = s->mjpeg_ctx;
  341. uint8_t *huff_size_ac;
  342. uint16_t *huff_code_ac;
  343. /* DC coef */
  344. component = (n <= 3 ? 0 : (n&1) + 1);
  345. dc = block[0]; /* overflow is impossible */
  346. val = dc - s->last_dc[component];
  347. if (n < 4) {
  348. ff_mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);
  349. huff_size_ac = m->huff_size_ac_luminance;
  350. huff_code_ac = m->huff_code_ac_luminance;
  351. } else {
  352. ff_mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  353. huff_size_ac = m->huff_size_ac_chrominance;
  354. huff_code_ac = m->huff_code_ac_chrominance;
  355. }
  356. s->last_dc[component] = dc;
  357. /* AC coefs */
  358. run = 0;
  359. last_index = s->block_last_index[n];
  360. for(i=1;i<=last_index;i++) {
  361. j = s->intra_scantable.permutated[i];
  362. val = block[j];
  363. if (val == 0) {
  364. run++;
  365. } else {
  366. while (run >= 16) {
  367. put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);
  368. run -= 16;
  369. }
  370. mant = val;
  371. if (val < 0) {
  372. val = -val;
  373. mant--;
  374. }
  375. nbits= av_log2(val) + 1;
  376. code = (run << 4) | nbits;
  377. put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);
  378. put_sbits(&s->pb, nbits, mant);
  379. run = 0;
  380. }
  381. }
  382. /* output EOB only if not already 64 values */
  383. if (last_index < 63 || run != 0)
  384. put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);
  385. }
  386. void ff_mjpeg_encode_mb(MpegEncContext *s, DCTELEM block[6][64])
  387. {
  388. int i;
  389. for(i=0;i<5;i++) {
  390. encode_block(s, block[i], i);
  391. }
  392. if (s->chroma_format == CHROMA_420) {
  393. encode_block(s, block[5], 5);
  394. } else {
  395. encode_block(s, block[6], 6);
  396. encode_block(s, block[5], 5);
  397. encode_block(s, block[7], 7);
  398. }
  399. s->i_tex_bits += get_bits_diff(s);
  400. }
  401. // maximum over s->mjpeg_vsample[i]
  402. #define V_MAX 2
  403. static int amv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
  404. const AVFrame *pic_arg, int *got_packet)
  405. {
  406. MpegEncContext *s = avctx->priv_data;
  407. AVFrame pic = *pic_arg;
  408. int i;
  409. //CODEC_FLAG_EMU_EDGE have to be cleared
  410. if(s->avctx->flags & CODEC_FLAG_EMU_EDGE)
  411. return -1;
  412. //picture should be flipped upside-down
  413. for(i=0; i < 3; i++) {
  414. pic.data[i] += (pic.linesize[i] * (s->mjpeg_vsample[i] * (8 * s->mb_height -((s->height/V_MAX)&7)) - 1 ));
  415. pic.linesize[i] *= -1;
  416. }
  417. return ff_MPV_encode_picture(avctx, pkt, &pic, got_packet);
  418. }
  419. AVCodec ff_mjpeg_encoder = {
  420. .name = "mjpeg",
  421. .type = AVMEDIA_TYPE_VIDEO,
  422. .id = CODEC_ID_MJPEG,
  423. .priv_data_size = sizeof(MpegEncContext),
  424. .init = ff_MPV_encode_init,
  425. .encode2 = ff_MPV_encode_picture,
  426. .close = ff_MPV_encode_end,
  427. .capabilities = CODEC_CAP_SLICE_THREADS,
  428. .pix_fmts = (const enum PixelFormat[]){
  429. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE
  430. },
  431. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
  432. };
  433. AVCodec ff_amv_encoder = {
  434. .name = "amv",
  435. .type = AVMEDIA_TYPE_VIDEO,
  436. .id = CODEC_ID_AMV,
  437. .priv_data_size = sizeof(MpegEncContext),
  438. .init = ff_MPV_encode_init,
  439. .encode2 = amv_encode_picture,
  440. .close = ff_MPV_encode_end,
  441. .pix_fmts = (const enum PixelFormat[]){
  442. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_NONE
  443. },
  444. .long_name = NULL_IF_CONFIG_SMALL("AMV Video"),
  445. };