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.

169 lines
5.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/avstring.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avformat.h"
  28. #include "avio_internal.h"
  29. #include "internal.h"
  30. #include "libavutil/opt.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 updatefirst;
  38. } VideoMuxData;
  39. static int write_header(AVFormatContext *s)
  40. {
  41. VideoMuxData *img = s->priv_data;
  42. AVStream *st = s->streams[0];
  43. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(st->codec->pix_fmt);
  44. const char *str;
  45. av_strlcpy(img->path, s->filename, sizeof(img->path));
  46. /* find format */
  47. if (s->oformat->flags & AVFMT_NOFILE)
  48. img->is_pipe = 0;
  49. else
  50. img->is_pipe = 1;
  51. str = strrchr(img->path, '.');
  52. img->split_planes = str
  53. && !av_strcasecmp(str + 1, "y")
  54. && s->nb_streams == 1
  55. && st->codec->codec_id == AV_CODEC_ID_RAWVIDEO
  56. && desc
  57. &&(desc->flags & PIX_FMT_PLANAR)
  58. && desc->nb_components >= 3;
  59. return 0;
  60. }
  61. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  62. {
  63. VideoMuxData *img = s->priv_data;
  64. AVIOContext *pb[4];
  65. char filename[1024];
  66. AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
  67. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(codec->pix_fmt);
  68. int i;
  69. if (!img->is_pipe) {
  70. if (av_get_frame_filename(filename, sizeof(filename),
  71. img->path, img->img_number) < 0 && img->img_number > 1 && !img->updatefirst) {
  72. av_log(s, AV_LOG_ERROR,
  73. "Could not get frame filename number %d from pattern '%s'\n",
  74. img->img_number, img->path);
  75. return AVERROR(EINVAL);
  76. }
  77. for (i = 0; i < 4; i++) {
  78. if (avio_open2(&pb[i], filename, AVIO_FLAG_WRITE,
  79. &s->interrupt_callback, NULL) < 0) {
  80. av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", filename);
  81. return AVERROR(EIO);
  82. }
  83. if (!img->split_planes || i+1 >= desc->nb_components)
  84. break;
  85. filename[strlen(filename) - 1] = ((int[]){'U','V','A','x'})[i];
  86. }
  87. } else {
  88. pb[0] = s->pb;
  89. }
  90. if (img->split_planes) {
  91. int ysize = codec->width * codec->height;
  92. int usize = ((-codec->width)>>desc->log2_chroma_w) * ((-codec->height)>>desc->log2_chroma_h);
  93. if (desc->comp[0].depth_minus1 >= 8) {
  94. ysize *= 2;
  95. usize *= 2;
  96. }
  97. avio_write(pb[0], pkt->data , ysize);
  98. avio_write(pb[1], pkt->data + ysize , usize);
  99. avio_write(pb[2], pkt->data + ysize + usize, usize);
  100. avio_close(pb[1]);
  101. avio_close(pb[2]);
  102. if (desc->nb_components > 3) {
  103. avio_write(pb[3], pkt->data + ysize + 2*usize, ysize);
  104. avio_close(pb[3]);
  105. }
  106. } else {
  107. avio_write(pb[0], pkt->data, pkt->size);
  108. }
  109. avio_flush(pb[0]);
  110. if (!img->is_pipe) {
  111. avio_close(pb[0]);
  112. }
  113. img->img_number++;
  114. return 0;
  115. }
  116. #define OFFSET(x) offsetof(VideoMuxData, x)
  117. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  118. static const AVOption muxoptions[] = {
  119. { "updatefirst", "", OFFSET(updatefirst), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  120. { "start_number", "first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, ENC },
  121. { NULL },
  122. };
  123. #if CONFIG_IMAGE2_MUXER
  124. static const AVClass img2mux_class = {
  125. .class_name = "image2 muxer",
  126. .item_name = av_default_item_name,
  127. .option = muxoptions,
  128. .version = LIBAVUTIL_VERSION_INT,
  129. };
  130. AVOutputFormat ff_image2_muxer = {
  131. .name = "image2",
  132. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  133. .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
  134. "ppm,sgi,tga,tif,tiff,jp2,j2c,xwd,sun,ras,rs,im1,im8,im24,"
  135. "sunras,xbm,xface",
  136. .priv_data_size = sizeof(VideoMuxData),
  137. .video_codec = AV_CODEC_ID_MJPEG,
  138. .write_header = write_header,
  139. .write_packet = write_packet,
  140. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
  141. .priv_class = &img2mux_class,
  142. };
  143. #endif
  144. #if CONFIG_IMAGE2PIPE_MUXER
  145. AVOutputFormat ff_image2pipe_muxer = {
  146. .name = "image2pipe",
  147. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  148. .priv_data_size = sizeof(VideoMuxData),
  149. .video_codec = AV_CODEC_ID_MJPEG,
  150. .write_header = write_header,
  151. .write_packet = write_packet,
  152. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
  153. };
  154. #endif