ljpegenc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 libavcodec/ljpegenc.c
  29. * lossless JPEG encoder.
  30. */
  31. #include "avcodec.h"
  32. #include "dsputil.h"
  33. #include "mpegvideo.h"
  34. #include "mjpeg.h"
  35. #include "mjpegenc.h"
  36. static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  37. MpegEncContext * const s = avctx->priv_data;
  38. MJpegContext * const m = s->mjpeg_ctx;
  39. AVFrame *pict = data;
  40. const int width= s->width;
  41. const int height= s->height;
  42. AVFrame * const p= (AVFrame*)&s->current_picture;
  43. const int predictor= avctx->prediction_method+1;
  44. init_put_bits(&s->pb, buf, buf_size);
  45. *p = *pict;
  46. p->pict_type= FF_I_TYPE;
  47. p->key_frame= 1;
  48. ff_mjpeg_encode_picture_header(s);
  49. s->header_bits= put_bits_count(&s->pb);
  50. if(avctx->pix_fmt == PIX_FMT_RGB32){
  51. int x, y, i;
  52. const int linesize= p->linesize[0];
  53. uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
  54. int left[3], top[3], topleft[3];
  55. for(i=0; i<3; i++){
  56. buffer[0][i]= 1 << (9 - 1);
  57. }
  58. for(y = 0; y < height; y++) {
  59. const int modified_predictor= y ? predictor : 1;
  60. uint8_t *ptr = p->data[0] + (linesize * y);
  61. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
  62. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  63. return -1;
  64. }
  65. for(i=0; i<3; i++){
  66. top[i]= left[i]= topleft[i]= buffer[0][i];
  67. }
  68. for(x = 0; x < width; x++) {
  69. buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
  70. buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
  71. buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
  72. for(i=0;i<3;i++) {
  73. int pred, diff;
  74. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  75. topleft[i]= top[i];
  76. top[i]= buffer[x+1][i];
  77. left[i]= buffer[x][i];
  78. diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
  79. if(i==0)
  80. ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  81. else
  82. ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  83. }
  84. }
  85. }
  86. }else{
  87. int mb_x, mb_y, i;
  88. const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
  89. const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
  90. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  91. 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]){
  92. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  93. return -1;
  94. }
  95. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  96. if(mb_x==0 || mb_y==0){
  97. for(i=0;i<3;i++) {
  98. uint8_t *ptr;
  99. int x, y, h, v, linesize;
  100. h = s->mjpeg_hsample[i];
  101. v = s->mjpeg_vsample[i];
  102. linesize= p->linesize[i];
  103. for(y=0; y<v; y++){
  104. for(x=0; x<h; x++){
  105. int pred;
  106. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  107. if(y==0 && mb_y==0){
  108. if(x==0 && mb_x==0){
  109. pred= 128;
  110. }else{
  111. pred= ptr[-1];
  112. }
  113. }else{
  114. if(x==0 && mb_x==0){
  115. pred= ptr[-linesize];
  116. }else{
  117. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  118. }
  119. }
  120. if(i==0)
  121. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  122. else
  123. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  124. }
  125. }
  126. }
  127. }else{
  128. for(i=0;i<3;i++) {
  129. uint8_t *ptr;
  130. int x, y, h, v, linesize;
  131. h = s->mjpeg_hsample[i];
  132. v = s->mjpeg_vsample[i];
  133. linesize= p->linesize[i];
  134. for(y=0; y<v; y++){
  135. for(x=0; x<h; x++){
  136. int pred;
  137. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  138. //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
  139. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  140. if(i==0)
  141. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  142. else
  143. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. emms_c();
  152. ff_mjpeg_encode_picture_trailer(s);
  153. s->picture_number++;
  154. flush_put_bits(&s->pb);
  155. return pbBufPtr(&s->pb) - s->pb.buf;
  156. // return (put_bits_count(&f->pb)+7)/8;
  157. }
  158. AVCodec ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
  159. "ljpeg",
  160. CODEC_TYPE_VIDEO,
  161. CODEC_ID_LJPEG,
  162. sizeof(MpegEncContext),
  163. MPV_encode_init,
  164. encode_picture_lossless,
  165. MPV_encode_end,
  166. .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
  167. };