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.

271 lines
9.7KB

  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. #include "img2.h"
  33. typedef struct VideoMuxData {
  34. const AVClass *class; /**< Class for private options. */
  35. int img_number;
  36. int split_planes; /**< use independent file for each Y, U, V plane */
  37. char path[1024];
  38. char tmp[4][1024];
  39. char target[4][1024];
  40. int update;
  41. int use_strftime;
  42. int frame_pts;
  43. const char *muxer;
  44. int use_rename;
  45. } VideoMuxData;
  46. static int write_header(AVFormatContext *s)
  47. {
  48. VideoMuxData *img = s->priv_data;
  49. AVStream *st = s->streams[0];
  50. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(st->codecpar->format);
  51. av_strlcpy(img->path, s->url, sizeof(img->path));
  52. if (st->codecpar->codec_id == AV_CODEC_ID_GIF) {
  53. img->muxer = "gif";
  54. } else if (st->codecpar->codec_id == AV_CODEC_ID_FITS) {
  55. img->muxer = "fits";
  56. } else if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) {
  57. const char *str = strrchr(img->path, '.');
  58. img->split_planes = str
  59. && !av_strcasecmp(str + 1, "y")
  60. && s->nb_streams == 1
  61. && desc
  62. &&(desc->flags & AV_PIX_FMT_FLAG_PLANAR)
  63. && desc->nb_components >= 3;
  64. }
  65. return 0;
  66. }
  67. static int write_muxed_file(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
  68. {
  69. VideoMuxData *img = s->priv_data;
  70. AVCodecParameters *par = s->streams[0]->codecpar;
  71. AVStream *st;
  72. AVPacket pkt2 = {0};
  73. AVFormatContext *fmt = NULL;
  74. int ret;
  75. /* URL is not used directly as we are overriding the IO context later. */
  76. ret = avformat_alloc_output_context2(&fmt, NULL, img->muxer, s->url);
  77. if (ret < 0)
  78. return ret;
  79. st = avformat_new_stream(fmt, NULL);
  80. if (!st) {
  81. avformat_free_context(fmt);
  82. return AVERROR(ENOMEM);
  83. }
  84. st->id = pkt->stream_index;
  85. fmt->pb = pb;
  86. if ((ret = av_packet_ref(&pkt2, pkt)) < 0 ||
  87. (ret = avcodec_parameters_copy(st->codecpar, par)) < 0 ||
  88. (ret = avformat_write_header(fmt, NULL)) < 0 ||
  89. (ret = av_interleaved_write_frame(fmt, &pkt2)) < 0 ||
  90. (ret = av_write_trailer(fmt)) < 0) {
  91. av_packet_unref(&pkt2);
  92. avformat_free_context(fmt);
  93. return ret;
  94. }
  95. av_packet_unref(&pkt2);
  96. avformat_free_context(fmt);
  97. return 0;
  98. }
  99. static int write_packet_pipe(AVFormatContext *s, AVPacket *pkt)
  100. {
  101. VideoMuxData *img = s->priv_data;
  102. if (img->muxer) {
  103. int ret = write_muxed_file(s, s->pb, pkt);
  104. if (ret < 0)
  105. return ret;
  106. } else {
  107. avio_write(s->pb, pkt->data, pkt->size);
  108. avio_flush(s->pb);
  109. }
  110. img->img_number++;
  111. return 0;
  112. }
  113. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  114. {
  115. VideoMuxData *img = s->priv_data;
  116. AVIOContext *pb[4];
  117. char filename[1024];
  118. AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
  119. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format);
  120. int ret, i;
  121. int nb_renames = 0;
  122. {
  123. if (img->update) {
  124. av_strlcpy(filename, img->path, sizeof(filename));
  125. } else if (img->use_strftime) {
  126. time_t now0;
  127. struct tm *tm, tmpbuf;
  128. time(&now0);
  129. tm = localtime_r(&now0, &tmpbuf);
  130. if (!strftime(filename, sizeof(filename), img->path, tm)) {
  131. av_log(s, AV_LOG_ERROR, "Could not get frame filename with strftime\n");
  132. return AVERROR(EINVAL);
  133. }
  134. } else if (img->frame_pts) {
  135. if (av_get_frame_filename2(filename, sizeof(filename), img->path, pkt->pts, AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0) {
  136. av_log(s, AV_LOG_ERROR, "Cannot write filename by pts of the frames.");
  137. return AVERROR(EINVAL);
  138. }
  139. } else if (av_get_frame_filename2(filename, sizeof(filename), img->path,
  140. img->img_number,
  141. AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0 &&
  142. img->img_number > 1) {
  143. av_log(s, AV_LOG_ERROR,
  144. "Could not get frame filename number %d from pattern '%s'. "
  145. "Use '-frames:v 1' for a single image, or '-update' option, or use a pattern such as %%03d within the filename.\n",
  146. img->img_number, img->path);
  147. return AVERROR(EINVAL);
  148. }
  149. for (i = 0; i < 4; i++) {
  150. snprintf(img->tmp[i], sizeof(img->tmp[i]), "%s.tmp", filename);
  151. av_strlcpy(img->target[i], filename, sizeof(img->target[i]));
  152. if (s->io_open(s, &pb[i], img->use_rename ? img->tmp[i] : filename, AVIO_FLAG_WRITE, NULL) < 0) {
  153. av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->use_rename ? img->tmp[i] : filename);
  154. return AVERROR(EIO);
  155. }
  156. if (!img->split_planes || i+1 >= desc->nb_components)
  157. break;
  158. filename[strlen(filename) - 1] = "UVAx"[i];
  159. }
  160. if (img->use_rename)
  161. nb_renames = i + 1;
  162. }
  163. if (img->split_planes) {
  164. int ysize = par->width * par->height;
  165. int usize = AV_CEIL_RSHIFT(par->width, desc->log2_chroma_w) * AV_CEIL_RSHIFT(par->height, desc->log2_chroma_h);
  166. if (desc->comp[0].depth >= 9) {
  167. ysize *= 2;
  168. usize *= 2;
  169. }
  170. avio_write(pb[0], pkt->data , ysize);
  171. avio_write(pb[1], pkt->data + ysize , usize);
  172. avio_write(pb[2], pkt->data + ysize + usize, usize);
  173. ff_format_io_close(s, &pb[1]);
  174. ff_format_io_close(s, &pb[2]);
  175. if (desc->nb_components > 3) {
  176. avio_write(pb[3], pkt->data + ysize + 2*usize, ysize);
  177. ff_format_io_close(s, &pb[3]);
  178. }
  179. } else if (img->muxer) {
  180. ret = write_muxed_file(s, pb[0], pkt);
  181. if (ret < 0)
  182. return ret;
  183. } else {
  184. avio_write(pb[0], pkt->data, pkt->size);
  185. }
  186. avio_flush(pb[0]);
  187. {
  188. ff_format_io_close(s, &pb[0]);
  189. for (i = 0; i < nb_renames; i++) {
  190. int ret = ff_rename(img->tmp[i], img->target[i], s);
  191. if (ret < 0)
  192. return ret;
  193. }
  194. }
  195. img->img_number++;
  196. return 0;
  197. }
  198. static int query_codec(enum AVCodecID id, int std_compliance)
  199. {
  200. int i;
  201. for (i = 0; ff_img_tags[i].id != AV_CODEC_ID_NONE; i++)
  202. if (ff_img_tags[i].id == id)
  203. return 1;
  204. // Anything really can be stored in img2
  205. return std_compliance < FF_COMPLIANCE_NORMAL;
  206. }
  207. #define OFFSET(x) offsetof(VideoMuxData, x)
  208. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  209. static const AVOption muxoptions[] = {
  210. { "update", "continuously overwrite one file", OFFSET(update), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
  211. { "start_number", "set first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, ENC },
  212. { "strftime", "use strftime for filename", OFFSET(use_strftime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
  213. { "frame_pts", "use current frame pts for filename", OFFSET(frame_pts), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
  214. { "atomic_writing", "write files atomically (using temporary files and renames)", OFFSET(use_rename), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
  215. { NULL },
  216. };
  217. #if CONFIG_IMAGE2_MUXER
  218. static const AVClass img2mux_class = {
  219. .class_name = "image2 muxer",
  220. .item_name = av_default_item_name,
  221. .option = muxoptions,
  222. .version = LIBAVUTIL_VERSION_INT,
  223. };
  224. AVOutputFormat ff_image2_muxer = {
  225. .name = "image2",
  226. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  227. .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
  228. "ppm,sgi,tga,tif,tiff,jp2,j2c,j2k,xwd,sun,ras,rs,im1,im8,im24,"
  229. "sunras,xbm,xface,pix,y",
  230. .priv_data_size = sizeof(VideoMuxData),
  231. .video_codec = AV_CODEC_ID_MJPEG,
  232. .write_header = write_header,
  233. .write_packet = write_packet,
  234. .query_codec = query_codec,
  235. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
  236. .priv_class = &img2mux_class,
  237. };
  238. #endif
  239. #if CONFIG_IMAGE2PIPE_MUXER
  240. AVOutputFormat ff_image2pipe_muxer = {
  241. .name = "image2pipe",
  242. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  243. .priv_data_size = sizeof(VideoMuxData),
  244. .video_codec = AV_CODEC_ID_MJPEG,
  245. .write_header = write_header,
  246. .write_packet = write_packet_pipe,
  247. .query_codec = query_codec,
  248. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
  249. };
  250. #endif