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.

237 lines
8.9KB

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