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.

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