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.

109 lines
2.8KB

  1. /*
  2. * Multipart JPEG format
  3. * Copyright (c) 2000, 2001, 2002, 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. /* Multipart JPEG */
  21. #define BOUNDARY_TAG "ffserver"
  22. static int mpjpeg_write_header(AVFormatContext *s)
  23. {
  24. uint8_t buf1[256];
  25. snprintf(buf1, sizeof(buf1), "--%s\n", BOUNDARY_TAG);
  26. put_buffer(&s->pb, buf1, strlen(buf1));
  27. put_flush_packet(&s->pb);
  28. return 0;
  29. }
  30. static int mpjpeg_write_packet(AVFormatContext *s, int stream_index,
  31. const uint8_t *buf, int size, int64_t pts)
  32. {
  33. uint8_t buf1[256];
  34. snprintf(buf1, sizeof(buf1), "Content-type: image/jpeg\n\n");
  35. put_buffer(&s->pb, buf1, strlen(buf1));
  36. put_buffer(&s->pb, buf, size);
  37. snprintf(buf1, sizeof(buf1), "\n--%s\n", BOUNDARY_TAG);
  38. put_buffer(&s->pb, buf1, strlen(buf1));
  39. put_flush_packet(&s->pb);
  40. return 0;
  41. }
  42. static int mpjpeg_write_trailer(AVFormatContext *s)
  43. {
  44. return 0;
  45. }
  46. static AVOutputFormat mpjpeg_format = {
  47. "mpjpeg",
  48. "Mime multipart JPEG format",
  49. "multipart/x-mixed-replace;boundary=" BOUNDARY_TAG,
  50. "mjpg",
  51. 0,
  52. CODEC_ID_NONE,
  53. CODEC_ID_MJPEG,
  54. mpjpeg_write_header,
  55. mpjpeg_write_packet,
  56. mpjpeg_write_trailer,
  57. };
  58. /*************************************/
  59. /* single frame JPEG */
  60. static int single_jpeg_write_header(AVFormatContext *s)
  61. {
  62. return 0;
  63. }
  64. static int single_jpeg_write_packet(AVFormatContext *s, int stream_index,
  65. const uint8_t *buf, int size, int64_t pts)
  66. {
  67. put_buffer(&s->pb, buf, size);
  68. put_flush_packet(&s->pb);
  69. return 1; /* no more data can be sent */
  70. }
  71. static int single_jpeg_write_trailer(AVFormatContext *s)
  72. {
  73. return 0;
  74. }
  75. static AVOutputFormat single_jpeg_format = {
  76. "singlejpeg",
  77. "single JPEG image",
  78. "image/jpeg",
  79. NULL, /* note: no extension to favorize jpeg multiple images match */
  80. 0,
  81. CODEC_ID_NONE,
  82. CODEC_ID_MJPEG,
  83. single_jpeg_write_header,
  84. single_jpeg_write_packet,
  85. single_jpeg_write_trailer,
  86. };
  87. int jpeg_init(void)
  88. {
  89. av_register_output_format(&mpjpeg_format);
  90. av_register_output_format(&single_jpeg_format);
  91. return 0;
  92. }