qt: playlist: use item title if available
[vlc.git] / modules / video_output / yuv.c
blobe7460a1d2fb388e7fe75e64a09bc2049574fe274
1 /*****************************************************************************
2 * yuv.c : yuv video output
3 *****************************************************************************
4 * Copyright (C) 2008, M2X BV
6 * Authors: Jean-Paul Saman <jpsaman@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program 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
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_display.h>
34 #include <vlc_fs.h>
36 /*****************************************************************************
37 * Module descriptor
38 *****************************************************************************/
39 #define YUV_FILE_TEXT N_("device, fifo or filename")
40 #define YUV_FILE_LONGTEXT N_("device, fifo or filename to write yuv frames too.")
42 #define CHROMA_TEXT N_("Chroma used")
43 #define CHROMA_LONGTEXT N_(\
44 "Force use of a specific chroma for output.")
46 #define YUV4MPEG2_TEXT N_("Add a YUV4MPEG2 header")
47 #define YUV4MPEG2_LONGTEXT N_("The YUV4MPEG2 header is compatible " \
48 "with mplayer yuv video output and requires YV12/I420 fourcc.")
50 #define CFG_PREFIX "yuv-"
52 static int Open(vout_display_t *vd, const vout_display_cfg_t *cfg,
53 video_format_t *fmtp, vlc_video_context *context);
54 static void Close(vout_display_t *vd);
56 vlc_module_begin()
57 set_shortname(N_("YUV output"))
58 set_description(N_("YUV video output"))
59 set_category(CAT_VIDEO)
60 set_subcategory(SUBCAT_VIDEO_VOUT)
62 add_string(CFG_PREFIX "file", "stream.yuv",
63 YUV_FILE_TEXT, YUV_FILE_LONGTEXT, false)
64 add_string(CFG_PREFIX "chroma", NULL,
65 CHROMA_TEXT, CHROMA_LONGTEXT, true)
66 add_bool (CFG_PREFIX "yuv4mpeg2", false,
67 YUV4MPEG2_TEXT, YUV4MPEG2_LONGTEXT, true)
69 set_callback_display(Open, 0)
70 vlc_module_end()
72 /*****************************************************************************
73 * Local prototypes
74 *****************************************************************************/
76 /* */
77 static void Display(vout_display_t *, picture_t *);
78 static int Control(vout_display_t *, int);
80 /*****************************************************************************
81 * vout_display_sys_t: video output descriptor
82 *****************************************************************************/
83 struct vout_display_sys_t {
84 FILE *f;
85 bool is_first;
86 bool is_yuv4mpeg2;
89 static const struct vlc_display_operations ops = {
90 Close, NULL, Display, Control, NULL, NULL,
93 /* */
94 static int Open(vout_display_t *vd, const vout_display_cfg_t *cfg,
95 video_format_t *fmtp, vlc_video_context *context)
97 vout_display_sys_t *sys;
99 /* Allocate instance and initialize some members */
100 vd->sys = sys = malloc(sizeof(*sys));
101 if (!sys)
102 return VLC_ENOMEM;
104 sys->is_first = false;
105 sys->is_yuv4mpeg2 = var_InheritBool(vd, CFG_PREFIX "yuv4mpeg2");
107 /* */
108 char *psz_fcc = var_InheritString(vd, CFG_PREFIX "chroma");
109 const vlc_fourcc_t requested_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES,
110 psz_fcc);
111 free(psz_fcc);
113 const vlc_fourcc_t chroma = requested_chroma ? requested_chroma :
114 VLC_CODEC_I420;
115 if (sys->is_yuv4mpeg2) {
116 switch (chroma) {
117 case VLC_CODEC_YV12:
118 case VLC_CODEC_I420:
119 case VLC_CODEC_J420:
120 break;
121 default:
122 msg_Err(vd, "YUV4MPEG2 mode needs chroma YV12 not %4.4s as requested",
123 (char *)&chroma);
124 free(sys);
125 return VLC_EGENERIC;
128 msg_Dbg(vd, "Using chroma %4.4s", (char *)&chroma);
130 /* */
131 char *name = var_InheritString(vd, CFG_PREFIX "file");
132 if (!name) {
133 msg_Err(vd, "Empty file name");
134 free(sys);
135 return VLC_EGENERIC;
137 sys->f = vlc_fopen(name, "wb");
139 if (!sys->f) {
140 msg_Err(vd, "Failed to open %s", name);
141 free(name);
142 free(sys);
143 return VLC_EGENERIC;
145 msg_Dbg(vd, "Writing data to %s", name);
146 free(name);
148 /* */
149 video_format_t fmt;
150 video_format_ApplyRotation(&fmt, vd->source);
151 fmt.i_chroma = chroma;
152 video_format_FixRgb(&fmt);
154 /* */
155 *fmtp = fmt;
156 vd->ops = &ops;
158 (void) cfg; (void) context;
159 return VLC_SUCCESS;
162 /* */
163 static void Close(vout_display_t *vd)
165 vout_display_sys_t *sys = vd->sys;
167 fclose(sys->f);
168 free(sys);
171 /*****************************************************************************
173 *****************************************************************************/
174 static void Display(vout_display_t *vd, picture_t *picture)
176 vout_display_sys_t *sys = vd->sys;
178 /* */
179 char type;
180 if (picture->b_progressive)
181 type = 'p';
182 else if (picture->b_top_field_first)
183 type = 't';
184 else
185 type = 'b';
187 if (type != 'p') {
188 msg_Warn(vd, "Received a non progressive frame, "
189 "it will be written as progressive.");
190 type = 'p';
193 /* */
194 if (!sys->is_first) {
195 const char *header;
196 char buffer[5];
197 video_format_t fmt = *vd->fmt;
199 if (ORIENT_IS_SWAP(vd->source->orientation))
201 fmt.i_sar_num = vd->source->i_sar_den;
202 fmt.i_sar_den = vd->source->i_sar_num;
204 else
206 fmt.i_sar_num = vd->source->i_sar_num;
207 fmt.i_sar_den = vd->source->i_sar_den;
210 if (sys->is_yuv4mpeg2) {
211 /* MPlayer compatible header, unfortunately it doesn't tell you
212 * the exact fourcc used. */
213 header = "YUV4MPEG2";
214 } else {
215 snprintf(buffer, sizeof(buffer), "%4.4s",
216 (const char*)&fmt.i_chroma);
217 header = buffer;
220 fprintf(sys->f, "%s W%d H%d F%d:%d I%c A%d:%d\n",
221 header,
222 fmt.i_visible_width, fmt.i_visible_height,
223 fmt.i_frame_rate, fmt.i_frame_rate_base,
224 type,
225 fmt.i_sar_num, fmt.i_sar_den);
226 sys->is_first = true;
229 /* */
230 fprintf(sys->f, "FRAME\n");
231 for (int i = 0; i < picture->i_planes; i++) {
232 const plane_t *plane = &picture->p[i];
233 const uint8_t *pixels = plane->p_pixels;
235 pixels += (vd->fmt->i_x_offset * plane->i_visible_pitch)
236 / vd->fmt->i_visible_height;
238 for( int y = 0; y < plane->i_visible_lines; y++) {
239 const size_t written = fwrite(pixels, 1, plane->i_visible_pitch,
240 sys->f);
241 if (written != (size_t)plane->i_visible_pitch)
242 msg_Warn(vd, "only %zd of %d bytes written",
243 written, plane->i_visible_pitch);
245 pixels += plane->i_pitch;
248 fflush(sys->f);
251 static int Control(vout_display_t *vd, int query)
253 (void) vd;
255 switch (query) {
256 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
257 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
258 case VOUT_DISPLAY_CHANGE_ZOOM:
259 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
260 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
261 return VLC_SUCCESS;
263 return VLC_EGENERIC;