1 /*
2 * Copyright (C) 2000-2018 the xine project
3 *
4 * This file is part of xine, a free video player.
5 *
6 * xine is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * xine is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 */
20
21 /*
22 * YUV4MPEG2 File Demuxer by Mike Melanson (melanson@pcisys.net)
23 * For more information regarding the YUV4MPEG2 file format and associated
24 * tools, visit:
25 * http://mjpeg.sourceforge.net/
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 #define LOG_MODULE "demux_yuv4mpeg2"
40 #include "group_video.h"
41
42 #include <xine/xine_internal.h>
43 #include <xine/xineutils.h>
44 #include <xine/compat.h>
45 #include <xine/demux.h>
46 #include "bswap.h"
47
48 #define Y4M_SIGNATURE_SIZE 9
49 #define Y4M_SIGNATURE "YUV4MPEG2"
50 #define Y4M_FRAME_SIGNATURE_SIZE 6
51 #define Y4M_FRAME_SIGNATURE "FRAME\x0A"
52 /* number of header bytes is completely arbitrary */
53 #define Y4M_HEADER_BYTES 100
54
55 typedef struct {
56 demux_plugin_t demux_plugin;
57
58 xine_stream_t *stream;
59 fifo_buffer_t *video_fifo;
60 fifo_buffer_t *audio_fifo;
61 input_plugin_t *input;
62 int status;
63
64 off_t data_start;
65 off_t data_size;
66
67 xine_bmiheader bih;
68
69 int fps_n;
70 int fps_d;
71 int aspect_n;
72 int aspect_d;
73 int progressive;
74 int top_field_first;
75 int color_matrix;
76
77 unsigned int frame_pts_inc;
78 unsigned int frame_size;
79
80 int seek_flag;
81 } demux_yuv4mpeg2_t;
82
83
84 /* returns 1 if the YUV4MPEG2 file was opened successfully, 0 otherwise */
open_yuv4mpeg2_file(demux_yuv4mpeg2_t * this)85 static int open_yuv4mpeg2_file(demux_yuv4mpeg2_t *this) {
86 char header[Y4M_HEADER_BYTES+1];
87 char *header_ptr, *header_endptr, *header_end;
88
89 this->bih.biWidth = this->bih.biHeight = this->fps_n = this->fps_d =
90 this->aspect_n = this->aspect_d = this->progressive =
91 this->top_field_first = this->data_start = 0;
92
93 this->color_matrix = 4; /* undefined, mpeg range */
94
95 if (_x_demux_read_header(this->input, header, Y4M_HEADER_BYTES) != Y4M_HEADER_BYTES)
96 return 0;
97
98 /* check for the Y4M signature */
99 if (memcmp(header, Y4M_SIGNATURE, Y4M_SIGNATURE_SIZE) != 0)
100 return 0;
101
102 /* null terminate the read data */
103 header[Y4M_HEADER_BYTES] = '\0';
104
105 /* check for stream header terminator */
106 if ((header_end = strchr(header, '\n')) == NULL)
107 return 0;
108
109 /* read tagged fields in stream header */
110 header_ptr = &header[Y4M_SIGNATURE_SIZE];
111 while (header_ptr < header_end) {
112 /* tagged fields should all start with a space */
113 if(*header_ptr != ' ')
114 break;
115 else
116 header_ptr++;
117
118 switch (*header_ptr) {
119 case 'W':
120 /* read the width */
121 this->bih.biWidth = strtol(header_ptr + 1, &header_endptr, 10);
122 if(header_endptr == header_ptr + 1)
123 return 0;
124 else
125 header_ptr = header_endptr;
126 break;
127 case 'H':
128 /* read the height */
129 this->bih.biHeight = strtol(header_ptr + 1, &header_endptr, 10);
130 if (header_endptr == header_ptr + 1)
131 return 0;
132 else
133 header_ptr = header_endptr;
134 break;
135 case 'I':
136 /* read interlacing spec */
137 switch (*(header_ptr + 1)) {
138 case 'p':
139 this->progressive = 1;
140 break;
141 case 't':
142 this->top_field_first = 1;
143 break;
144 case 'b':
145 case '?':
146 default:
147 break;
148 }
149 header_ptr += 2;
150 break;
151 case 'F':
152 /* read frame rate - stored as a ratio
153 * numberator */
154 this->fps_n = strtol(header_ptr + 1, &header_endptr, 10);
155 if ((header_endptr == header_ptr + 1) || (*header_endptr != ':'))
156 return 0;
157 else
158 header_ptr = header_endptr;
159
160 /* denominator */
161 this->fps_d = strtol(header_ptr + 1, &header_endptr, 10);
162 if (header_endptr == header_ptr + 1)
163 return 0;
164 else
165 header_ptr = header_endptr;
166
167 break;
168 case 'A':
169 /* read aspect ratio - stored as a ratio(!)
170 * numerator */
171 this->aspect_n = strtol(header_ptr + 1, &header_endptr, 10);
172 if ((header_endptr == header_ptr + 1) || (*header_endptr != ':'))
173 return 0;
174 else
175 header_ptr = header_endptr;
176
177 /* denominator */
178 this->aspect_d = strtol(header_ptr + 1, &header_endptr, 10);
179 if (header_endptr == header_ptr + 1)
180 return 0;
181 else
182 header_ptr = header_endptr;
183
184 break;
185 case 'X':
186 /* private extra info */
187 if (!strncasecmp (header_ptr + 1, "XINE_CM=", 8)) {
188 int i = strtol(header_ptr + 9, &header_endptr, 10);
189 if (header_endptr > header_ptr + 9) {
190 this->color_matrix = i;
191 header_ptr = header_endptr;
192 break;
193 }
194 }
195 /* fall through */
196 default:
197 /* skip whatever this is */
198 while ((*header_ptr != ' ') && (header_ptr < header_end))
199 header_ptr++;
200 }
201 }
202
203 /* make sure all the data was found */
204 if (!this->bih.biWidth || !this->bih.biHeight || !this->fps_n || !this->fps_d)
205 return 0;
206
207 /* compute the size of an individual frame */
208 this->frame_size = this->bih.biWidth * this->bih.biHeight * 3 / 2;
209
210 /* pts difference between frames */
211 this->frame_pts_inc = (90000 * this->fps_d) / this->fps_n;
212
213 /* finally, look for the first frame */
214 size_t left = (size_t)Y4M_HEADER_BYTES - (size_t)(header_ptr - header);
215 char *data_start_ptr = memmem(header_ptr, left, "FRAME", 5);
216
217 /* make sure the first frame was found */
218 if ( !data_start_ptr ) {
219 return 0;
220 }
221
222 this->data_start = data_start_ptr - header;
223
224 /* compute size of all frames */
225 if (INPUT_IS_SEEKABLE(this->input)) {
226 this->data_size = this->input->get_length(this->input) -
227 this->data_start;
228 }
229
230 /* file is qualified; seek to first frame */
231 if (this->input->seek(this->input, this->data_start, SEEK_SET) != this->data_start)
232 return 0;
233
234 return 1;
235 }
236
demux_yuv4mpeg2_send_chunk(demux_plugin_t * this_gen)237 static int demux_yuv4mpeg2_send_chunk(demux_plugin_t *this_gen) {
238 demux_yuv4mpeg2_t *this = (demux_yuv4mpeg2_t *) this_gen;
239
240 /* validate that this is an actual frame boundary */
241 {
242 uint8_t preamble[Y4M_FRAME_SIGNATURE_SIZE];
243 if (this->input->read(this->input, preamble, Y4M_FRAME_SIGNATURE_SIZE) !=
244 Y4M_FRAME_SIGNATURE_SIZE) {
245 this->status = DEMUX_FINISHED;
246 return this->status;
247 }
248 if (memcmp(preamble, Y4M_FRAME_SIGNATURE, Y4M_FRAME_SIGNATURE_SIZE) !=
249 0) {
250 this->status = DEMUX_FINISHED;
251 return this->status;
252 }
253 }
254
255 /* load and dispatch the raw frame */
256 int bytes_remaining = this->frame_size;
257 off_t current_file_pos =
258 this->input->get_current_pos(this->input) - this->data_start;
259 int64_t pts = current_file_pos;
260 pts /= (this->frame_size + Y4M_FRAME_SIGNATURE_SIZE);
261 pts *= this->frame_pts_inc;
262
263 /* reset the pts after a seek */
264 if (this->seek_flag) {
265 _x_demux_control_newpts(this->stream, pts, BUF_FLAG_SEEK);
266 this->seek_flag = 0;
267 }
268
269 while(bytes_remaining) {
270 buf_element_t *buf = this->video_fifo->buffer_pool_size_alloc (this->video_fifo, bytes_remaining);
271 buf->type = BUF_VIDEO_I420;
272 if( this->data_size )
273 buf->extra_info->input_normpos = (int)((double) current_file_pos * 65535 / this->data_size);
274 buf->extra_info->input_time = pts / 90;
275 buf->pts = pts;
276
277 buf->decoder_flags |= BUF_FLAG_COLOR_MATRIX;
278 buf->decoder_info[4] = this->color_matrix;
279
280 buf->size = MIN(bytes_remaining, buf->max_size);
281 bytes_remaining -= buf->size;
282
283 if (this->input->read(this->input, buf->content, buf->size) !=
284 buf->size) {
285 buf->free_buffer(buf);
286 this->status = DEMUX_FINISHED;
287 break;
288 }
289
290 if (!bytes_remaining)
291 buf->decoder_flags |= BUF_FLAG_FRAME_END;
292 this->video_fifo->put(this->video_fifo, buf);
293 }
294
295 return this->status;
296 }
297
demux_yuv4mpeg2_send_headers(demux_plugin_t * this_gen)298 static void demux_yuv4mpeg2_send_headers(demux_plugin_t *this_gen) {
299 demux_yuv4mpeg2_t *this = (demux_yuv4mpeg2_t *) this_gen;
300
301 this->video_fifo = this->stream->video_fifo;
302 this->audio_fifo = this->stream->audio_fifo;
303
304 this->status = DEMUX_OK;
305
306 /* load stream information */
307 _x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_VIDEO, 1);
308 _x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_AUDIO, 0);
309 _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_WIDTH,
310 this->bih.biWidth);
311 _x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HEIGHT,
312 this->bih.biHeight);
313
314 /* send start buffers */
315 _x_demux_control_start(this->stream);
316
317 /* send init info to decoders */
318 buf_element_t *buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
319 buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_STDHEADER|BUF_FLAG_FRAMERATE|
320 BUF_FLAG_FRAME_END;
321 buf->decoder_info[0] = this->frame_pts_inc; /* initial video step */
322
323 if(this->aspect_n && this->aspect_d) {
324 buf->decoder_flags |= BUF_FLAG_ASPECT;
325 buf->decoder_info[1] = this->bih.biWidth * this->aspect_n;
326 buf->decoder_info[2] = this->bih.biHeight * this->aspect_d;
327 }
328
329 buf->decoder_info[3] = this->progressive;
330 buf->decoder_info[4] = this->top_field_first;
331
332 memcpy(buf->content, &this->bih, sizeof(this->bih));
333 buf->size = sizeof(this->bih);
334 buf->type = BUF_VIDEO_I420;
335 this->video_fifo->put (this->video_fifo, buf);
336 }
337
demux_yuv4mpeg2_seek(demux_plugin_t * this_gen,off_t start_pos,int start_time,int playing)338 static int demux_yuv4mpeg2_seek (demux_plugin_t *this_gen,
339 off_t start_pos, int start_time, int playing) {
340
341 demux_yuv4mpeg2_t *this = (demux_yuv4mpeg2_t *) this_gen;
342 start_time /= 1000;
343 start_pos = (off_t) ( (double) start_pos / 65535 *
344 this->data_size );
345
346 if (INPUT_IS_SEEKABLE(this->input)) {
347
348 /* YUV4MPEG2 files are essentially constant bit-rate video. Seek along
349 * the calculated frame boundaries. Divide the requested seek offset
350 * by the frame size integer-wise to obtain the desired frame number
351 * and then multiply the frame number by the frame size to get the
352 * starting offset. Add the data_start offset to obtain the final
353 * offset. */
354
355 start_pos /= (this->frame_size + Y4M_FRAME_SIGNATURE_SIZE);
356 start_pos *= (this->frame_size + Y4M_FRAME_SIGNATURE_SIZE);
357 start_pos += this->data_start;
358
359 this->input->seek(this->input, start_pos, SEEK_SET);
360 }
361
362 this->seek_flag = 1;
363 this->status = DEMUX_OK;
364 _x_demux_flush_engine (this->stream);
365
366 /* if thread is not running, initialize demuxer */
367 if( !playing ) {
368
369 /* send new pts */
370 _x_demux_control_newpts(this->stream, 0, 0);
371
372 this->status = DEMUX_OK;
373 }
374
375 return this->status;
376 }
377
demux_yuv4mpeg2_get_status(demux_plugin_t * this_gen)378 static int demux_yuv4mpeg2_get_status (demux_plugin_t *this_gen) {
379 demux_yuv4mpeg2_t *this = (demux_yuv4mpeg2_t *) this_gen;
380
381 return this->status;
382 }
383
demux_yuv4mpeg2_get_stream_length(demux_plugin_t * this_gen)384 static int demux_yuv4mpeg2_get_stream_length (demux_plugin_t *this_gen) {
385 demux_yuv4mpeg2_t *this = (demux_yuv4mpeg2_t *) this_gen;
386
387 return (int)(((int64_t) this->data_size * 1000 * this->fps_d) /
388 ((this->frame_size + Y4M_FRAME_SIGNATURE_SIZE) * this->fps_n));
389 }
390
demux_yuv4mpeg2_get_capabilities(demux_plugin_t * this_gen)391 static uint32_t demux_yuv4mpeg2_get_capabilities(demux_plugin_t *this_gen) {
392 (void)this_gen;
393 return DEMUX_CAP_NOCAP;
394 }
395
demux_yuv4mpeg2_get_optional_data(demux_plugin_t * this_gen,void * data,int data_type)396 static int demux_yuv4mpeg2_get_optional_data(demux_plugin_t *this_gen,
397 void *data, int data_type) {
398 (void)this_gen;
399 (void)data;
400 (void)data_type;
401 return DEMUX_OPTIONAL_UNSUPPORTED;
402 }
403
open_plugin(demux_class_t * class_gen,xine_stream_t * stream,input_plugin_t * input)404 static demux_plugin_t *open_plugin (demux_class_t *class_gen, xine_stream_t *stream,
405 input_plugin_t *input) {
406 demux_yuv4mpeg2_t *this;
407
408 this = calloc(1, sizeof(demux_yuv4mpeg2_t));
409 if (!this)
410 return NULL;
411
412 this->stream = stream;
413 this->input = input;
414
415 this->demux_plugin.send_headers = demux_yuv4mpeg2_send_headers;
416 this->demux_plugin.send_chunk = demux_yuv4mpeg2_send_chunk;
417 this->demux_plugin.seek = demux_yuv4mpeg2_seek;
418 this->demux_plugin.dispose = default_demux_plugin_dispose;
419 this->demux_plugin.get_status = demux_yuv4mpeg2_get_status;
420 this->demux_plugin.get_stream_length = demux_yuv4mpeg2_get_stream_length;
421 this->demux_plugin.get_capabilities = demux_yuv4mpeg2_get_capabilities;
422 this->demux_plugin.get_optional_data = demux_yuv4mpeg2_get_optional_data;
423 this->demux_plugin.demux_class = class_gen;
424
425 this->status = DEMUX_FINISHED;
426
427 switch (stream->content_detection_method) {
428
429 case METHOD_BY_MRL:
430 case METHOD_BY_CONTENT:
431 case METHOD_EXPLICIT:
432
433 if (!open_yuv4mpeg2_file(this)) {
434 free (this);
435 return NULL;
436 }
437
438 break;
439
440 default:
441 free (this);
442 return NULL;
443 }
444
445 return &this->demux_plugin;
446 }
447
demux_yuv4mpeg2_init_class(xine_t * xine,const void * data)448 void *demux_yuv4mpeg2_init_class (xine_t *xine, const void *data) {
449
450 (void)xine;
451 (void)data;
452
453 static const demux_class_t demux_yuv4mpeg2_class = {
454 .open_plugin = open_plugin,
455 .description = N_("YUV4MPEG2 file demux plugin"),
456 .identifier = "YUV4MPEG2",
457 .mimetypes = NULL,
458 .extensions = "y4m",
459 .dispose = NULL,
460 };
461
462 return (void *)&demux_yuv4mpeg2_class;
463 }
464
465