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.

495 lines
14KB

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