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.

239 lines
6.2KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. switch(c->pix_fmt) {
  42. case PIX_FMT_YUV420P:
  43. info->pix_fmt = PIX_FMT_YUVJ420P;
  44. break;
  45. case PIX_FMT_YUV422P:
  46. info->pix_fmt = PIX_FMT_YUVJ422P;
  47. break;
  48. case PIX_FMT_YUV444P:
  49. info->pix_fmt = PIX_FMT_YUVJ444P;
  50. break;
  51. default:
  52. return -1;
  53. }
  54. ret = jctx->alloc_cb(jctx->opaque, info);
  55. if (ret) {
  56. jctx->ret_code = ret;
  57. return -1;
  58. } else {
  59. for(i=0;i<3;i++) {
  60. picture->data[i] = info->pict.data[i];
  61. picture->linesize[i] = info->pict.linesize[i];
  62. }
  63. return 0;
  64. }
  65. }
  66. static void jpeg_img_copy(uint8_t *dst, int dst_wrap,
  67. uint8_t *src, int src_wrap,
  68. int width, int height)
  69. {
  70. for(;height > 0; height--) {
  71. memcpy(dst, src, width);
  72. dst += dst_wrap;
  73. src += src_wrap;
  74. }
  75. }
  76. /* XXX: libavcodec is broken for truncated jpegs! */
  77. #define IO_BUF_SIZE (1024*1024)
  78. static int jpeg_read(ByteIOContext *f,
  79. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
  80. {
  81. AVCodecContext *c;
  82. AVFrame *picture, picture1;
  83. int len, size, got_picture, i;
  84. uint8_t *inbuf_ptr, inbuf[IO_BUF_SIZE];
  85. JpegOpaque jctx;
  86. jctx.alloc_cb = alloc_cb;
  87. jctx.opaque = opaque;
  88. jctx.ret_code = -1; /* default return code is error */
  89. c = avcodec_alloc_context();
  90. if (!c)
  91. return -1;
  92. picture= avcodec_alloc_frame();
  93. if (!picture) {
  94. av_free(c);
  95. return -1;
  96. }
  97. c->opaque = &jctx;
  98. c->get_buffer = jpeg_get_buffer;
  99. c->flags |= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */
  100. if (avcodec_open(c, &mjpeg_decoder) < 0)
  101. goto fail1;
  102. for(;;) {
  103. size = get_buffer(f, inbuf, sizeof(inbuf));
  104. if (size == 0)
  105. break;
  106. inbuf_ptr = inbuf;
  107. while (size > 0) {
  108. len = avcodec_decode_video(c, &picture1, &got_picture,
  109. inbuf_ptr, size);
  110. if (len < 0)
  111. goto fail;
  112. if (got_picture)
  113. goto the_end;
  114. size -= len;
  115. inbuf_ptr += len;
  116. }
  117. }
  118. the_end:
  119. /* XXX: currently, the mjpeg decoder does not use AVFrame, so we
  120. must do it by hand */
  121. if (jpeg_get_buffer(c, picture) < 0)
  122. goto fail;
  123. for(i=0;i<3;i++) {
  124. int w, h;
  125. w = c->width;
  126. h = c->height;
  127. if (i >= 1) {
  128. switch(c->pix_fmt) {
  129. default:
  130. case PIX_FMT_YUV420P:
  131. w = (w + 1) >> 1;
  132. h = (h + 1) >> 1;
  133. break;
  134. case PIX_FMT_YUV422P:
  135. w = (w + 1) >> 1;
  136. break;
  137. case PIX_FMT_YUV444P:
  138. break;
  139. }
  140. }
  141. jpeg_img_copy(picture->data[i], picture->linesize[i],
  142. picture1.data[i], picture1.linesize[i],
  143. w, h);
  144. }
  145. jctx.ret_code = 0;
  146. fail:
  147. avcodec_close(c);
  148. fail1:
  149. av_free(picture);
  150. av_free(c);
  151. return jctx.ret_code;
  152. }
  153. #ifdef CONFIG_MUXERS
  154. static int jpeg_write(ByteIOContext *pb, AVImageInfo *info)
  155. {
  156. AVCodecContext *c;
  157. uint8_t *outbuf = NULL;
  158. int outbuf_size, ret, size, i;
  159. AVFrame *picture;
  160. ret = -1;
  161. c = avcodec_alloc_context();
  162. if (!c)
  163. return -1;
  164. picture = avcodec_alloc_frame();
  165. if (!picture)
  166. goto fail2;
  167. c->width = info->width;
  168. c->height = info->height;
  169. /* XXX: currently move that to the codec ? */
  170. switch(info->pix_fmt) {
  171. case PIX_FMT_YUVJ420P:
  172. c->pix_fmt = PIX_FMT_YUV420P;
  173. break;
  174. case PIX_FMT_YUVJ422P:
  175. c->pix_fmt = PIX_FMT_YUV422P;
  176. break;
  177. case PIX_FMT_YUVJ444P:
  178. c->pix_fmt = PIX_FMT_YUV444P;
  179. break;
  180. default:
  181. goto fail1;
  182. }
  183. for(i=0;i<3;i++) {
  184. picture->data[i] = info->pict.data[i];
  185. picture->linesize[i] = info->pict.linesize[i];
  186. }
  187. /* set the quality */
  188. picture->quality = 3; /* XXX: a parameter should be used */
  189. c->flags |= CODEC_FLAG_QSCALE;
  190. if (avcodec_open(c, &mjpeg_encoder) < 0)
  191. goto fail1;
  192. /* XXX: needs to sort out that size problem */
  193. outbuf_size = 1000000;
  194. outbuf = av_malloc(outbuf_size);
  195. size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
  196. if (size < 0)
  197. goto fail;
  198. put_buffer(pb, outbuf, size);
  199. put_flush_packet(pb);
  200. ret = 0;
  201. fail:
  202. avcodec_close(c);
  203. av_free(outbuf);
  204. fail1:
  205. av_free(picture);
  206. fail2:
  207. av_free(c);
  208. return ret;
  209. }
  210. #endif //CONFIG_MUXERS
  211. AVImageFormat jpeg_image_format = {
  212. "jpeg",
  213. "jpg,jpeg",
  214. jpeg_probe,
  215. jpeg_read,
  216. (1 << PIX_FMT_YUVJ420P) | (1 << PIX_FMT_YUVJ422P) | (1 << PIX_FMT_YUVJ444P),
  217. #ifdef CONFIG_MUXERS
  218. jpeg_write,
  219. #else
  220. NULL,
  221. #endif //CONFIG_MUXERS
  222. };