demux: avi: invalidate skipped chunks
[vlc.git] / modules / video_output / yuv.c
blob1fcc9e9f0553930b4b580743a65fbc6e0039d9da
1 /*****************************************************************************
2 * yuv.c : yuv video output
3 *****************************************************************************
4 * Copyright (C) 2008, M2X BV
5 * $Id$
7 * Authors: Jean-Paul Saman <jpsaman@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout_display.h>
35 #include <vlc_picture_pool.h>
36 #include <vlc_fs.h>
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
41 #define YUV_FILE_TEXT N_("device, fifo or filename")
42 #define YUV_FILE_LONGTEXT N_("device, fifo or filename to write yuv frames too.")
44 #define CHROMA_TEXT N_("Chroma used")
45 #define CHROMA_LONGTEXT N_(\
46 "Force use of a specific chroma for output.")
48 #define YUV4MPEG2_TEXT N_("Add a YUV4MPEG2 header")
49 #define YUV4MPEG2_LONGTEXT N_("The YUV4MPEG2 header is compatible " \
50 "with mplayer yuv video output and requires YV12/I420 fourcc.")
52 #define CFG_PREFIX "yuv-"
54 static int Open (vlc_object_t *);
55 static void Close(vlc_object_t *);
57 vlc_module_begin()
58 set_shortname(N_("YUV output"))
59 set_description(N_("YUV video output"))
60 set_category(CAT_VIDEO)
61 set_subcategory(SUBCAT_VIDEO_VOUT)
62 set_capability("vout display", 0)
64 add_string(CFG_PREFIX "file", "stream.yuv",
65 YUV_FILE_TEXT, YUV_FILE_LONGTEXT, false)
66 add_string(CFG_PREFIX "chroma", NULL,
67 CHROMA_TEXT, CHROMA_LONGTEXT, true)
68 add_bool (CFG_PREFIX "yuv4mpeg2", false,
69 YUV4MPEG2_TEXT, YUV4MPEG2_LONGTEXT, true)
71 set_callbacks(Open, Close)
72 vlc_module_end()
74 /*****************************************************************************
75 * Local prototypes
76 *****************************************************************************/
78 /* */
79 static picture_pool_t *Pool (vout_display_t *, unsigned);
80 static void Display(vout_display_t *, picture_t *, subpicture_t *subpicture);
81 static int Control(vout_display_t *, int, va_list);
83 /*****************************************************************************
84 * vout_display_sys_t: video output descriptor
85 *****************************************************************************/
86 struct vout_display_sys_t {
87 FILE *f;
88 bool is_first;
89 bool is_yuv4mpeg2;
91 picture_pool_t *pool;
94 /* */
95 static int Open(vlc_object_t *object)
97 vout_display_t *vd = (vout_display_t *)object;
98 vout_display_sys_t *sys;
100 /* Allocate instance and initialize some members */
101 vd->sys = sys = malloc(sizeof(*sys));
102 if (!sys)
103 return VLC_ENOMEM;
105 sys->is_first = false;
106 sys->is_yuv4mpeg2 = var_InheritBool(vd, CFG_PREFIX "yuv4mpeg2");
107 sys->pool = NULL;
109 /* */
110 char *psz_fcc = var_InheritString(vd, CFG_PREFIX "chroma");
111 const vlc_fourcc_t requested_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES,
112 psz_fcc);
113 free(psz_fcc);
115 const vlc_fourcc_t chroma = requested_chroma ? requested_chroma :
116 VLC_CODEC_I420;
117 if (sys->is_yuv4mpeg2) {
118 switch (chroma) {
119 case VLC_CODEC_YV12:
120 case VLC_CODEC_I420:
121 case VLC_CODEC_J420:
122 break;
123 default:
124 msg_Err(vd, "YUV4MPEG2 mode needs chroma YV12 not %4.4s as requested",
125 (char *)&chroma);
126 free(sys);
127 return VLC_EGENERIC;
130 msg_Dbg(vd, "Using chroma %4.4s", (char *)&chroma);
132 /* */
133 char *name = var_InheritString(vd, CFG_PREFIX "file");
134 if (!name) {
135 msg_Err(vd, "Empty file name");
136 free(sys);
137 return VLC_EGENERIC;
139 sys->f = vlc_fopen(name, "wb");
141 if (!sys->f) {
142 msg_Err(vd, "Failed to open %s", name);
143 free(name);
144 free(sys);
145 return VLC_EGENERIC;
147 msg_Dbg(vd, "Writing data to %s", name);
148 free(name);
150 /* */
151 video_format_t fmt;
152 video_format_ApplyRotation(&fmt, &vd->fmt);
153 fmt.i_chroma = chroma;
154 video_format_FixRgb(&fmt);
156 /* */
157 vout_display_info_t info = vd->info;
158 info.has_hide_mouse = true;
160 /* */
161 vd->fmt = fmt;
162 vd->info = info;
163 vd->pool = Pool;
164 vd->prepare = NULL;
165 vd->display = Display;
166 vd->control = Control;
167 vd->manage = NULL;
169 vout_display_DeleteWindow(vd, NULL);
170 return VLC_SUCCESS;
173 /* */
174 static void Close(vlc_object_t *object)
176 vout_display_t *vd = (vout_display_t *)object;
177 vout_display_sys_t *sys = vd->sys;
179 if (sys->pool)
180 picture_pool_Release(sys->pool);
181 fclose(sys->f);
182 free(sys);
185 /*****************************************************************************
187 *****************************************************************************/
188 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
190 vout_display_sys_t *sys = vd->sys;
191 if (!sys->pool)
192 sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
193 return sys->pool;
196 static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
198 vout_display_sys_t *sys = vd->sys;
200 /* */
201 video_format_t fmt = vd->fmt;
203 if (ORIENT_IS_SWAP(vd->source.orientation))
205 fmt.i_sar_num = vd->source.i_sar_den;
206 fmt.i_sar_den = vd->source.i_sar_num;
208 else
210 fmt.i_sar_num = vd->source.i_sar_num;
211 fmt.i_sar_den = vd->source.i_sar_den;
214 /* */
215 char type;
216 if (picture->b_progressive)
217 type = 'p';
218 else if (picture->b_top_field_first)
219 type = 't';
220 else
221 type = 'b';
223 if (type != 'p') {
224 msg_Warn(vd, "Received a non progressive frame, "
225 "it will be written as progressive.");
226 type = 'p';
229 /* */
230 if (!sys->is_first) {
231 const char *header;
232 char buffer[5];
233 if (sys->is_yuv4mpeg2) {
234 /* MPlayer compatible header, unfortunately it doesn't tell you
235 * the exact fourcc used. */
236 header = "YUV4MPEG2";
237 } else {
238 snprintf(buffer, sizeof(buffer), "%4.4s",
239 (const char*)&fmt.i_chroma);
240 header = buffer;
243 fprintf(sys->f, "%s W%d H%d F%d:%d I%c A%d:%d\n",
244 header,
245 fmt.i_visible_width, fmt.i_visible_height,
246 fmt.i_frame_rate, fmt.i_frame_rate_base,
247 type,
248 fmt.i_sar_num, fmt.i_sar_den);
249 sys->is_first = true;
252 /* */
253 fprintf(sys->f, "FRAME\n");
254 for (int i = 0; i < picture->i_planes; i++) {
255 const plane_t *plane = &picture->p[i];
256 const uint8_t *pixels = plane->p_pixels;
258 pixels += (vd->fmt.i_x_offset * plane->i_visible_pitch)
259 / vd->fmt.i_visible_height;
261 for( int y = 0; y < plane->i_visible_lines; y++) {
262 const size_t written = fwrite(pixels, 1, plane->i_visible_pitch,
263 sys->f);
264 if (written != (size_t)plane->i_visible_pitch)
265 msg_Warn(vd, "only %zd of %d bytes written",
266 written, plane->i_visible_pitch);
268 pixels += plane->i_pitch;
271 fflush(sys->f);
273 /* */
274 picture_Release(picture);
275 VLC_UNUSED(subpicture);
278 static int Control(vout_display_t *vd, int query, va_list args)
280 (void) vd; (void) query; (void) args;
281 return VLC_EGENERIC;