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.

241 lines
6.3KB

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