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.

192 lines
6.5KB

  1. /*
  2. * Image format
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. * Copyright (c) 2004 Michael Niedermayer
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/log.h"
  25. #include "avformat.h"
  26. #include "avio_internal.h"
  27. #include "internal.h"
  28. #include "libavutil/opt.h"
  29. typedef struct RenameIO {
  30. AVIOContext *pb;
  31. char filename[1024];
  32. char tmp[1024];
  33. } RenameIO;
  34. typedef struct VideoMuxData {
  35. const AVClass *class; /**< Class for private options. */
  36. int img_number;
  37. int is_pipe;
  38. char path[1024];
  39. int update;
  40. } VideoMuxData;
  41. static int write_header(AVFormatContext *s)
  42. {
  43. VideoMuxData *img = s->priv_data;
  44. av_strlcpy(img->path, s->filename, sizeof(img->path));
  45. /* find format */
  46. if (s->oformat->flags & AVFMT_NOFILE)
  47. img->is_pipe = 0;
  48. else
  49. img->is_pipe = 1;
  50. return 0;
  51. }
  52. static int open_temporary(AVFormatContext *s, RenameIO *out, const char *filename)
  53. {
  54. snprintf(out->tmp, sizeof(out->tmp), "%s.tmp", filename);
  55. av_strlcpy(out->filename, filename, sizeof(out->filename));
  56. if (s->io_open(s, &out->pb, out->tmp, AVIO_FLAG_WRITE, NULL) < 0) {
  57. av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", out->tmp);
  58. return AVERROR(EIO);
  59. }
  60. return 0;
  61. }
  62. static void close_and_rename(AVFormatContext *s, RenameIO *out)
  63. {
  64. ff_format_io_close(s, &out->pb);
  65. ff_rename(out->tmp, out->filename);
  66. }
  67. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  68. {
  69. VideoMuxData *img = s->priv_data;
  70. RenameIO out[3];
  71. char filename[1024];
  72. AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
  73. int i;
  74. if (!img->is_pipe) {
  75. if (img->update) {
  76. av_strlcpy(filename, img->path, sizeof(filename));
  77. } else if (av_get_frame_filename(filename, sizeof(filename), img->path, img->img_number) < 0 &&
  78. img->img_number > 1) {
  79. av_log(s, AV_LOG_ERROR,
  80. "Could not get frame filename number %d from pattern '%s'\n",
  81. img->img_number, img->path);
  82. return AVERROR(EIO);
  83. }
  84. for (i = 0; i < 3; i++) {
  85. int ret = open_temporary(s, &out[i], filename);
  86. if (ret < 0)
  87. return ret;
  88. if (par->codec_id != AV_CODEC_ID_RAWVIDEO)
  89. break;
  90. filename[strlen(filename) - 1] = 'U' + i;
  91. }
  92. } else {
  93. out[0].pb = s->pb;
  94. }
  95. if (par->codec_id == AV_CODEC_ID_RAWVIDEO) {
  96. int ysize = par->width * par->height;
  97. avio_write(out[0].pb, pkt->data, ysize);
  98. avio_write(out[1].pb, pkt->data + ysize, (pkt->size - ysize) / 2);
  99. avio_write(out[2].pb, pkt->data + ysize + (pkt->size - ysize) / 2, (pkt->size - ysize) / 2);
  100. close_and_rename(s, &out[1]);
  101. close_and_rename(s, &out[2]);
  102. } else {
  103. if (ff_guess_image2_codec(s->filename) == AV_CODEC_ID_JPEG2000) {
  104. AVStream *st = s->streams[0];
  105. if (st->codecpar->extradata_size > 8 &&
  106. AV_RL32(st->codecpar->extradata + 4) == MKTAG('j', 'p', '2', 'h')) {
  107. if (pkt->size < 8 ||
  108. AV_RL32(pkt->data + 4) != MKTAG('j', 'p', '2', 'c'))
  109. goto error;
  110. avio_wb32(out[0].pb, 12);
  111. ffio_wfourcc(out[0].pb, "jP ");
  112. avio_wb32(out[0].pb, 0x0D0A870A); // signature
  113. avio_wb32(out[0].pb, 20);
  114. ffio_wfourcc(out[0].pb, "ftyp");
  115. ffio_wfourcc(out[0].pb, "jp2 ");
  116. avio_wb32(out[0].pb, 0);
  117. ffio_wfourcc(out[0].pb, "jp2 ");
  118. avio_write(out[0].pb, st->codecpar->extradata, st->codecpar->extradata_size);
  119. } else if (pkt->size < 8 ||
  120. (!st->codecpar->extradata_size &&
  121. AV_RL32(pkt->data + 4) != MKTAG('j', 'P', ' ', ' '))) { // signature
  122. error:
  123. av_log(s, AV_LOG_ERROR, "malformed JPEG 2000 codestream\n");
  124. return -1;
  125. }
  126. }
  127. avio_write(out[0].pb, pkt->data, pkt->size);
  128. }
  129. avio_flush(out[0].pb);
  130. if (!img->is_pipe) {
  131. close_and_rename(s, &out[0]);
  132. }
  133. img->img_number++;
  134. return 0;
  135. }
  136. #define OFFSET(x) offsetof(VideoMuxData, x)
  137. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  138. static const AVOption muxoptions[] = {
  139. { "start_number", "first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, ENC },
  140. { "update", "continuously overwrite one file", OFFSET(update), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  141. { NULL },
  142. };
  143. #if CONFIG_IMAGE2_MUXER
  144. static const AVClass img2mux_class = {
  145. .class_name = "image2 muxer",
  146. .item_name = av_default_item_name,
  147. .option = muxoptions,
  148. .version = LIBAVUTIL_VERSION_INT,
  149. };
  150. AVOutputFormat ff_image2_muxer = {
  151. .name = "image2",
  152. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  153. .extensions = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
  154. "ppm,sgi,tga,tif,tiff,jp2,xwd,sun,ras,rs,im1,im8,im24,"
  155. "sunras,webp,xbm,j2c,pix",
  156. .priv_data_size = sizeof(VideoMuxData),
  157. .video_codec = AV_CODEC_ID_MJPEG,
  158. .write_header = write_header,
  159. .write_packet = write_packet,
  160. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
  161. .priv_class = &img2mux_class,
  162. };
  163. #endif
  164. #if CONFIG_IMAGE2PIPE_MUXER
  165. AVOutputFormat ff_image2pipe_muxer = {
  166. .name = "image2pipe",
  167. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  168. .priv_data_size = sizeof(VideoMuxData),
  169. .video_codec = AV_CODEC_ID_MJPEG,
  170. .write_header = write_header,
  171. .write_packet = write_packet,
  172. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
  173. };
  174. #endif