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.

235 lines
8.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 "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 == AV_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 (!s->edge_emu_buffer &&
  56. (ret = ff_mpv_frame_size_alloc(s, pict->linesize[0])) < 0) {
  57. av_log(avctx, AV_LOG_ERROR, "failed to allocate context scratch buffers.\n");
  58. return ret;
  59. }
  60. if ((ret = ff_alloc_packet2(avctx, pkt, max_pkt_size)) < 0)
  61. return ret;
  62. init_put_bits(&s->pb, pkt->data, pkt->size);
  63. *p = *pict;
  64. p->pict_type= AV_PICTURE_TYPE_I;
  65. p->key_frame= 1;
  66. ff_mjpeg_encode_picture_header(s);
  67. s->header_bits= put_bits_count(&s->pb);
  68. if(avctx->pix_fmt == AV_PIX_FMT_BGR0
  69. || avctx->pix_fmt == AV_PIX_FMT_BGRA
  70. || avctx->pix_fmt == AV_PIX_FMT_BGR24){
  71. int x, y, i;
  72. const int linesize= p->linesize[0];
  73. uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
  74. int left[3], top[3], topleft[3];
  75. for(i=0; i<3; i++){
  76. buffer[0][i]= 1 << (9 - 1);
  77. }
  78. for(y = 0; y < height; y++) {
  79. const int modified_predictor= y ? predictor : 1;
  80. uint8_t *ptr = p->data[0] + (linesize * y);
  81. if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < width*3*4){
  82. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  83. return -1;
  84. }
  85. for(i=0; i<3; i++){
  86. top[i]= left[i]= topleft[i]= buffer[0][i];
  87. }
  88. for(x = 0; x < width; x++) {
  89. if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
  90. buffer[x][1] = ptr[3*x+0] - ptr[3*x+1] + 0x100;
  91. buffer[x][2] = ptr[3*x+2] - ptr[3*x+1] + 0x100;
  92. buffer[x][0] = (ptr[3*x+0] + 2*ptr[3*x+1] + ptr[3*x+2])>>2;
  93. }else{
  94. buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;
  95. buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;
  96. buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;
  97. }
  98. for(i=0;i<3;i++) {
  99. int pred, diff;
  100. PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
  101. topleft[i]= top[i];
  102. top[i]= buffer[x+1][i];
  103. left[i]= buffer[x][i];
  104. diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;
  105. if(i==0)
  106. ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  107. else
  108. ff_mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  109. }
  110. }
  111. }
  112. }else{
  113. int mb_x, mb_y, i;
  114. for(mb_y = 0; mb_y < mb_height; mb_y++) {
  115. 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]){
  116. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  117. return -1;
  118. }
  119. for(mb_x = 0; mb_x < mb_width; mb_x++) {
  120. if(mb_x==0 || mb_y==0){
  121. for(i=0;i<3;i++) {
  122. uint8_t *ptr;
  123. int x, y, h, v, linesize;
  124. h = s->mjpeg_hsample[i];
  125. v = s->mjpeg_vsample[i];
  126. linesize= p->linesize[i];
  127. for(y=0; y<v; y++){
  128. for(x=0; x<h; x++){
  129. int pred;
  130. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  131. if(y==0 && mb_y==0){
  132. if(x==0 && mb_x==0){
  133. pred= 128;
  134. }else{
  135. pred= ptr[-1];
  136. }
  137. }else{
  138. if(x==0 && mb_x==0){
  139. pred= ptr[-linesize];
  140. }else{
  141. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  142. }
  143. }
  144. if(i==0)
  145. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  146. else
  147. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  148. }
  149. }
  150. }
  151. }else{
  152. for(i=0;i<3;i++) {
  153. uint8_t *ptr;
  154. int x, y, h, v, linesize;
  155. h = s->mjpeg_hsample[i];
  156. v = s->mjpeg_vsample[i];
  157. linesize= p->linesize[i];
  158. for(y=0; y<v; y++){
  159. for(x=0; x<h; x++){
  160. int pred;
  161. ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
  162. PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
  163. if(i==0)
  164. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly
  165. else
  166. ff_mjpeg_encode_dc(s, *ptr - pred, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. emms_c();
  175. av_assert0(s->esc_pos == s->header_bits >> 3);
  176. ff_mjpeg_encode_stuffing(s);
  177. ff_mjpeg_encode_picture_trailer(s);
  178. s->picture_number++;
  179. flush_put_bits(&s->pb);
  180. pkt->size = put_bits_ptr(&s->pb) - s->pb.buf;
  181. pkt->flags |= AV_PKT_FLAG_KEY;
  182. *got_packet = 1;
  183. return 0;
  184. // return (put_bits_count(&f->pb)+7)/8;
  185. }
  186. AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
  187. .name = "ljpeg",
  188. .type = AVMEDIA_TYPE_VIDEO,
  189. .id = AV_CODEC_ID_LJPEG,
  190. .priv_data_size = sizeof(MpegEncContext),
  191. .init = ff_MPV_encode_init,
  192. .encode2 = encode_picture_lossless,
  193. .close = ff_MPV_encode_end,
  194. .pix_fmts = (const enum AVPixelFormat[]){
  195. AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_BGR0,
  196. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  197. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  198. AV_PIX_FMT_NONE},
  199. .long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
  200. };