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.

218 lines
8.0KB

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