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.

208 lines
5.5KB

  1. /*
  2. * JPEG image format
  3. * Copyright (c) 2003 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. static int jpeg_probe(AVProbeData *pd)
  21. {
  22. if (pd->buf_size >= 64 &&
  23. pd->buf[0] == 0xff && pd->buf[1] == 0xd8 && pd->buf[2] == 0xff)
  24. return AVPROBE_SCORE_MAX;
  25. else
  26. return 0;
  27. }
  28. typedef struct JpegOpaque {
  29. int (*alloc_cb)(void *opaque, AVImageInfo *info);
  30. void *opaque;
  31. int ret_code;
  32. } JpegOpaque;
  33. /* called by the codec to allocate the image */
  34. static int jpeg_get_buffer(AVCodecContext *c, AVFrame *picture)
  35. {
  36. JpegOpaque *jctx = c->opaque;
  37. AVImageInfo info1, *info = &info1;
  38. int ret, i;
  39. info->width = c->width;
  40. info->height = c->height;
  41. info->pix_fmt = c->pix_fmt;
  42. ret = jctx->alloc_cb(jctx->opaque, info);
  43. if (ret) {
  44. jctx->ret_code = ret;
  45. return -1;
  46. } else {
  47. for(i=0;i<3;i++) {
  48. picture->data[i] = info->pict.data[i];
  49. picture->linesize[i] = info->pict.linesize[i];
  50. }
  51. return 0;
  52. }
  53. }
  54. static void img_copy(uint8_t *dst, int dst_wrap,
  55. uint8_t *src, int src_wrap,
  56. int width, int height)
  57. {
  58. for(;height > 0; height--) {
  59. memcpy(dst, src, width);
  60. dst += dst_wrap;
  61. src += src_wrap;
  62. }
  63. }
  64. /* XXX: libavcodec is broken for truncated jpegs! */
  65. #define IO_BUF_SIZE (1024*1024)
  66. static int jpeg_read(ByteIOContext *f,
  67. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
  68. {
  69. AVCodecContext *c;
  70. AVFrame *picture, picture1;
  71. int len, size, got_picture, i;
  72. uint8_t *inbuf_ptr, inbuf[IO_BUF_SIZE];
  73. JpegOpaque jctx;
  74. jctx.alloc_cb = alloc_cb;
  75. jctx.opaque = opaque;
  76. jctx.ret_code = -1; /* default return code is error */
  77. c = avcodec_alloc_context();
  78. if (!c)
  79. return -1;
  80. picture= avcodec_alloc_frame();
  81. if (!picture) {
  82. av_free(c);
  83. return -1;
  84. }
  85. c->opaque = &jctx;
  86. c->get_buffer = jpeg_get_buffer;
  87. c->flags |= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */
  88. if (avcodec_open(c, &mjpeg_decoder) < 0)
  89. goto fail1;
  90. for(;;) {
  91. size = get_buffer(f, inbuf, sizeof(inbuf));
  92. if (size == 0)
  93. break;
  94. inbuf_ptr = inbuf;
  95. while (size > 0) {
  96. len = avcodec_decode_video(c, &picture1, &got_picture,
  97. inbuf_ptr, size);
  98. if (len < 0)
  99. goto fail;
  100. if (got_picture)
  101. goto the_end;
  102. size -= len;
  103. inbuf_ptr += len;
  104. }
  105. }
  106. the_end:
  107. /* XXX: currently, the mjpeg decoder does not use AVFrame, so we
  108. must do it by hand */
  109. if (jpeg_get_buffer(c, picture) < 0)
  110. goto fail;
  111. for(i=0;i<3;i++) {
  112. int w, h;
  113. w = c->width;
  114. h = c->height;
  115. if (i >= 1) {
  116. switch(c->pix_fmt) {
  117. default:
  118. case PIX_FMT_YUV420P:
  119. w = (w + 1) >> 1;
  120. h = (h + 1) >> 1;
  121. break;
  122. case PIX_FMT_YUV422P:
  123. w = (w + 1) >> 1;
  124. break;
  125. case PIX_FMT_YUV444P:
  126. break;
  127. }
  128. }
  129. img_copy(picture->data[i], picture->linesize[i],
  130. picture1.data[i], picture1.linesize[i],
  131. w, h);
  132. }
  133. jctx.ret_code = 0;
  134. fail:
  135. avcodec_close(c);
  136. fail1:
  137. av_free(picture);
  138. av_free(c);
  139. return jctx.ret_code;
  140. }
  141. static int jpeg_write(ByteIOContext *pb, AVImageInfo *info)
  142. {
  143. AVCodecContext *c;
  144. uint8_t *outbuf = NULL;
  145. int outbuf_size, ret, size, i;
  146. AVFrame *picture;
  147. ret = -1;
  148. c = avcodec_alloc_context();
  149. if (!c)
  150. return -1;
  151. picture = avcodec_alloc_frame();
  152. if (!picture)
  153. goto fail2;
  154. c->width = info->width;
  155. c->height = info->height;
  156. c->pix_fmt = info->pix_fmt;
  157. for(i=0;i<3;i++) {
  158. picture->data[i] = info->pict.data[i];
  159. picture->linesize[i] = info->pict.linesize[i];
  160. }
  161. /* set the quality */
  162. picture->quality = 3; /* XXX: a parameter should be used */
  163. c->flags |= CODEC_FLAG_QSCALE;
  164. if (avcodec_open(c, &mjpeg_encoder) < 0)
  165. goto fail1;
  166. /* XXX: needs to sort out that size problem */
  167. outbuf_size = 1000000;
  168. outbuf = av_malloc(outbuf_size);
  169. size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
  170. if (size < 0)
  171. goto fail;
  172. put_buffer(pb, outbuf, size);
  173. put_flush_packet(pb);
  174. ret = 0;
  175. fail:
  176. avcodec_close(c);
  177. av_free(outbuf);
  178. fail1:
  179. av_free(picture);
  180. fail2:
  181. av_free(c);
  182. return ret;
  183. }
  184. AVImageFormat jpeg_image_format = {
  185. "jpeg",
  186. "jpg,jpeg",
  187. jpeg_probe,
  188. jpeg_read,
  189. (1 << PIX_FMT_YUV420P) | (1 << PIX_FMT_YUV422P) | (1 << PIX_FMT_YUV444P),
  190. jpeg_write,
  191. };