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.

218 lines
7.9KB

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