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 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_display.h>
36 /*****************************************************************************
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
);
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)
72 /*****************************************************************************
74 *****************************************************************************/
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
{
89 static const struct vlc_display_operations ops
= {
90 Close
, NULL
, Display
, Control
, NULL
, NULL
,
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
));
104 sys
->is_first
= false;
105 sys
->is_yuv4mpeg2
= var_InheritBool(vd
, CFG_PREFIX
"yuv4mpeg2");
108 char *psz_fcc
= var_InheritString(vd
, CFG_PREFIX
"chroma");
109 const vlc_fourcc_t requested_chroma
= vlc_fourcc_GetCodecFromString(VIDEO_ES
,
113 const vlc_fourcc_t chroma
= requested_chroma
? requested_chroma
:
115 if (sys
->is_yuv4mpeg2
) {
122 msg_Err(vd
, "YUV4MPEG2 mode needs chroma YV12 not %4.4s as requested",
128 msg_Dbg(vd
, "Using chroma %4.4s", (char *)&chroma
);
131 char *name
= var_InheritString(vd
, CFG_PREFIX
"file");
133 msg_Err(vd
, "Empty file name");
137 sys
->f
= vlc_fopen(name
, "wb");
140 msg_Err(vd
, "Failed to open %s", name
);
145 msg_Dbg(vd
, "Writing data to %s", name
);
150 video_format_ApplyRotation(&fmt
, vd
->source
);
151 fmt
.i_chroma
= chroma
;
152 video_format_FixRgb(&fmt
);
158 (void) cfg
; (void) context
;
163 static void Close(vout_display_t
*vd
)
165 vout_display_sys_t
*sys
= vd
->sys
;
171 /*****************************************************************************
173 *****************************************************************************/
174 static void Display(vout_display_t
*vd
, picture_t
*picture
)
176 vout_display_sys_t
*sys
= vd
->sys
;
180 if (picture
->b_progressive
)
182 else if (picture
->b_top_field_first
)
188 msg_Warn(vd
, "Received a non progressive frame, "
189 "it will be written as progressive.");
194 if (!sys
->is_first
) {
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
;
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";
215 snprintf(buffer
, sizeof(buffer
), "%4.4s",
216 (const char*)&fmt
.i_chroma
);
220 fprintf(sys
->f
, "%s W%d H%d F%d:%d I%c A%d:%d\n",
222 fmt
.i_visible_width
, fmt
.i_visible_height
,
223 fmt
.i_frame_rate
, fmt
.i_frame_rate_base
,
225 fmt
.i_sar_num
, fmt
.i_sar_den
);
226 sys
->is_first
= true;
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
,
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
;
251 static int Control(vout_display_t
*vd
, int 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
: