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.

545 lines
16KB

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