img2enc.c
Go to the documentation of this file.
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 
23 #include "libavutil/intreadwrite.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 #include "libavutil/opt.h"
32 
33 typedef struct {
34  const AVClass *class; /**< Class for private options. */
36  int is_pipe;
37  int split_planes; /**< use independent file for each Y, U, V plane */
38  char path[1024];
39  int update;
40 } VideoMuxData;
41 
43 {
45  AVStream *st = s->streams[0];
47  const char *str;
48 
49  av_strlcpy(img->path, s->filename, sizeof(img->path));
50 
51  /* find format */
52  if (s->oformat->flags & AVFMT_NOFILE)
53  img->is_pipe = 0;
54  else
55  img->is_pipe = 1;
56 
57  str = strrchr(img->path, '.');
58  img->split_planes = str
59  && !av_strcasecmp(str + 1, "y")
60  && s->nb_streams == 1
62  && desc
63  &&(desc->flags & PIX_FMT_PLANAR)
64  && desc->nb_components >= 3;
65  return 0;
66 }
67 
69 {
71  AVIOContext *pb[4];
72  char filename[1024];
73  AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
74  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(codec->pix_fmt);
75  int i;
76 
77  if (!img->is_pipe) {
78  if (img->update) {
79  av_strlcpy(filename, img->path, sizeof(filename));
80  } else if (av_get_frame_filename(filename, sizeof(filename), img->path, img->img_number) < 0 &&
81  img->img_number > 1) {
83  "Could not get frame filename number %d from pattern '%s' (either set updatefirst or use a pattern like %%03d within the filename pattern)\n",
84  img->img_number, img->path);
85  return AVERROR(EINVAL);
86  }
87  for (i = 0; i < 4; i++) {
88  if (avio_open2(&pb[i], filename, AVIO_FLAG_WRITE,
89  &s->interrupt_callback, NULL) < 0) {
90  av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", filename);
91  return AVERROR(EIO);
92  }
93 
94  if (!img->split_planes || i+1 >= desc->nb_components)
95  break;
96  filename[strlen(filename) - 1] = ((int[]){'U','V','A','x'})[i];
97  }
98  } else {
99  pb[0] = s->pb;
100  }
101 
102  if (img->split_planes) {
103  int ysize = codec->width * codec->height;
104  int usize = ((-codec->width)>>desc->log2_chroma_w) * ((-codec->height)>>desc->log2_chroma_h);
105  if (desc->comp[0].depth_minus1 >= 8) {
106  ysize *= 2;
107  usize *= 2;
108  }
109  avio_write(pb[0], pkt->data , ysize);
110  avio_write(pb[1], pkt->data + ysize , usize);
111  avio_write(pb[2], pkt->data + ysize + usize, usize);
112  avio_close(pb[1]);
113  avio_close(pb[2]);
114  if (desc->nb_components > 3) {
115  avio_write(pb[3], pkt->data + ysize + 2*usize, ysize);
116  avio_close(pb[3]);
117  }
118  } else {
119  avio_write(pb[0], pkt->data, pkt->size);
120  }
121  avio_flush(pb[0]);
122  if (!img->is_pipe) {
123  avio_close(pb[0]);
124  }
125 
126  img->img_number++;
127  return 0;
128 }
129 
130 #define OFFSET(x) offsetof(VideoMuxData, x)
131 #define ENC AV_OPT_FLAG_ENCODING_PARAM
132 static const AVOption muxoptions[] = {
133  { "updatefirst", "continuously overwrite one file", OFFSET(update), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
134  { "update", "continuously overwrite one file", OFFSET(update), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, ENC },
135  { "start_number", "set first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, ENC },
136  { NULL },
137 };
138 
139 #if CONFIG_IMAGE2_MUXER
140 static const AVClass img2mux_class = {
141  .class_name = "image2 muxer",
142  .item_name = av_default_item_name,
143  .option = muxoptions,
144  .version = LIBAVUTIL_VERSION_INT,
145 };
146 
147 AVOutputFormat ff_image2_muxer = {
148  .name = "image2",
149  .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
150  .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
151  "ppm,sgi,tga,tif,tiff,jp2,j2c,xwd,sun,ras,rs,im1,im8,im24,"
152  "sunras,xbm,xface",
153  .priv_data_size = sizeof(VideoMuxData),
154  .video_codec = AV_CODEC_ID_MJPEG,
158  .priv_class = &img2mux_class,
159 };
160 #endif
161 #if CONFIG_IMAGE2PIPE_MUXER
162 AVOutputFormat ff_image2pipe_muxer = {
163  .name = "image2pipe",
164  .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
165  .priv_data_size = sizeof(VideoMuxData),
166  .video_codec = AV_CODEC_ID_MJPEG,
170 };
171 #endif
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1125
AVOption.
Definition: opt.h:251
av_default_item_name
#define OFFSET(x)
Definition: img2enc.c:130
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:333
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
Format I/O context.
Definition: avformat.h:944
#define img
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:86
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_RAWPICTURE, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT
Definition: avformat.h:397
AVOptions.
static AVPacket pkt
Definition: demuxing.c:56
char path[1024]
Definition: img2enc.c:38
AVStream ** streams
Definition: avformat.h:992
static const AVOption muxoptions[]
Definition: img2enc.c:132
uint8_t * data
static int write_header(AVFormatContext *s)
Definition: img2enc.c:42
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
struct AVOutputFormat * oformat
Definition: avformat.h:958
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: img2enc.c:68
uint16_t depth_minus1
number of bits in the component minus 1
Definition: pixdesc.h:43
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:821
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:82
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:57
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
char filename[1024]
input or output filename
Definition: avformat.h:994
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:212
int width
picture width / height.
const char * name
Definition: avformat.h:378
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Return in &#39;buf&#39; the path with &#39;d&#39; replaced by a number.
Stream structure.
Definition: avformat.h:643
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:352
NULL
Definition: eval.c:55
int img_number
Definition: img2enc.c:35
enum AVCodecID codec_id
#define PIX_FMT_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:93
AVIOContext * pb
I/O context.
Definition: avformat.h:977
uint8_t flags
Definition: pixdesc.h:76
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
Describe the class of an AVClass context structure.
Definition: log.h:50
synthesis window for stochastic i
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:804
#define ENC
Definition: img2enc.c:131
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
static int flags
Definition: cpu.c:23
Main libavformat public API header.
int update
Definition: img2enc.c:39
int split_planes
use independent file for each Y, U, V plane
Definition: img2enc.c:37
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:345
int is_pipe
Definition: img2enc.c:36
void * priv_data
Format private data.
Definition: avformat.h:964
#define AVFMT_NODIMENSIONS
Format does not need width/height.
Definition: avformat.h:356
This structure stores compressed data.