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.

217 lines
7.8KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/log.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avformat.h"
  29. #include "avio_internal.h"
  30. #include "internal.h"
  31. typedef struct {
  32. const AVClass *class; /**< Class for private options. */
  33. int img_number;
  34. int is_pipe;
  35. int split_planes; /**< use independent file for each Y, U, V plane */
  36. char path[1024];
  37. int update;
  38. int use_strftime;
  39. const char *muxer;
  40. } VideoMuxData;
  41. static int write_header(AVFormatContext *s)
  42. {
  43. VideoMuxData *img = s->priv_data;
  44. AVStream *st = s->streams[0];
  45. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(st->codec->pix_fmt);
  46. av_strlcpy(img->path, s->filename, sizeof(img->path));
  47. /* find format */
  48. if (s->oformat->flags & AVFMT_NOFILE)
  49. img->is_pipe = 0;
  50. else
  51. img->is_pipe = 1;
  52. if (st->codec->codec_id == AV_CODEC_ID_GIF) {
  53. img->muxer = "gif";
  54. } else if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
  55. const char *str = strrchr(img->path, '.');
  56. img->split_planes = str
  57. && !av_strcasecmp(str + 1, "y")
  58. && s->nb_streams == 1
  59. && desc
  60. &&(desc->flags & AV_PIX_FMT_FLAG_PLANAR)
  61. && desc->nb_components >= 3;
  62. }
  63. return 0;
  64. }
  65. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  66. {
  67. VideoMuxData *img = s->priv_data;
  68. AVIOContext *pb[4];
  69. char filename[1024];
  70. AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
  71. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(codec->pix_fmt);
  72. int i;
  73. if (!img->is_pipe) {
  74. if (img->update) {
  75. av_strlcpy(filename, img->path, sizeof(filename));
  76. } else if (img->use_strftime) {
  77. time_t now0;
  78. struct tm *tm;
  79. time(&now0);
  80. tm = localtime(&now0);
  81. if (!strftime(filename, sizeof(filename), img->path, tm)) {
  82. av_log(s, AV_LOG_ERROR, "Could not get frame filename with strftime\n");
  83. return AVERROR(EINVAL);
  84. }
  85. } else if (av_get_frame_filename(filename, sizeof(filename), img->path, img->img_number) < 0 &&
  86. img->img_number > 1) {
  87. av_log(s, AV_LOG_ERROR,
  88. "Could not get frame filename number %d from pattern '%s' (either set updatefirst or use a pattern like %%03d within the filename pattern)\n",
  89. img->img_number, img->path);
  90. return AVERROR(EINVAL);
  91. }
  92. for (i = 0; i < 4; i++) {
  93. if (avio_open2(&pb[i], filename, AVIO_FLAG_WRITE,
  94. &s->interrupt_callback, NULL) < 0) {
  95. av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", filename);
  96. return AVERROR(EIO);
  97. }
  98. if (!img->split_planes || i+1 >= desc->nb_components)
  99. break;
  100. filename[strlen(filename) - 1] = "UVAx"[i];
  101. }
  102. } else {
  103. pb[0] = s->pb;
  104. }
  105. if (img->split_planes) {
  106. int ysize = codec->width * codec->height;
  107. int usize = FF_CEIL_RSHIFT(codec->width, desc->log2_chroma_w) * FF_CEIL_RSHIFT(codec->height, desc->log2_chroma_h);
  108. if (desc->comp[0].depth_minus1 >= 8) {
  109. ysize *= 2;
  110. usize *= 2;
  111. }
  112. avio_write(pb[0], pkt->data , ysize);
  113. avio_write(pb[1], pkt->data + ysize , usize);
  114. avio_write(pb[2], pkt->data + ysize + usize, usize);
  115. avio_close(pb[1]);
  116. avio_close(pb[2]);
  117. if (desc->nb_components > 3) {
  118. avio_write(pb[3], pkt->data + ysize + 2*usize, ysize);
  119. avio_close(pb[3]);
  120. }
  121. } else if (img->muxer) {
  122. int ret;
  123. AVStream *st;
  124. AVPacket pkt2 = {0};
  125. AVFormatContext *fmt = NULL;
  126. av_assert0(!img->split_planes);
  127. ret = avformat_alloc_output_context2(&fmt, NULL, img->muxer, s->filename);
  128. if (ret < 0)
  129. return ret;
  130. st = avformat_new_stream(fmt, NULL);
  131. if (!st) {
  132. avformat_free_context(fmt);
  133. return AVERROR(ENOMEM);
  134. }
  135. st->id = pkt->stream_index;
  136. fmt->pb = pb[0];
  137. if ((ret = av_copy_packet(&pkt2, pkt)) < 0 ||
  138. (ret = av_dup_packet(&pkt2)) < 0 ||
  139. (ret = avcodec_copy_context(st->codec, s->streams[0]->codec)) < 0 ||
  140. (ret = avformat_write_header(fmt, NULL)) < 0 ||
  141. (ret = av_interleaved_write_frame(fmt, &pkt2)) < 0 ||
  142. (ret = av_write_trailer(fmt)) < 0) {
  143. av_free_packet(&pkt2);
  144. avformat_free_context(fmt);
  145. return ret;
  146. }
  147. av_free_packet(&pkt2);
  148. avformat_free_context(fmt);
  149. } else {
  150. avio_write(pb[0], pkt->data, pkt->size);
  151. }
  152. avio_flush(pb[0]);
  153. if (!img->is_pipe) {
  154. avio_close(pb[0]);
  155. }
  156. img->img_number++;
  157. return 0;
  158. }
  159. #define OFFSET(x) offsetof(VideoMuxData, x)
  160. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  161. static const AVOption muxoptions[] = {
  162. { "updatefirst", "continuously overwrite one file", OFFSET(update), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  163. { "update", "continuously overwrite one file", OFFSET(update), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  164. { "start_number", "set first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, ENC },
  165. { "strftime", "use strftime for filename", OFFSET(use_strftime), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  166. { NULL },
  167. };
  168. #if CONFIG_IMAGE2_MUXER
  169. static const AVClass img2mux_class = {
  170. .class_name = "image2 muxer",
  171. .item_name = av_default_item_name,
  172. .option = muxoptions,
  173. .version = LIBAVUTIL_VERSION_INT,
  174. };
  175. AVOutputFormat ff_image2_muxer = {
  176. .name = "image2",
  177. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  178. .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
  179. "ppm,sgi,tga,tif,tiff,jp2,j2c,j2k,xwd,sun,ras,rs,im1,im8,im24,"
  180. "sunras,webp,xbm,xface,pix,y",
  181. .priv_data_size = sizeof(VideoMuxData),
  182. .video_codec = AV_CODEC_ID_MJPEG,
  183. .write_header = write_header,
  184. .write_packet = write_packet,
  185. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
  186. .priv_class = &img2mux_class,
  187. };
  188. #endif
  189. #if CONFIG_IMAGE2PIPE_MUXER
  190. AVOutputFormat ff_image2pipe_muxer = {
  191. .name = "image2pipe",
  192. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  193. .priv_data_size = sizeof(VideoMuxData),
  194. .video_codec = AV_CODEC_ID_MJPEG,
  195. .write_header = write_header,
  196. .write_packet = write_packet,
  197. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
  198. };
  199. #endif