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.

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