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.

229 lines
8.6KB

  1. /*
  2. * lossless JPEG 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. * lossless JPEG encoder.
  30. */
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "internal.h"
  34. #include "mpegvideo.h"
  35. #include "mjpeg.h"
  36. #include "mjpegenc.h"
  37. static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt,
  38. const AVFrame *pict, int *got_packet)
  39. {
  40. MpegEncContext * const s = avctx->priv_data;
  41. MJpegContext * const m = s->mjpeg_ctx;
  42. const int width= s->width;
  43. const int height= s->height;
  44. AVFrame * const p = &s->current_picture.f;
  45. const int predictor= avctx->prediction_method+1;
  46. const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
  47. const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
  48. int ret, max_pkt_size = FF_MIN_BUFFER_SIZE;
  49. if (avctx->pix_fmt == PIX_FMT_BGRA)
  50. max_pkt_size += width * height * 3 * 4;
  51. else {
  52. max_pkt_size += mb_width * mb_height * 3 * 4
  53. * s->mjpeg_hsample[0] * s->mjpeg_vsample[0];
  54. }
  55. if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
  56. return ret;
  57. init_put_bits(&s->pb, pkt->data, pkt->size);
  58. *p = *pict;
  59. p->pict_type= AV_PICTURE_TYPE_I;
  60. p->key_frame= 1;
  61. ff_mjpeg_encode_picture_header(s);
  62. s->header_bits= put_bits_count(&s->pb);
  63. if(avctx->pix_fmt == PIX_FMT_BGR0
  64. || avctx->pix_fmt == PIX_FMT_BGRA
  65. || avctx->pix_fmt == PIX_FMT_BGR24){
  66. int x, y, i;
  67. const int linesize= p->linesize[0];
  68. uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
  69. int left[3], top[3], topleft[3];
  70. for(i=0; i<3; i++){
  71. buffer[0][i]= 1 << (9 - 1);
  72. }
  73. for(y = 0; y < height; y++) {
  74. const int modified_predictor= y ? predictor : 1;
  75. uint8_t *ptr = p->data[0] + (linesize * y);
  76. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
  77. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  78. return -1;
  79. }
  80. for(i=0; i<3; i++){
  81. top[i]= left[i]= topleft[i]= buffer[0][i];
  82. }
  83. for(x = 0; x < width; x++) {
  84. if(avctx->pix_fmt == PIX_FMT_BGR24){
  85. buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
  86. buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
  87. buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
  88. }else{
  89. buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
  90. buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
  91. buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
  92. }
  93. for(i=0;i<3;i++) {
  94. int pred, diff;
  95. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  96. topleft[i]= top[i];
  97. top[i]= buffer[x+1][i];
  98. left[i]= buffer[x][i];
  99. diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
  100. if(i==0)
  101. ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  102. else
  103. ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  104. }
  105. }
  106. }
  107. }else{
  108. int mb_x, mb_y, i;
  109. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  110. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
  111. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  112. return -1;
  113. }
  114. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  115. if(mb_x==0 || mb_y==0){
  116. for(i=0;i<3;i++) {
  117. uint8_t *ptr;
  118. int x, y, h, v, linesize;
  119. h = s->mjpeg_hsample[i];
  120. v = s->mjpeg_vsample[i];
  121. linesize= p->linesize[i];
  122. for(y=0; y<v; y++){
  123. for(x=0; x<h; x++){
  124. int pred;
  125. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  126. if(y==0 && mb_y==0){
  127. if(x==0 && mb_x==0){
  128. pred= 128;
  129. }else{
  130. pred= ptr[-1];
  131. }
  132. }else{
  133. if(x==0 && mb_x==0){
  134. pred= ptr[-linesize];
  135. }else{
  136. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  137. }
  138. }
  139. if(i==0)
  140. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  141. else
  142. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  143. }
  144. }
  145. }
  146. }else{
  147. for(i=0;i<3;i++) {
  148. uint8_t *ptr;
  149. int x, y, h, v, linesize;
  150. h = s->mjpeg_hsample[i];
  151. v = s->mjpeg_vsample[i];
  152. linesize= p->linesize[i];
  153. for(y=0; y<v; y++){
  154. for(x=0; x<h; x++){
  155. int pred;
  156. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  157. //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
  158. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  159. if(i==0)
  160. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  161. else
  162. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. emms_c();
  171. av_assert0(s->esc_pos == s->header_bits >> 3);
  172. ff_mjpeg_encode_stuffing(s);
  173. ff_mjpeg_encode_picture_trailer(s);
  174. s->picture_number++;
  175. flush_put_bits(&s->pb);
  176. pkt->size = put_bits_ptr(&s->pb) - s->pb.buf;
  177. pkt->flags |= AV_PKT_FLAG_KEY;
  178. *got_packet = 1;
  179. return 0;
  180. // return (put_bits_count(&f->pb)+7)/8;
  181. }
  182. AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
  183. .name = "ljpeg",
  184. .type = AVMEDIA_TYPE_VIDEO,
  185. .id = AV_CODEC_ID_LJPEG,
  186. .priv_data_size = sizeof(MpegEncContext),
  187. .init = ff_MPV_encode_init,
  188. .encode2 = encode_picture_lossless,
  189. .close = ff_MPV_encode_end,
  190. .pix_fmts = (const enum PixelFormat[]){
  191. PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_BGR0,
  192. PIX_FMT_YUVJ420P, PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
  193. PIX_FMT_YUV420P, PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  194. PIX_FMT_NONE},
  195. .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
  196. };