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.

171 lines
5.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 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 {
  30. const AVClass *class; /**< Class for private options. */
  31. int img_number;
  32. int is_pipe;
  33. char path[1024];
  34. int update;
  35. } VideoMuxData;
  36. static int write_header(AVFormatContext *s)
  37. {
  38. VideoMuxData *img = s->priv_data;
  39. av_strlcpy(img->path, s->filename, sizeof(img->path));
  40. /* find format */
  41. if (s->oformat->flags & AVFMT_NOFILE)
  42. img->is_pipe = 0;
  43. else
  44. img->is_pipe = 1;
  45. return 0;
  46. }
  47. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  48. {
  49. VideoMuxData *img = s->priv_data;
  50. AVIOContext *pb[3];
  51. char filename[1024];
  52. AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
  53. int i;
  54. if (!img->is_pipe) {
  55. if (img->update) {
  56. av_strlcpy(filename, img->path, sizeof(filename));
  57. } else if (av_get_frame_filename(filename, sizeof(filename), img->path, img->img_number) < 0 &&
  58. img->img_number > 1) {
  59. av_log(s, AV_LOG_ERROR,
  60. "Could not get frame filename number %d from pattern '%s'\n",
  61. img->img_number, img->path);
  62. return AVERROR(EIO);
  63. }
  64. for (i = 0; i < 3; i++) {
  65. if (avio_open2(&pb[i], filename, AVIO_FLAG_WRITE,
  66. &s->interrupt_callback, NULL) < 0) {
  67. av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", filename);
  68. return AVERROR(EIO);
  69. }
  70. if (codec->codec_id != AV_CODEC_ID_RAWVIDEO)
  71. break;
  72. filename[strlen(filename) - 1] = 'U' + i;
  73. }
  74. } else {
  75. pb[0] = s->pb;
  76. }
  77. if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) {
  78. int ysize = codec->width * codec->height;
  79. avio_write(pb[0], pkt->data, ysize);
  80. avio_write(pb[1], pkt->data + ysize, (pkt->size - ysize) / 2);
  81. avio_write(pb[2], pkt->data + ysize + (pkt->size - ysize) / 2, (pkt->size - ysize) / 2);
  82. avio_close(pb[1]);
  83. avio_close(pb[2]);
  84. } else {
  85. if (ff_guess_image2_codec(s->filename) == AV_CODEC_ID_JPEG2000) {
  86. AVStream *st = s->streams[0];
  87. if (st->codec->extradata_size > 8 &&
  88. AV_RL32(st->codec->extradata + 4) == MKTAG('j', 'p', '2', 'h')) {
  89. if (pkt->size < 8 ||
  90. AV_RL32(pkt->data + 4) != MKTAG('j', 'p', '2', 'c'))
  91. goto error;
  92. avio_wb32(pb[0], 12);
  93. ffio_wfourcc(pb[0], "jP ");
  94. avio_wb32(pb[0], 0x0D0A870A); // signature
  95. avio_wb32(pb[0], 20);
  96. ffio_wfourcc(pb[0], "ftyp");
  97. ffio_wfourcc(pb[0], "jp2 ");
  98. avio_wb32(pb[0], 0);
  99. ffio_wfourcc(pb[0], "jp2 ");
  100. avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
  101. } else if (pkt->size < 8 ||
  102. (!st->codec->extradata_size &&
  103. AV_RL32(pkt->data + 4) != MKTAG('j', 'P', ' ', ' '))) { // signature
  104. error:
  105. av_log(s, AV_LOG_ERROR, "malformed JPEG 2000 codestream\n");
  106. return -1;
  107. }
  108. }
  109. avio_write(pb[0], pkt->data, pkt->size);
  110. }
  111. avio_flush(pb[0]);
  112. if (!img->is_pipe) {
  113. avio_close(pb[0]);
  114. }
  115. img->img_number++;
  116. return 0;
  117. }
  118. #define OFFSET(x) offsetof(VideoMuxData, x)
  119. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  120. static const AVOption muxoptions[] = {
  121. { "start_number", "first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, ENC },
  122. { "update", "continuously overwrite one file", OFFSET(update), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
  123. { NULL },
  124. };
  125. #if CONFIG_IMAGE2_MUXER
  126. static const AVClass img2mux_class = {
  127. .class_name = "image2 muxer",
  128. .item_name = av_default_item_name,
  129. .option = muxoptions,
  130. .version = LIBAVUTIL_VERSION_INT,
  131. };
  132. AVOutputFormat ff_image2_muxer = {
  133. .name = "image2",
  134. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  135. .extensions = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
  136. "ppm,sgi,tga,tif,tiff,jp2,xwd,sun,ras,rs,im1,im8,im24,"
  137. "sunras,webp,xbm,j2c,pix",
  138. .priv_data_size = sizeof(VideoMuxData),
  139. .video_codec = AV_CODEC_ID_MJPEG,
  140. .write_header = write_header,
  141. .write_packet = write_packet,
  142. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
  143. .priv_class = &img2mux_class,
  144. };
  145. #endif
  146. #if CONFIG_IMAGE2PIPE_MUXER
  147. AVOutputFormat ff_image2pipe_muxer = {
  148. .name = "image2pipe",
  149. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  150. .priv_data_size = sizeof(VideoMuxData),
  151. .video_codec = AV_CODEC_ID_MJPEG,
  152. .write_header = write_header,
  153. .write_packet = write_packet,
  154. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
  155. };
  156. #endif