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.

207 lines
7.8KB

  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 "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= AV_PICTURE_TYPE_I;
  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_BGR0
  51. || avctx->pix_fmt == PIX_FMT_BGRA
  52. || avctx->pix_fmt == PIX_FMT_BGR24){
  53. int x, y, i;
  54. const int linesize= p->linesize[0];
  55. uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
  56. int left[3], top[3], topleft[3];
  57. for(i=0; i<3; i++){
  58. buffer[0][i]= 1 << (9 - 1);
  59. }
  60. for(y = 0; y < height; y++) {
  61. const int modified_predictor= y ? predictor : 1;
  62. uint8_t *ptr = p->data[0] + (linesize * y);
  63. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
  64. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  65. return -1;
  66. }
  67. for(i=0; i<3; i++){
  68. top[i]= left[i]= topleft[i]= buffer[0][i];
  69. }
  70. for(x = 0; x < width; x++) {
  71. if(avctx->pix_fmt == PIX_FMT_BGR24){
  72. buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
  73. buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
  74. buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
  75. }else{
  76. buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
  77. buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
  78. buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
  79. }
  80. for(i=0;i<3;i++) {
  81. int pred, diff;
  82. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  83. topleft[i]= top[i];
  84. top[i]= buffer[x+1][i];
  85. left[i]= buffer[x][i];
  86. diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
  87. if(i==0)
  88. ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  89. else
  90. ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  91. }
  92. }
  93. }
  94. }else{
  95. int mb_x, mb_y, i;
  96. const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
  97. const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
  98. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  99. 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]){
  100. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  101. return -1;
  102. }
  103. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  104. if(mb_x==0 || mb_y==0){
  105. for(i=0;i<3;i++) {
  106. uint8_t *ptr;
  107. int x, y, h, v, linesize;
  108. h = s->mjpeg_hsample[i];
  109. v = s->mjpeg_vsample[i];
  110. linesize= p->linesize[i];
  111. for(y=0; y<v; y++){
  112. for(x=0; x<h; x++){
  113. int pred;
  114. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  115. if(y==0 && mb_y==0){
  116. if(x==0 && mb_x==0){
  117. pred= 128;
  118. }else{
  119. pred= ptr[-1];
  120. }
  121. }else{
  122. if(x==0 && mb_x==0){
  123. pred= ptr[-linesize];
  124. }else{
  125. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  126. }
  127. }
  128. if(i==0)
  129. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  130. else
  131. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  132. }
  133. }
  134. }
  135. }else{
  136. for(i=0;i<3;i++) {
  137. uint8_t *ptr;
  138. int x, y, h, v, linesize;
  139. h = s->mjpeg_hsample[i];
  140. v = s->mjpeg_vsample[i];
  141. linesize= p->linesize[i];
  142. for(y=0; y<v; y++){
  143. for(x=0; x<h; x++){
  144. int pred;
  145. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  146. //printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
  147. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  148. if(i==0)
  149. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  150. else
  151. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. emms_c();
  160. ff_mjpeg_encode_picture_trailer(s);
  161. s->picture_number++;
  162. flush_put_bits(&s->pb);
  163. return put_bits_ptr(&s->pb) - s->pb.buf;
  164. // return (put_bits_count(&f->pb)+7)/8;
  165. }
  166. AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
  167. .name = "ljpeg",
  168. .type = AVMEDIA_TYPE_VIDEO,
  169. .id = CODEC_ID_LJPEG,
  170. .priv_data_size = sizeof(MpegEncContext),
  171. .init = ff_MPV_encode_init,
  172. .encode = encode_picture_lossless,
  173. .close = ff_MPV_encode_end,
  174. .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
  175. };