1 /*
2 * This file is part of bino, a 3D video player.
3 *
4 * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015
5 * Martin Lambers <marlam@marlam.de>
6 * Frédéric Devernay <frederic.devernay@inrialpes.fr>
7 * Joe <cuchac@email.cz>
8 * D. Matz <bandregent@yahoo.de>
9 * Vittorio Giovara <vittorio.giovara@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "config.h"
26
27 extern "C"
28 {
29 #define __STDC_CONSTANT_MACROS
30 #include <libavformat/avformat.h>
31 #include <libavdevice/avdevice.h>
32 #include <libavcodec/avcodec.h>
33 #include <libswscale/swscale.h>
34 }
35
36 #include <deque>
37 #include <limits>
38 #include <cerrno>
39 #include <cstring>
40 #include <cctype>
41
42 #if HAVE_SYSCONF
43 # include <unistd.h>
44 #else
45 # include <windows.h>
46 #endif
47
48 #include "base/dbg.h"
49 #include "base/blb.h"
50 #include "base/exc.h"
51 #include "base/msg.h"
52 #include "base/str.h"
53 #include "base/pth.h"
54
55 #include "base/gettext.h"
56 #define _(string) gettext(string)
57
58 #include "media_object.h"
59
60 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 25, 0)
61 #define AV_CODEC_ID_TEXT CODEC_ID_TEXT
62 #endif
63
64 // The read thread.
65 // This thread reads packets from the AVFormatContext and stores them in the
66 // appropriate packet queues.
67 class read_thread : public thread
68 {
69 private:
70 const std::string _url;
71 const bool _is_device;
72 struct ffmpeg_stuff *_ffmpeg;
73 bool _eof;
74
75 public:
76 read_thread(const std::string &url, bool is_device, struct ffmpeg_stuff *ffmpeg);
77 void run();
78 void reset();
eof() const79 bool eof() const
80 {
81 return _eof;
82 }
83 };
84
85 // The video decode thread.
86 // This thread reads packets from its packet queue and decodes them to video frames.
87 class video_decode_thread : public thread
88 {
89 private:
90 std::string _url;
91 struct ffmpeg_stuff *_ffmpeg;
92 int _video_stream;
93 video_frame _frame;
94 int _raw_frames;
95
96 int64_t handle_timestamp(int64_t timestamp);
97
98 public:
99 video_decode_thread(const std::string &url, struct ffmpeg_stuff *ffmpeg, int video_stream);
set_raw_frames(int raw_frames)100 void set_raw_frames(int raw_frames)
101 {
102 _raw_frames = raw_frames;
103 }
104 void run();
frame()105 const video_frame &frame()
106 {
107 return _frame;
108 }
109 };
110
111 // The audio decode thread.
112 // This thread reads packets from its packet queue and decodes them to audio blobs.
113 class audio_decode_thread : public thread
114 {
115 private:
116 std::string _url;
117 struct ffmpeg_stuff *_ffmpeg;
118 int _audio_stream;
119 audio_blob _blob;
120
121 int64_t handle_timestamp(int64_t timestamp);
122
123 public:
124 audio_decode_thread(const std::string &url, struct ffmpeg_stuff *ffmpeg, int audio_stream);
125 void run();
blob()126 const audio_blob &blob()
127 {
128 return _blob;
129 }
130 };
131
132 // The subtitle decode thread.
133 // This thread reads packets from its packet queue and decodes them to subtitle boxes.
134 class subtitle_decode_thread : public thread
135 {
136 private:
137 std::string _url;
138 struct ffmpeg_stuff *_ffmpeg;
139 int _subtitle_stream;
140 subtitle_box _box;
141
142 int64_t handle_timestamp(int64_t timestamp);
143
144 public:
145 subtitle_decode_thread(const std::string &url, struct ffmpeg_stuff *ffmpeg, int subtitle_stream);
146 void run();
box()147 const subtitle_box &box()
148 {
149 return _box;
150 }
151 };
152
153
154 // Hide the FFmpeg stuff so that their messy header files cannot cause problems
155 // in other source files.
156
157 static const size_t max_audio_frame_size = 192000; // 1 second of 48khz 32bit audio
158 static const size_t audio_tmpbuf_size = (max_audio_frame_size * 3) / 2;
159
160 struct ffmpeg_stuff
161 {
162 AVFormatContext *format_ctx;
163
164 bool have_active_audio_stream;
165 int64_t pos;
166
167 read_thread *reader;
168
169 std::vector<int> video_streams;
170 std::vector<AVCodecContext *> video_codec_ctxs;
171 std::vector<video_frame> video_frame_templates;
172 std::vector<struct SwsContext *> video_sws_ctxs;
173 std::vector<AVCodec *> video_codecs;
174 std::vector<std::deque<AVPacket> > video_packet_queues;
175 std::vector<mutex> video_packet_queue_mutexes;
176 std::vector<AVPacket> video_packets;
177 std::vector<video_decode_thread> video_decode_threads;
178 std::vector<AVFrame *> video_frames;
179 std::vector<AVFrame *> video_buffered_frames;
180 std::vector<uint8_t *> video_buffers;
181 std::vector<AVFrame *> video_sws_frames;
182 std::vector<uint8_t *> video_sws_buffers;
183 std::vector<int64_t> video_last_timestamps;
184
185 std::vector<int> audio_streams;
186 std::vector<AVCodecContext *> audio_codec_ctxs;
187 std::vector<audio_blob> audio_blob_templates;
188 std::vector<AVCodec *> audio_codecs;
189 std::vector<std::deque<AVPacket> > audio_packet_queues;
190 std::vector<mutex> audio_packet_queue_mutexes;
191 std::vector<audio_decode_thread> audio_decode_threads;
192 std::vector<unsigned char *> audio_tmpbufs;
193 std::vector<blob> audio_blobs;
194 std::vector<std::vector<unsigned char> > audio_buffers;
195 std::vector<int64_t> audio_last_timestamps;
196
197 std::vector<int> subtitle_streams;
198 std::vector<AVCodecContext *> subtitle_codec_ctxs;
199 std::vector<subtitle_box> subtitle_box_templates;
200 std::vector<AVCodec *> subtitle_codecs;
201 std::vector<std::deque<AVPacket> > subtitle_packet_queues;
202 std::vector<mutex> subtitle_packet_queue_mutexes;
203 std::vector<subtitle_decode_thread> subtitle_decode_threads;
204 std::vector<std::deque<subtitle_box> > subtitle_box_buffers;
205 std::vector<int64_t> subtitle_last_timestamps;
206 };
207
208 // Use one decoding thread per processor for video decoding.
video_decoding_threads()209 static int video_decoding_threads()
210 {
211 static long n = -1;
212 if (n < 0)
213 {
214 #ifdef HAVE_SYSCONF
215 n = sysconf(_SC_NPROCESSORS_ONLN);
216 #else
217 SYSTEM_INFO si;
218 GetSystemInfo(&si);
219 n = si.dwNumberOfProcessors;
220 #endif
221 if (n < 1)
222 {
223 n = 1;
224 }
225 else if (n > 16)
226 {
227 n = 16;
228 }
229 }
230 return n;
231 }
232
233 // Return FFmpeg error as std::string.
my_av_strerror(int err)234 static std::string my_av_strerror(int err)
235 {
236 blob b(1024);
237 av_strerror(err, b.ptr<char>(), b.size());
238 return std::string(b.ptr<const char>());
239 }
240
241 // Convert FFmpeg log messages to our log messages.
my_av_log(void * ptr,int level,const char * fmt,va_list vl)242 static void my_av_log(void *ptr, int level, const char *fmt, va_list vl)
243 {
244 static mutex line_mutex;
245 static std::string line;
246 if (level > av_log_get_level())
247 {
248 return;
249 }
250
251 line_mutex.lock();
252 std::string p;
253 AVClass* avc = ptr ? *reinterpret_cast<AVClass**>(ptr) : NULL;
254 if (avc)
255 {
256 p = str::asprintf("[%s @ %p] ", avc->item_name(ptr), ptr);
257 }
258 std::string s = str::vasprintf(fmt, vl);
259 bool line_ends = false;
260 if (s.length() > 0)
261 {
262 if (s[s.length() - 1] == '\n')
263 {
264 line_ends = true;
265 s.erase(s.length() - 1);
266 }
267 }
268 line += s;
269 if (line_ends)
270 {
271 msg::level_t l;
272 switch (level)
273 {
274 case AV_LOG_PANIC:
275 case AV_LOG_FATAL:
276 case AV_LOG_ERROR:
277 l = msg::ERR;
278 break;
279 case AV_LOG_WARNING:
280 l = msg::WRN;
281 break;
282 case AV_LOG_INFO:
283 case AV_LOG_VERBOSE:
284 case AV_LOG_DEBUG:
285 default:
286 l = msg::DBG;
287 break;
288 }
289 size_t n;
290 while ((n = line.find('\n')) != std::string::npos)
291 {
292 msg::msg(l, std::string("FFmpeg: ") + p + line.substr(0, n));
293 line = line.substr(n + 1);
294 }
295 msg::msg(l, std::string("FFmpeg: ") + p + line);
296 line.clear();
297 }
298 line_mutex.unlock();
299 }
300
301 // Handle timestamps
timestamp_helper(int64_t & last_timestamp,int64_t timestamp)302 static int64_t timestamp_helper(int64_t &last_timestamp, int64_t timestamp)
303 {
304 if (timestamp == std::numeric_limits<int64_t>::min())
305 {
306 timestamp = last_timestamp;
307 }
308 last_timestamp = timestamp;
309 return timestamp;
310 }
311
312 // Get a stream duration
stream_duration(AVStream * stream,AVFormatContext * format)313 static int64_t stream_duration(AVStream *stream, AVFormatContext *format)
314 {
315 // Try to get duration from the stream first. If that fails, fall back to
316 // the value provided by the container.
317 int64_t duration = stream->duration;
318 if (duration > 0)
319 {
320 AVRational time_base = stream->time_base;
321 return duration * 1000000 * time_base.num / time_base.den;
322 }
323 else
324 {
325 duration = format->duration;
326 return duration * 1000000 / AV_TIME_BASE;
327 }
328 }
329
330 // Get a file name (or URL) extension, if any
get_extension(const std::string & url)331 static std::string get_extension(const std::string& url)
332 {
333 std::string extension;
334 size_t dot_pos = url.find_last_of('.');
335 if (dot_pos != std::string::npos) {
336 extension = url.substr(dot_pos + 1);
337 for (size_t i = 0; i < extension.length(); i++)
338 extension[i] = std::tolower(extension[i]);
339 }
340 return extension;
341 }
342
343
media_object(bool always_convert_to_bgra32)344 media_object::media_object(bool always_convert_to_bgra32) :
345 _always_convert_to_bgra32(always_convert_to_bgra32), _ffmpeg(NULL)
346 {
347 avdevice_register_all();
348 av_register_all();
349 avformat_network_init();
350 switch (msg::level())
351 {
352 case msg::DBG:
353 av_log_set_level(AV_LOG_DEBUG);
354 break;
355 case msg::INF:
356 av_log_set_level(AV_LOG_INFO);
357 break;
358 case msg::WRN:
359 av_log_set_level(AV_LOG_WARNING);
360 break;
361 case msg::ERR:
362 av_log_set_level(AV_LOG_ERROR);
363 break;
364 case msg::REQ:
365 default:
366 av_log_set_level(AV_LOG_FATAL);
367 break;
368 }
369 av_log_set_callback(my_av_log);
370 }
371
~media_object()372 media_object::~media_object()
373 {
374 if (_ffmpeg)
375 {
376 close();
377 }
378 }
379
set_video_frame_template(int index,int width_before_avcodec_open,int height_before_avcodec_open)380 void media_object::set_video_frame_template(int index, int width_before_avcodec_open, int height_before_avcodec_open)
381 {
382 AVStream *video_stream = _ffmpeg->format_ctx->streams[_ffmpeg->video_streams[index]];
383 AVCodecContext *video_codec_ctx = _ffmpeg->video_codec_ctxs[index];
384 video_frame &video_frame_template = _ffmpeg->video_frame_templates[index];
385
386 // Dimensions and aspect ratio
387 video_frame_template.raw_width = video_codec_ctx->width;
388 video_frame_template.raw_height = video_codec_ctx->height;
389 // XXX Use width/height values from before avcodec_open() if they
390 // differ and seem safe to use. See also media_object::open().
391 if (width_before_avcodec_open >= 1
392 && height_before_avcodec_open >= 1
393 && width_before_avcodec_open <= video_codec_ctx->width
394 && height_before_avcodec_open <= video_codec_ctx->height
395 && (width_before_avcodec_open != video_codec_ctx->width
396 || height_before_avcodec_open != video_codec_ctx->height))
397 {
398 msg::dbg("%s video stream %d: using frame size %dx%d instead of %dx%d.",
399 _url.c_str(), index + 1,
400 width_before_avcodec_open, height_before_avcodec_open,
401 video_codec_ctx->width, video_codec_ctx->height);
402 video_frame_template.raw_width = width_before_avcodec_open;
403 video_frame_template.raw_height = height_before_avcodec_open;
404 }
405 int ar_num = 1;
406 int ar_den = 1;
407 int ar_snum = video_stream->sample_aspect_ratio.num;
408 int ar_sden = video_stream->sample_aspect_ratio.den;
409 int ar_cnum = video_codec_ctx->sample_aspect_ratio.num;
410 int ar_cden = video_codec_ctx->sample_aspect_ratio.den;
411 if (ar_cnum > 0 && ar_cden > 0)
412 {
413 ar_num = ar_cnum;
414 ar_den = ar_cden;
415 }
416 else if (ar_snum > 0 && ar_sden > 0)
417 {
418 ar_num = ar_snum;
419 ar_den = ar_sden;
420 }
421 video_frame_template.raw_aspect_ratio =
422 static_cast<float>(ar_num * video_frame_template.raw_width)
423 / static_cast<float>(ar_den * video_frame_template.raw_height);
424 // Data layout and color space
425 video_frame_template.layout = video_frame::bgra32;
426 video_frame_template.color_space = video_frame::srgb;
427 video_frame_template.value_range = video_frame::u8_full;
428 video_frame_template.chroma_location = video_frame::center;
429 if (!_always_convert_to_bgra32
430 && (video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV444P
431 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV444P10
432 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV422P
433 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV422P10
434 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV420P
435 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV420P10))
436 {
437 if (video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV444P
438 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV444P10)
439 {
440 video_frame_template.layout = video_frame::yuv444p;
441 }
442 else if (video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV422P
443 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV422P10)
444 {
445 video_frame_template.layout = video_frame::yuv422p;
446 }
447 else
448 {
449 video_frame_template.layout = video_frame::yuv420p;
450 }
451 video_frame_template.color_space = video_frame::yuv601;
452 if (video_codec_ctx->colorspace == AVCOL_SPC_BT709)
453 {
454 video_frame_template.color_space = video_frame::yuv709;
455 }
456 if (video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV444P10
457 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV422P10
458 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUV420P10)
459 {
460 video_frame_template.value_range = video_frame::u10_mpeg;
461 if (video_codec_ctx->color_range == AVCOL_RANGE_JPEG)
462 {
463 video_frame_template.value_range = video_frame::u10_full;
464 }
465 }
466 else
467 {
468 video_frame_template.value_range = video_frame::u8_mpeg;
469 if (video_codec_ctx->color_range == AVCOL_RANGE_JPEG)
470 {
471 video_frame_template.value_range = video_frame::u8_full;
472 }
473 }
474 video_frame_template.chroma_location = video_frame::center;
475 if (video_codec_ctx->chroma_sample_location == AVCHROMA_LOC_LEFT)
476 {
477 video_frame_template.chroma_location = video_frame::left;
478 }
479 else if (video_codec_ctx->chroma_sample_location == AVCHROMA_LOC_TOPLEFT)
480 {
481 video_frame_template.chroma_location = video_frame::topleft;
482 }
483 }
484 else if (!_always_convert_to_bgra32
485 && (video_codec_ctx->pix_fmt == AV_PIX_FMT_YUVJ444P
486 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUVJ422P
487 || video_codec_ctx->pix_fmt == AV_PIX_FMT_YUVJ420P))
488 {
489 if (video_codec_ctx->pix_fmt == AV_PIX_FMT_YUVJ444P)
490 {
491 video_frame_template.layout = video_frame::yuv444p;
492 }
493 else if (video_codec_ctx->pix_fmt == AV_PIX_FMT_YUVJ422P)
494 {
495 video_frame_template.layout = video_frame::yuv422p;
496 }
497 else
498 {
499 video_frame_template.layout = video_frame::yuv420p;
500 }
501 video_frame_template.color_space = video_frame::yuv601;
502 video_frame_template.value_range = video_frame::u8_full;
503 video_frame_template.chroma_location = video_frame::center;
504 }
505 // Stereo layout
506 video_frame_template.stereo_layout = parameters::layout_mono;
507 video_frame_template.stereo_layout_swap = false;
508 std::string val;
509 /* Determine the stereo layout from the resolution.*/
510 if (video_frame_template.raw_width / 2 > video_frame_template.raw_height)
511 {
512 video_frame_template.stereo_layout = parameters::layout_left_right;
513 }
514 else if (video_frame_template.raw_height > video_frame_template.raw_width)
515 {
516 video_frame_template.stereo_layout = parameters::layout_top_bottom;
517 }
518 /* Gather hints from the filename extension */
519 std::string extension = get_extension(_url);
520 if (extension == "mpo")
521 {
522 /* MPO files are alternating-left-right. */
523 video_frame_template.stereo_layout = parameters::layout_alternating;
524 }
525 else if (extension == "jps" || extension == "pns")
526 {
527 /* JPS and PNS are side-by-side in right-left mode */
528 video_frame_template.stereo_layout = parameters::layout_left_right;
529 video_frame_template.stereo_layout_swap = true;
530 }
531 /* Determine the input mode by looking at the file name.
532 * This should be compatible to these conventions:
533 * http://www.tru3d.com/technology/3D_Media_Formats_Software.php?file=TriDef%20Supported%203D%20Formats */
534 std::string marker = _url.substr(0, _url.find_last_of('.'));
535 size_t last_dash = marker.find_last_of('-');
536 if (last_dash != std::string::npos)
537 {
538 marker = marker.substr(last_dash + 1);
539 }
540 else
541 {
542 marker = "";
543 }
544 for (size_t i = 0; i < marker.length(); i++)
545 {
546 marker[i] = std::tolower(marker[i]);
547 }
548 if (marker == "lr")
549 {
550 video_frame_template.stereo_layout = parameters::layout_left_right;
551 video_frame_template.stereo_layout_swap = false;
552 }
553 else if (marker == "rl")
554 {
555 video_frame_template.stereo_layout = parameters::layout_left_right;
556 video_frame_template.stereo_layout_swap = true;
557 }
558 else if (marker == "lrh" || marker == "lrq")
559 {
560 video_frame_template.stereo_layout = parameters::layout_left_right_half;
561 video_frame_template.stereo_layout_swap = false;
562 }
563 else if (marker == "rlh" || marker == "rlq")
564 {
565 video_frame_template.stereo_layout = parameters::layout_left_right_half;
566 video_frame_template.stereo_layout_swap = true;
567 }
568 else if (marker == "tb" || marker == "ab")
569 {
570 video_frame_template.stereo_layout = parameters::layout_top_bottom;
571 video_frame_template.stereo_layout_swap = false;
572 }
573 else if (marker == "bt" || marker == "ba")
574 {
575 video_frame_template.stereo_layout = parameters::layout_top_bottom;
576 video_frame_template.stereo_layout_swap = true;
577 }
578 else if (marker == "tbh" || marker == "abq")
579 {
580 video_frame_template.stereo_layout = parameters::layout_top_bottom_half;
581 video_frame_template.stereo_layout_swap = false;
582 }
583 else if (marker == "bth" || marker == "baq")
584 {
585 video_frame_template.stereo_layout = parameters::layout_top_bottom_half;
586 video_frame_template.stereo_layout_swap = true;
587 }
588 else if (marker == "eo")
589 {
590 video_frame_template.stereo_layout = parameters::layout_even_odd_rows;
591 video_frame_template.stereo_layout_swap = false;
592 // all image lines are given in this case, and there should be no interpolation [TODO]
593 }
594 else if (marker == "oe")
595 {
596 video_frame_template.stereo_layout = parameters::layout_even_odd_rows;
597 video_frame_template.stereo_layout_swap = true;
598 // all image lines are given in this case, and there should be no interpolation [TODO]
599 }
600 else if (marker == "eoq" || marker == "3dir")
601 {
602 video_frame_template.stereo_layout = parameters::layout_even_odd_rows;
603 video_frame_template.stereo_layout_swap = false;
604 }
605 else if (marker == "oeq" || marker == "3di")
606 {
607 video_frame_template.stereo_layout = parameters::layout_even_odd_rows;
608 video_frame_template.stereo_layout_swap = true;
609 }
610 else if (marker == "2d")
611 {
612 video_frame_template.stereo_layout = parameters::layout_mono;
613 video_frame_template.stereo_layout_swap = false;
614 }
615 /* Check some tags defined at this link: http://www.3dtv.at/Knowhow/StereoWmvSpec_en.aspx
616 * This is necessary to make the example movies provided by 3dtv.at work out of the box. */
617 val = tag_value("StereoscopicLayout");
618 if (val == "SideBySideRF" || val == "SideBySideLF")
619 {
620 video_frame_template.stereo_layout_swap = (val == "SideBySideRF");
621 val = tag_value("StereoscopicHalfWidth");
622 video_frame_template.stereo_layout = (val == "1" ? parameters::layout_left_right_half : parameters::layout_left_right);
623
624 }
625 else if (val == "OverUnderRT" || val == "OverUnderLT")
626 {
627 video_frame_template.stereo_layout_swap = (val == "OverUnderRT");
628 val = tag_value("StereoscopicHalfHeight");
629 video_frame_template.stereo_layout = (val == "1" ? parameters::layout_top_bottom_half : parameters::layout_top_bottom);
630 }
631 /* Check the Matroska StereoMode metadata, which is translated by FFmpeg to a "stereo_mode" tag.
632 * This tag is per-track, not per-file!
633 * This tag is the most reliable source of information about the stereo layout and should be used
634 * by everyone. Unfortunately, we still have to look at the resolution to guess whether we have
635 * a reduced resolution (*_half) stereo layout. */
636 val = "";
637 AVDictionaryEntry *tag = NULL;
638 while ((tag = av_dict_get(video_stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
639 {
640 if (std::string(tag->key) == "stereo_mode")
641 {
642 val = tag->value;
643 break;
644 }
645 }
646 if (val == "mono")
647 {
648 video_frame_template.stereo_layout = parameters::layout_mono;
649 video_frame_template.stereo_layout_swap = false;
650 }
651 else if (val == "left_right" || val == "right_left")
652 {
653 if (video_frame_template.raw_width / 2 > video_frame_template.raw_height)
654 {
655 video_frame_template.stereo_layout = parameters::layout_left_right;
656 }
657 else
658 {
659 video_frame_template.stereo_layout = parameters::layout_left_right_half;
660 }
661 video_frame_template.stereo_layout_swap = (val == "right_left");
662 }
663 else if (val == "top_bottom" || val == "bottom_top")
664 {
665 if (video_frame_template.raw_height > video_frame_template.raw_width)
666 {
667 video_frame_template.stereo_layout = parameters::layout_top_bottom;
668 }
669 else
670 {
671 video_frame_template.stereo_layout = parameters::layout_top_bottom_half;
672 }
673 video_frame_template.stereo_layout_swap = (val == "bottom_top");
674 }
675 else if (val == "row_interleaved_lr" || val == "row_interleaved_rl")
676 {
677 video_frame_template.stereo_layout = parameters::layout_even_odd_rows;
678 video_frame_template.stereo_layout_swap = (val == "row_interleaved_rl");
679 }
680 else if (val == "block_lr" || val == "block_rl")
681 {
682 video_frame_template.stereo_layout = parameters::layout_alternating;
683 video_frame_template.stereo_layout_swap = (val == "block_rl");
684 }
685 else if (!val.empty())
686 {
687 msg::wrn(_("%s video stream %d: Unsupported stereo layout %s."),
688 _url.c_str(), index + 1, str::sanitize(val).c_str());
689 video_frame_template.stereo_layout = parameters::layout_mono;
690 video_frame_template.stereo_layout_swap = false;
691 }
692 /* Sanity checks. If these fail, use safe fallback */
693 if (((video_frame_template.stereo_layout == parameters::layout_left_right
694 || video_frame_template.stereo_layout == parameters::layout_left_right_half)
695 && video_frame_template.raw_width % 2 != 0)
696 || ((video_frame_template.stereo_layout == parameters::layout_top_bottom
697 || video_frame_template.stereo_layout == parameters::layout_top_bottom_half)
698 && video_frame_template.raw_height % 2 != 0)
699 || (video_frame_template.stereo_layout == parameters::layout_even_odd_rows
700 && video_frame_template.raw_height % 2 != 0))
701 {
702 video_frame_template.stereo_layout = parameters::layout_mono;
703 video_frame_template.stereo_layout_swap = false;
704 }
705 /* Set width and height of a single view */
706 video_frame_template.set_view_dimensions();
707 }
708
set_audio_blob_template(int index)709 void media_object::set_audio_blob_template(int index)
710 {
711 AVStream *audio_stream = _ffmpeg->format_ctx->streams[_ffmpeg->audio_streams[index]];
712 AVCodecContext *audio_codec_ctx = _ffmpeg->audio_codec_ctxs[index];
713 audio_blob &audio_blob_template = _ffmpeg->audio_blob_templates[index];
714
715 AVDictionaryEntry *tag = av_dict_get(audio_stream->metadata, "language", NULL, AV_DICT_IGNORE_SUFFIX);
716 if (tag)
717 {
718 audio_blob_template.language = tag->value;
719 }
720 if (audio_codec_ctx->channels < 1
721 || audio_codec_ctx->channels > 8
722 || audio_codec_ctx->channels == 3
723 || audio_codec_ctx->channels == 5)
724 {
725 throw exc(str::asprintf(_("%s audio stream %d: Cannot handle audio with %d channels."),
726 _url.c_str(), index + 1, audio_codec_ctx->channels));
727 }
728 audio_blob_template.channels = audio_codec_ctx->channels;
729 audio_blob_template.rate = audio_codec_ctx->sample_rate;
730 if (audio_codec_ctx->sample_fmt == AV_SAMPLE_FMT_U8
731 || audio_codec_ctx->sample_fmt == AV_SAMPLE_FMT_U8P)
732 {
733 audio_blob_template.sample_format = audio_blob::u8;
734 }
735 else if (audio_codec_ctx->sample_fmt == AV_SAMPLE_FMT_S16
736 || audio_codec_ctx->sample_fmt == AV_SAMPLE_FMT_S16P)
737 {
738 audio_blob_template.sample_format = audio_blob::s16;
739 }
740 else if (audio_codec_ctx->sample_fmt == AV_SAMPLE_FMT_FLT
741 || audio_codec_ctx->sample_fmt == AV_SAMPLE_FMT_FLTP)
742 {
743 audio_blob_template.sample_format = audio_blob::f32;
744 }
745 else if (audio_codec_ctx->sample_fmt == AV_SAMPLE_FMT_DBL
746 || audio_codec_ctx->sample_fmt == AV_SAMPLE_FMT_DBLP)
747 {
748 audio_blob_template.sample_format = audio_blob::d64;
749 }
750 else if ((audio_codec_ctx->sample_fmt == AV_SAMPLE_FMT_S32
751 || audio_codec_ctx->sample_fmt == AV_SAMPLE_FMT_S32P)
752 && sizeof(int32_t) == sizeof(float))
753 {
754 // we need to convert this to AV_SAMPLE_FMT_FLT after decoding
755 audio_blob_template.sample_format = audio_blob::f32;
756 }
757 else
758 {
759 throw exc(str::asprintf(_("%s audio stream %d: Cannot handle audio with sample format %s."),
760 _url.c_str(), index + 1, av_get_sample_fmt_name(audio_codec_ctx->sample_fmt)));
761 }
762 }
763
set_subtitle_box_template(int index)764 void media_object::set_subtitle_box_template(int index)
765 {
766 AVStream *subtitle_stream = _ffmpeg->format_ctx->streams[_ffmpeg->subtitle_streams[index]];
767 //AVCodecContext *subtitle_codec_ctx = _ffmpeg->subtitle_codec_ctxs[index];
768 subtitle_box &subtitle_box_template = _ffmpeg->subtitle_box_templates[index];
769
770 AVDictionaryEntry *tag = av_dict_get(subtitle_stream->metadata, "language", NULL, AV_DICT_IGNORE_SUFFIX);
771 if (tag)
772 {
773 subtitle_box_template.language = tag->value;
774 }
775 }
776
open(const std::string & url,const device_request & dev_request)777 void media_object::open(const std::string &url, const device_request &dev_request)
778 {
779 assert(!_ffmpeg);
780
781 _url = url;
782 _is_device = dev_request.is_device();
783 _ffmpeg = new struct ffmpeg_stuff;
784 _ffmpeg->format_ctx = NULL;
785 _ffmpeg->have_active_audio_stream = false;
786 _ffmpeg->pos = 0;
787 _ffmpeg->reader = new read_thread(_url, _is_device, _ffmpeg);
788 int e;
789
790 /* Set format and parameters for device input */
791 AVInputFormat *iformat = NULL;
792 AVDictionary *iparams = NULL;
793 switch (dev_request.device)
794 {
795 case device_request::firewire:
796 iformat = av_find_input_format("libdc1394");
797 break;
798 case device_request::x11:
799 iformat = av_find_input_format("x11grab");
800 break;
801 case device_request::sys_default:
802 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
803 iformat = av_find_input_format("vfwcap");
804 #elif defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __APPLE__
805 iformat = av_find_input_format("bktr");
806 #else
807 iformat = av_find_input_format("video4linux2");
808 #endif
809 break;
810 case device_request::no_device:
811 /* Force the format for a few file types that are unknown to older
812 * versions of FFmpeg and to Libav. Note: this may be removed in
813 * the future if all relevant FFmpeg/Libav versions recognize these
814 * files automatically.
815 * This fixes the problems for MPO and JPS images listed here:
816 * http://lists.nongnu.org/archive/html/bino-list/2012-03/msg00026.html
817 * This does not fix the PNS problem, because the format is "image2" but
818 * we also need to tell FFmpeg about the codec (PNG), which does not
819 * seem to be possible.
820 */
821 {
822 std::string extension = get_extension(_url);
823 if (extension == "mpo" || extension == "jps")
824 iformat = av_find_input_format("mjpeg");
825 }
826 break;
827 }
828 if (_is_device && !iformat)
829 {
830 throw exc(str::asprintf(_("No support available for %s device."),
831 dev_request.device == device_request::firewire ? _("Firewire")
832 : dev_request.device == device_request::x11 ? _("X11")
833 : _("default")));
834 }
835 if (_is_device && dev_request.width != 0 && dev_request.height != 0)
836 {
837 av_dict_set(&iparams, "video_size", str::asprintf("%dx%d",
838 dev_request.width, dev_request.height).c_str(), 0);
839 }
840 if (_is_device && dev_request.frame_rate_num != 0 && dev_request.frame_rate_den != 0)
841 {
842 av_dict_set(&iparams, "framerate", str::asprintf("%d/%d",
843 dev_request.frame_rate_num, dev_request.frame_rate_den).c_str(), 0);
844 }
845 if (_is_device && dev_request.request_mjpeg)
846 {
847 av_dict_set(&iparams, "input_format", "mjpeg", 0);
848 }
849
850 /* Open the input */
851 _ffmpeg->format_ctx = NULL;
852 if ((e = avformat_open_input(&_ffmpeg->format_ctx, _url.c_str(), iformat, &iparams)) != 0)
853 {
854 av_dict_free(&iparams);
855 throw exc(str::asprintf(_("%s: %s"),
856 _url.c_str(), my_av_strerror(e).c_str()));
857 }
858 av_dict_free(&iparams);
859 if (_is_device)
860 {
861 // For a camera device, do not read ahead multiple packets, to avoid a startup delay.
862 _ffmpeg->format_ctx->max_analyze_duration = 0;
863 }
864 if ((e = avformat_find_stream_info(_ffmpeg->format_ctx, NULL)) < 0)
865 {
866 throw exc(str::asprintf(_("%s: Cannot read stream info: %s"),
867 _url.c_str(), my_av_strerror(e).c_str()));
868 }
869 av_dump_format(_ffmpeg->format_ctx, 0, _url.c_str(), 0);
870
871 /* Metadata */
872 AVDictionaryEntry *tag = NULL;
873 while ((tag = av_dict_get(_ffmpeg->format_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
874 {
875 _tag_names.push_back(tag->key);
876 _tag_values.push_back(tag->value);
877 }
878
879 _ffmpeg->have_active_audio_stream = false;
880 _ffmpeg->pos = std::numeric_limits<int64_t>::min();
881
882 for (unsigned int i = 0; i < _ffmpeg->format_ctx->nb_streams
883 && i < static_cast<unsigned int>(std::numeric_limits<int>::max()); i++)
884 {
885 _ffmpeg->format_ctx->streams[i]->discard = AVDISCARD_ALL; // ignore by default; user must activate streams
886 AVCodecContext *codec_ctx = _ffmpeg->format_ctx->streams[i]->codec;
887 AVCodec *codec = (codec_ctx->codec_id == AV_CODEC_ID_TEXT
888 ? NULL : avcodec_find_decoder(codec_ctx->codec_id));
889 // XXX: Sometimes the reported width and height for a video stream change after avcodec_open(),
890 // but the original values seem to be correct. This seems to happen mostly with 1920x1080 video
891 // that later is reported as 1920x1088, which results in a gray bar displayed at the bottom of
892 // the frame. FFplay is also affected. As a workaround, we keep the original values here and use
893 // them later in set_video_frame_template().
894 int width_before_avcodec_open = codec_ctx->width;
895 int height_before_avcodec_open = codec_ctx->height;
896 if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
897 {
898 // Activate multithreaded decoding. This must be done before opening the codec; see
899 // http://lists.gnu.org/archive/html/bino-list/2011-08/msg00019.html
900 codec_ctx->thread_count = video_decoding_threads();
901 // Set CODEC_FLAG_EMU_EDGE in the same situations in which ffplay sets it.
902 // I don't know what exactly this does, but it is necessary to fix the problem
903 // described in this thread: http://lists.nongnu.org/archive/html/bino-list/2012-02/msg00039.html
904 int lowres = 0;
905 #ifdef FF_API_LOWRES
906 lowres = codec_ctx->lowres;
907 #endif
908 #ifdef FF_API_EMU_EDGE
909 if (lowres || (codec && (codec->capabilities & CODEC_CAP_DR1)))
910 codec_ctx->flags |= CODEC_FLAG_EMU_EDGE;
911 #endif
912 }
913 // Find and open the codec. AV_CODEC_ID_TEXT is a special case: it has no decoder since it is unencoded raw data.
914 if (codec_ctx->codec_id != AV_CODEC_ID_TEXT && (!codec || (e = avcodec_open2(codec_ctx, codec, NULL)) < 0))
915 {
916 msg::wrn(_("%s stream %d: Cannot open %s: %s"), _url.c_str(), i,
917 codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ? _("video codec")
918 : codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO ? _("audio codec")
919 : codec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE ? _("subtitle codec")
920 : _("data"),
921 codec ? my_av_strerror(e).c_str() : _("codec not supported"));
922 }
923 else if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
924 {
925 _ffmpeg->video_streams.push_back(i);
926 int j = _ffmpeg->video_streams.size() - 1;
927 msg::dbg(_url + " stream " + str::from(i) + " is video stream " + str::from(j) + ".");
928 _ffmpeg->video_codec_ctxs.push_back(codec_ctx);
929 if (_ffmpeg->video_codec_ctxs[j]->width < 1 || _ffmpeg->video_codec_ctxs[j]->height < 1)
930 {
931 throw exc(str::asprintf(_("%s video stream %d: Invalid frame size."),
932 _url.c_str(), j + 1));
933 }
934 _ffmpeg->video_codecs.push_back(codec);
935 // Determine frame template.
936 _ffmpeg->video_frame_templates.push_back(video_frame());
937 set_video_frame_template(j, width_before_avcodec_open, height_before_avcodec_open);
938 // Allocate things required for decoding
939 _ffmpeg->video_packets.push_back(AVPacket());
940 av_init_packet(&(_ffmpeg->video_packets[j]));
941 _ffmpeg->video_decode_threads.push_back(video_decode_thread(_url, _ffmpeg, j));
942 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 28, 1)
943 _ffmpeg->video_frames.push_back(avcodec_alloc_frame());
944 _ffmpeg->video_buffered_frames.push_back(avcodec_alloc_frame());
945 #else
946 _ffmpeg->video_frames.push_back(av_frame_alloc());
947 _ffmpeg->video_buffered_frames.push_back(av_frame_alloc());
948 #endif
949 enum AVPixelFormat frame_fmt = (_ffmpeg->video_frame_templates[j].layout == video_frame::bgra32
950 ? AV_PIX_FMT_BGRA : _ffmpeg->video_codec_ctxs[j]->pix_fmt);
951 int frame_bufsize = (avpicture_get_size(frame_fmt,
952 _ffmpeg->video_codec_ctxs[j]->width, _ffmpeg->video_codec_ctxs[j]->height));
953 _ffmpeg->video_buffers.push_back(static_cast<uint8_t *>(av_malloc(frame_bufsize)));
954 avpicture_fill(reinterpret_cast<AVPicture *>(_ffmpeg->video_buffered_frames[j]), _ffmpeg->video_buffers[j],
955 frame_fmt, _ffmpeg->video_codec_ctxs[j]->width, _ffmpeg->video_codec_ctxs[j]->height);
956 if (!_ffmpeg->video_frames[j] || !_ffmpeg->video_buffered_frames[j] || !_ffmpeg->video_buffers[j])
957 {
958 throw exc(HERE + ": " + strerror(ENOMEM));
959 }
960 if (_ffmpeg->video_frame_templates[j].layout == video_frame::bgra32)
961 {
962 // Initialize things needed for software pixel format conversion
963 int sws_bufsize = avpicture_get_size(AV_PIX_FMT_BGRA,
964 _ffmpeg->video_codec_ctxs[j]->width, _ffmpeg->video_codec_ctxs[j]->height);
965 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 28, 1)
966 _ffmpeg->video_sws_frames.push_back(avcodec_alloc_frame());
967 #else
968 _ffmpeg->video_sws_frames.push_back(av_frame_alloc());
969 #endif
970 _ffmpeg->video_sws_buffers.push_back(static_cast<uint8_t *>(av_malloc(sws_bufsize)));
971 if (!_ffmpeg->video_sws_frames[j] || !_ffmpeg->video_sws_buffers[j])
972 {
973 throw exc(HERE + ": " + strerror(ENOMEM));
974 }
975 avpicture_fill(reinterpret_cast<AVPicture *>(_ffmpeg->video_sws_frames[j]), _ffmpeg->video_sws_buffers[j],
976 AV_PIX_FMT_BGRA, _ffmpeg->video_codec_ctxs[j]->width, _ffmpeg->video_codec_ctxs[j]->height);
977 // Call sws_getCachedContext(NULL, ...) instead of sws_getContext(...) just to avoid a deprecation warning.
978 _ffmpeg->video_sws_ctxs.push_back(sws_getCachedContext(NULL,
979 _ffmpeg->video_codec_ctxs[j]->width, _ffmpeg->video_codec_ctxs[j]->height, _ffmpeg->video_codec_ctxs[j]->pix_fmt,
980 _ffmpeg->video_codec_ctxs[j]->width, _ffmpeg->video_codec_ctxs[j]->height, AV_PIX_FMT_BGRA,
981 SWS_POINT, NULL, NULL, NULL));
982 if (!_ffmpeg->video_sws_ctxs[j])
983 {
984 throw exc(str::asprintf(_("%s video stream %d: Cannot initialize conversion context."),
985 _url.c_str(), j + 1));
986 }
987 }
988 else
989 {
990 _ffmpeg->video_sws_frames.push_back(NULL);
991 _ffmpeg->video_sws_buffers.push_back(NULL);
992 _ffmpeg->video_sws_ctxs.push_back(NULL);
993 }
994 _ffmpeg->video_last_timestamps.push_back(std::numeric_limits<int64_t>::min());
995 }
996 else if (codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO)
997 {
998 _ffmpeg->audio_streams.push_back(i);
999 int j = _ffmpeg->audio_streams.size() - 1;
1000 msg::dbg(_url + " stream " + str::from(i) + " is audio stream " + str::from(j) + ".");
1001 _ffmpeg->audio_codec_ctxs.push_back(codec_ctx);
1002 _ffmpeg->audio_codecs.push_back(codec);
1003 _ffmpeg->audio_blob_templates.push_back(audio_blob());
1004 set_audio_blob_template(j);
1005 _ffmpeg->audio_decode_threads.push_back(audio_decode_thread(_url, _ffmpeg, j));
1006 // Manage audio_tmpbufs with av_malloc/av_free, to guarantee correct alignment.
1007 // Not doing this results in hard to debug crashes on some systems.
1008 _ffmpeg->audio_tmpbufs.push_back(static_cast<unsigned char*>(av_malloc(audio_tmpbuf_size)));
1009 if (!_ffmpeg->audio_tmpbufs[j])
1010 {
1011 throw exc(HERE + ": " + strerror(ENOMEM));
1012 }
1013 _ffmpeg->audio_blobs.push_back(blob());
1014 _ffmpeg->audio_buffers.push_back(std::vector<unsigned char>());
1015 _ffmpeg->audio_last_timestamps.push_back(std::numeric_limits<int64_t>::min());
1016 }
1017 else if (codec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE)
1018 {
1019 _ffmpeg->subtitle_streams.push_back(i);
1020 int j = _ffmpeg->subtitle_streams.size() - 1;
1021 msg::dbg(_url + " stream " + str::from(i) + " is subtitle stream " + str::from(j) + ".");
1022 _ffmpeg->subtitle_codec_ctxs.push_back(codec_ctx);
1023 // AV_CODEC_ID_TEXT does not have any decoder; it is just UTF-8 text in the packet data.
1024 _ffmpeg->subtitle_codecs.push_back(
1025 _ffmpeg->subtitle_codec_ctxs[j]->codec_id == AV_CODEC_ID_TEXT ? NULL : codec);
1026 _ffmpeg->subtitle_box_templates.push_back(subtitle_box());
1027 set_subtitle_box_template(j);
1028 _ffmpeg->subtitle_decode_threads.push_back(subtitle_decode_thread(_url, _ffmpeg, j));
1029 _ffmpeg->subtitle_box_buffers.push_back(std::deque<subtitle_box>());
1030 _ffmpeg->subtitle_last_timestamps.push_back(std::numeric_limits<int64_t>::min());
1031 }
1032 else
1033 {
1034 msg::dbg(_url + " stream " + str::from(i) + " contains neither video nor audio nor subtitles.");
1035 }
1036 }
1037 _ffmpeg->video_packet_queues.resize(video_streams());
1038 _ffmpeg->audio_packet_queues.resize(audio_streams());
1039 _ffmpeg->subtitle_packet_queues.resize(subtitle_streams());
1040 _ffmpeg->video_packet_queue_mutexes.resize(video_streams());
1041 _ffmpeg->audio_packet_queue_mutexes.resize(audio_streams());
1042 _ffmpeg->subtitle_packet_queue_mutexes.resize(subtitle_streams());
1043
1044 msg::inf(_url + ":");
1045 for (int i = 0; i < video_streams(); i++)
1046 {
1047 msg::inf(4, _("Video stream %d: %s / %s, %g seconds"), i,
1048 video_frame_template(i).format_info().c_str(),
1049 video_frame_template(i).format_name().c_str(),
1050 video_duration(i) / 1e6f);
1051 msg::inf(8, _("Using up to %d threads for decoding."),
1052 _ffmpeg->video_codec_ctxs.at(i)->thread_count);
1053 }
1054 for (int i = 0; i < audio_streams(); i++)
1055 {
1056 msg::inf(4, _("Audio stream %d: %s / %s, %g seconds"), i,
1057 audio_blob_template(i).format_info().c_str(),
1058 audio_blob_template(i).format_name().c_str(),
1059 audio_duration(i) / 1e6f);
1060 }
1061 for (int i = 0; i < subtitle_streams(); i++)
1062 {
1063 msg::inf(4, _("Subtitle stream %d: %s / %s, %g seconds"), i,
1064 subtitle_box_template(i).format_info().c_str(),
1065 subtitle_box_template(i).format_name().c_str(),
1066 subtitle_duration(i) / 1e6f);
1067 }
1068 if (video_streams() == 0 && audio_streams() == 0 && subtitle_streams() == 0)
1069 {
1070 msg::inf(4, _("No usable streams."));
1071 }
1072 }
1073
url() const1074 const std::string &media_object::url() const
1075 {
1076 return _url;
1077 }
1078
tags() const1079 size_t media_object::tags() const
1080 {
1081 return _tag_names.size();
1082 }
1083
tag_name(size_t i) const1084 const std::string &media_object::tag_name(size_t i) const
1085 {
1086 assert(i < tags());
1087 return _tag_names[i];
1088 }
1089
tag_value(size_t i) const1090 const std::string &media_object::tag_value(size_t i) const
1091 {
1092 assert(i < tags());
1093 return _tag_values[i];
1094 }
1095
tag_value(const std::string & tag_name) const1096 const std::string &media_object::tag_value(const std::string &tag_name) const
1097 {
1098 static std::string empty;
1099 for (size_t i = 0; i < _tag_names.size(); i++)
1100 {
1101 if (std::string(tag_name) == _tag_names[i])
1102 {
1103 return _tag_values[i];
1104 }
1105 }
1106 return empty;
1107 }
1108
video_streams() const1109 int media_object::video_streams() const
1110 {
1111 return _ffmpeg->video_streams.size();
1112 }
1113
audio_streams() const1114 int media_object::audio_streams() const
1115 {
1116 return _ffmpeg->audio_streams.size();
1117 }
1118
subtitle_streams() const1119 int media_object::subtitle_streams() const
1120 {
1121 return _ffmpeg->subtitle_streams.size();
1122 }
1123
video_stream_set_active(int index,bool active)1124 void media_object::video_stream_set_active(int index, bool active)
1125 {
1126 assert(index >= 0);
1127 assert(index < video_streams());
1128 // Stop decoder threads
1129 for (size_t i = 0; i < _ffmpeg->video_streams.size(); i++)
1130 {
1131 _ffmpeg->video_decode_threads[i].finish();
1132 }
1133 for (size_t i = 0; i < _ffmpeg->audio_streams.size(); i++)
1134 {
1135 _ffmpeg->audio_decode_threads[i].finish();
1136 }
1137 for (size_t i = 0; i < _ffmpeg->subtitle_streams.size(); i++)
1138 {
1139 _ffmpeg->subtitle_decode_threads[i].finish();
1140 }
1141 // Stop reading packets
1142 _ffmpeg->reader->finish();
1143 // Set status
1144 _ffmpeg->format_ctx->streams[_ffmpeg->video_streams.at(index)]->discard =
1145 (active ? AVDISCARD_DEFAULT : AVDISCARD_ALL);
1146 // Restart reader
1147 _ffmpeg->reader->start();
1148 }
1149
audio_stream_set_active(int index,bool active)1150 void media_object::audio_stream_set_active(int index, bool active)
1151 {
1152 assert(index >= 0);
1153 assert(index < audio_streams());
1154 // Stop decoder threads
1155 for (size_t i = 0; i < _ffmpeg->video_streams.size(); i++)
1156 {
1157 _ffmpeg->video_decode_threads[i].finish();
1158 }
1159 for (size_t i = 0; i < _ffmpeg->audio_streams.size(); i++)
1160 {
1161 _ffmpeg->audio_decode_threads[i].finish();
1162 }
1163 for (size_t i = 0; i < _ffmpeg->subtitle_streams.size(); i++)
1164 {
1165 _ffmpeg->subtitle_decode_threads[i].finish();
1166 }
1167 // Stop reading packets
1168 _ffmpeg->reader->finish();
1169 // Set status
1170 _ffmpeg->format_ctx->streams[_ffmpeg->audio_streams.at(index)]->discard =
1171 (active ? AVDISCARD_DEFAULT : AVDISCARD_ALL);
1172 _ffmpeg->have_active_audio_stream = false;
1173 for (int i = 0; i < audio_streams(); i++)
1174 {
1175 if (_ffmpeg->format_ctx->streams[_ffmpeg->audio_streams.at(index)]->discard == AVDISCARD_DEFAULT)
1176 {
1177 _ffmpeg->have_active_audio_stream = true;
1178 break;
1179 }
1180 }
1181 // Restart reader
1182 _ffmpeg->reader->start();
1183 }
1184
subtitle_stream_set_active(int index,bool active)1185 void media_object::subtitle_stream_set_active(int index, bool active)
1186 {
1187 assert(index >= 0);
1188 assert(index < subtitle_streams());
1189 // Stop decoder threads
1190 for (size_t i = 0; i < _ffmpeg->video_streams.size(); i++)
1191 {
1192 _ffmpeg->video_decode_threads[i].finish();
1193 }
1194 for (size_t i = 0; i < _ffmpeg->audio_streams.size(); i++)
1195 {
1196 _ffmpeg->audio_decode_threads[i].finish();
1197 }
1198 for (size_t i = 0; i < _ffmpeg->subtitle_streams.size(); i++)
1199 {
1200 _ffmpeg->subtitle_decode_threads[i].finish();
1201 }
1202 // Stop reading packets
1203 _ffmpeg->reader->finish();
1204 // Set status
1205 _ffmpeg->format_ctx->streams[_ffmpeg->subtitle_streams.at(index)]->discard =
1206 (active ? AVDISCARD_DEFAULT : AVDISCARD_ALL);
1207 // Restart reader
1208 _ffmpeg->reader->start();
1209 }
1210
video_frame_template(int video_stream) const1211 const video_frame &media_object::video_frame_template(int video_stream) const
1212 {
1213 assert(video_stream >= 0);
1214 assert(video_stream < video_streams());
1215 return _ffmpeg->video_frame_templates.at(video_stream);
1216 }
1217
video_frame_rate_numerator(int index) const1218 int media_object::video_frame_rate_numerator(int index) const
1219 {
1220 assert(index >= 0);
1221 assert(index < video_streams());
1222 int n = _ffmpeg->format_ctx->streams[_ffmpeg->video_streams.at(index)]->avg_frame_rate.num;
1223 if (n <= 0)
1224 n = 1;
1225 return n;
1226 }
1227
video_frame_rate_denominator(int index) const1228 int media_object::video_frame_rate_denominator(int index) const
1229 {
1230 assert(index >= 0);
1231 assert(index < video_streams());
1232 int d = _ffmpeg->format_ctx->streams[_ffmpeg->video_streams.at(index)]->avg_frame_rate.den;
1233 if (d <= 0)
1234 d = 1;
1235 return d;
1236 }
1237
video_duration(int index) const1238 int64_t media_object::video_duration(int index) const
1239 {
1240 assert(index >= 0);
1241 assert(index < video_streams());
1242 return stream_duration(
1243 _ffmpeg->format_ctx->streams[_ffmpeg->video_streams.at(index)],
1244 _ffmpeg->format_ctx);
1245 }
1246
audio_blob_template(int audio_stream) const1247 const audio_blob &media_object::audio_blob_template(int audio_stream) const
1248 {
1249 assert(audio_stream >= 0);
1250 assert(audio_stream < audio_streams());
1251 return _ffmpeg->audio_blob_templates.at(audio_stream);
1252 }
1253
audio_duration(int index) const1254 int64_t media_object::audio_duration(int index) const
1255 {
1256 assert(index >= 0);
1257 assert(index < audio_streams());
1258 return stream_duration(
1259 _ffmpeg->format_ctx->streams[_ffmpeg->audio_streams.at(index)],
1260 _ffmpeg->format_ctx);
1261 }
1262
subtitle_box_template(int subtitle_stream) const1263 const subtitle_box &media_object::subtitle_box_template(int subtitle_stream) const
1264 {
1265 assert(subtitle_stream >= 0);
1266 assert(subtitle_stream < subtitle_streams());
1267 return _ffmpeg->subtitle_box_templates.at(subtitle_stream);
1268 }
1269
subtitle_duration(int index) const1270 int64_t media_object::subtitle_duration(int index) const
1271 {
1272 assert(index >= 0);
1273 assert(index < subtitle_streams());
1274 return stream_duration(
1275 _ffmpeg->format_ctx->streams[_ffmpeg->subtitle_streams.at(index)],
1276 _ffmpeg->format_ctx);
1277 }
1278
read_thread(const std::string & url,bool is_device,struct ffmpeg_stuff * ffmpeg)1279 read_thread::read_thread(const std::string &url, bool is_device, struct ffmpeg_stuff *ffmpeg) :
1280 _url(url), _is_device(is_device), _ffmpeg(ffmpeg), _eof(false)
1281 {
1282 }
1283
run()1284 void read_thread::run()
1285 {
1286 while (!_eof)
1287 {
1288 // We need another packet if the number of queued packets for an active stream is below a threshold.
1289 // For files, we often want to read ahead to avoid i/o waits. For devices, we do not want to read
1290 // ahead to avoid latency.
1291 const size_t video_stream_low_threshold = (_is_device ? 1 : 2); // Often, 1 packet results in one video frame
1292 const size_t audio_stream_low_threshold = (_is_device ? 1 : 5); // Often, 3-4 packets are needed for one buffer fill
1293 const size_t subtitle_stream_low_threshold = (_is_device ? 1 : 1); // Just a guess
1294 bool need_another_packet = false;
1295 for (size_t i = 0; !need_another_packet && i < _ffmpeg->video_streams.size(); i++)
1296 {
1297 if (_ffmpeg->format_ctx->streams[_ffmpeg->video_streams[i]]->discard == AVDISCARD_DEFAULT)
1298 {
1299 _ffmpeg->video_packet_queue_mutexes[i].lock();
1300 need_another_packet = _ffmpeg->video_packet_queues[i].size() < video_stream_low_threshold;
1301 _ffmpeg->video_packet_queue_mutexes[i].unlock();
1302 }
1303 }
1304 for (size_t i = 0; !need_another_packet && i < _ffmpeg->audio_streams.size(); i++)
1305 {
1306 if (_ffmpeg->format_ctx->streams[_ffmpeg->audio_streams[i]]->discard == AVDISCARD_DEFAULT)
1307 {
1308 _ffmpeg->audio_packet_queue_mutexes[i].lock();
1309 need_another_packet = _ffmpeg->audio_packet_queues[i].size() < audio_stream_low_threshold;
1310 _ffmpeg->audio_packet_queue_mutexes[i].unlock();
1311 }
1312 }
1313 for (size_t i = 0; !need_another_packet && i < _ffmpeg->subtitle_streams.size(); i++)
1314 {
1315 if (_ffmpeg->format_ctx->streams[_ffmpeg->subtitle_streams[i]]->discard == AVDISCARD_DEFAULT)
1316 {
1317 _ffmpeg->subtitle_packet_queue_mutexes[i].lock();
1318 need_another_packet = _ffmpeg->subtitle_packet_queues[i].size() < subtitle_stream_low_threshold;
1319 _ffmpeg->subtitle_packet_queue_mutexes[i].unlock();
1320 }
1321 }
1322 if (!need_another_packet)
1323 {
1324 msg::dbg(_url + ": No need to read more packets.");
1325 break;
1326 }
1327 // Read a packet.
1328 msg::dbg(_url + ": Reading a packet.");
1329 AVPacket packet;
1330 int e = av_read_frame(_ffmpeg->format_ctx, &packet);
1331 if (e < 0)
1332 {
1333 if (e == AVERROR_EOF)
1334 {
1335 msg::dbg(_url + ": EOF.");
1336 _eof = true;
1337 return;
1338 }
1339 else
1340 {
1341 throw exc(str::asprintf(_("%s: %s"), _url.c_str(), my_av_strerror(e).c_str()));
1342 }
1343 }
1344 // Put the packet in the right queue.
1345 bool packet_queued = false;
1346 for (size_t i = 0; i < _ffmpeg->video_streams.size() && !packet_queued; i++)
1347 {
1348 if (packet.stream_index == _ffmpeg->video_streams[i])
1349 {
1350 // We do not check for missing timestamps here, as we do with audio
1351 // packets, for the following reasons:
1352 // 1. The video decoder might fill in a timestamp for us
1353 // 2. We cannot drop video packets anyway, because of their
1354 // interdependencies. We would mess up decoding.
1355 if (av_dup_packet(&packet) < 0)
1356 {
1357 throw exc(str::asprintf(_("%s: Cannot duplicate packet."), _url.c_str()));
1358 }
1359 _ffmpeg->video_packet_queue_mutexes[i].lock();
1360 _ffmpeg->video_packet_queues[i].push_back(packet);
1361 _ffmpeg->video_packet_queue_mutexes[i].unlock();
1362 packet_queued = true;
1363 msg::dbg(_url + ": "
1364 + str::from(_ffmpeg->video_packet_queues[i].size())
1365 + " packets queued in video stream " + str::from(i) + ".");
1366 }
1367 }
1368 for (size_t i = 0; i < _ffmpeg->audio_streams.size() && !packet_queued; i++)
1369 {
1370 if (packet.stream_index == _ffmpeg->audio_streams[i])
1371 {
1372 _ffmpeg->audio_packet_queue_mutexes[i].lock();
1373 if (_ffmpeg->audio_packet_queues[i].empty()
1374 && _ffmpeg->audio_last_timestamps[i] == std::numeric_limits<int64_t>::min()
1375 && packet.dts == static_cast<int64_t>(AV_NOPTS_VALUE))
1376 {
1377 // We have no packet in the queue and no last timestamp, probably
1378 // because we just seeked. We *need* a packet with a timestamp.
1379 msg::dbg(_url + ": audio stream " + str::from(i)
1380 + ": dropping packet because it has no timestamp");
1381 }
1382 else
1383 {
1384 if (av_dup_packet(&packet) < 0)
1385 {
1386 _ffmpeg->audio_packet_queue_mutexes[i].unlock();
1387 throw exc(str::asprintf(_("%s: Cannot duplicate packet."), _url.c_str()));
1388 }
1389 _ffmpeg->audio_packet_queues[i].push_back(packet);
1390 packet_queued = true;
1391 msg::dbg(_url + ": "
1392 + str::from(_ffmpeg->audio_packet_queues[i].size())
1393 + " packets queued in audio stream " + str::from(i) + ".");
1394 }
1395 _ffmpeg->audio_packet_queue_mutexes[i].unlock();
1396 }
1397 }
1398 for (size_t i = 0; i < _ffmpeg->subtitle_streams.size() && !packet_queued; i++)
1399 {
1400 if (packet.stream_index == _ffmpeg->subtitle_streams[i])
1401 {
1402 _ffmpeg->subtitle_packet_queue_mutexes[i].lock();
1403 if (_ffmpeg->subtitle_packet_queues[i].empty()
1404 && _ffmpeg->subtitle_last_timestamps[i] == std::numeric_limits<int64_t>::min()
1405 && packet.dts == static_cast<int64_t>(AV_NOPTS_VALUE))
1406 {
1407 // We have no packet in the queue and no last timestamp, probably
1408 // because we just seeked. We want a packet with a timestamp.
1409 msg::dbg(_url + ": subtitle stream " + str::from(i)
1410 + ": dropping packet because it has no timestamp");
1411 }
1412 else
1413 {
1414 if (av_dup_packet(&packet) < 0)
1415 {
1416 _ffmpeg->subtitle_packet_queue_mutexes[i].unlock();
1417 throw exc(str::asprintf(_("%s: Cannot duplicate packet."), _url.c_str()));
1418 }
1419 _ffmpeg->subtitle_packet_queues[i].push_back(packet);
1420 packet_queued = true;
1421 msg::dbg(_url + ": "
1422 + str::from(_ffmpeg->subtitle_packet_queues[i].size())
1423 + " packets queued in subtitle stream " + str::from(i) + ".");
1424 }
1425 _ffmpeg->subtitle_packet_queue_mutexes[i].unlock();
1426 }
1427 }
1428 if (!packet_queued)
1429 {
1430 av_free_packet(&packet);
1431 }
1432 }
1433 }
1434
reset()1435 void read_thread::reset()
1436 {
1437 exception() = exc();
1438 _eof = false;
1439 }
1440
video_decode_thread(const std::string & url,struct ffmpeg_stuff * ffmpeg,int video_stream)1441 video_decode_thread::video_decode_thread(const std::string &url, struct ffmpeg_stuff *ffmpeg, int video_stream) :
1442 _url(url), _ffmpeg(ffmpeg), _video_stream(video_stream), _frame(), _raw_frames(1)
1443 {
1444 }
1445
handle_timestamp(int64_t timestamp)1446 int64_t video_decode_thread::handle_timestamp(int64_t timestamp)
1447 {
1448 int64_t ts = timestamp_helper(_ffmpeg->video_last_timestamps[_video_stream], timestamp);
1449 if (!_ffmpeg->have_active_audio_stream || _ffmpeg->pos == std::numeric_limits<int64_t>::min())
1450 {
1451 _ffmpeg->pos = ts;
1452 }
1453 return ts;
1454 }
1455
run()1456 void video_decode_thread::run()
1457 {
1458 _frame = _ffmpeg->video_frame_templates[_video_stream];
1459 for (int raw_frame = 0; raw_frame < _raw_frames; raw_frame++)
1460 {
1461 int frame_finished = 0;
1462 read_frame:
1463 do
1464 {
1465 bool empty;
1466 do
1467 {
1468 _ffmpeg->video_packet_queue_mutexes[_video_stream].lock();
1469 empty = _ffmpeg->video_packet_queues[_video_stream].empty();
1470 _ffmpeg->video_packet_queue_mutexes[_video_stream].unlock();
1471 if (empty)
1472 {
1473 if (_ffmpeg->reader->eof())
1474 {
1475 if (raw_frame == 1)
1476 {
1477 _frame.data[1][0] = _frame.data[0][0];
1478 _frame.data[1][1] = _frame.data[0][1];
1479 _frame.data[1][2] = _frame.data[0][2];
1480 _frame.line_size[1][0] = _frame.line_size[0][0];
1481 _frame.line_size[1][1] = _frame.line_size[0][1];
1482 _frame.line_size[1][2] = _frame.line_size[0][2];
1483 }
1484 else
1485 {
1486 _frame = video_frame();
1487 }
1488 return;
1489 }
1490 msg::dbg(_url + ": video stream " + str::from(_video_stream) + ": need to wait for packets...");
1491 _ffmpeg->reader->start();
1492 _ffmpeg->reader->finish();
1493 }
1494 }
1495 while (empty);
1496 av_free_packet(&(_ffmpeg->video_packets[_video_stream]));
1497 _ffmpeg->video_packet_queue_mutexes[_video_stream].lock();
1498 _ffmpeg->video_packets[_video_stream] = _ffmpeg->video_packet_queues[_video_stream].front();
1499 _ffmpeg->video_packet_queues[_video_stream].pop_front();
1500 _ffmpeg->video_packet_queue_mutexes[_video_stream].unlock();
1501 _ffmpeg->reader->start(); // Refill the packet queue
1502 avcodec_decode_video2(_ffmpeg->video_codec_ctxs[_video_stream],
1503 _ffmpeg->video_frames[_video_stream], &frame_finished,
1504 &(_ffmpeg->video_packets[_video_stream]));
1505 }
1506 while (!frame_finished);
1507 if (_ffmpeg->video_frames[_video_stream]->width != _ffmpeg->video_frame_templates[_video_stream].raw_width
1508 || _ffmpeg->video_frames[_video_stream]->height != _ffmpeg->video_frame_templates[_video_stream].raw_height)
1509 {
1510 msg::wrn(_("%s video stream %d: Dropping %dx%d frame"), _url.c_str(), _video_stream + 1,
1511 _ffmpeg->video_frames[_video_stream]->width, _ffmpeg->video_frames[_video_stream]->height);
1512 goto read_frame;
1513 }
1514 if (_frame.layout == video_frame::bgra32)
1515 {
1516 sws_scale(_ffmpeg->video_sws_ctxs[_video_stream],
1517 _ffmpeg->video_frames[_video_stream]->data,
1518 _ffmpeg->video_frames[_video_stream]->linesize,
1519 0, _frame.raw_height,
1520 _ffmpeg->video_sws_frames[_video_stream]->data,
1521 _ffmpeg->video_sws_frames[_video_stream]->linesize);
1522 // TODO: Handle sws_scale errors. How?
1523 _frame.data[raw_frame][0] = _ffmpeg->video_sws_frames[_video_stream]->data[0];
1524 _frame.line_size[raw_frame][0] = _ffmpeg->video_sws_frames[_video_stream]->linesize[0];
1525 }
1526 else
1527 {
1528 const AVFrame *src_frame = _ffmpeg->video_frames[_video_stream];
1529 if (_raw_frames == 2 && raw_frame == 0)
1530 {
1531 // We need to buffer the data because FFmpeg will clubber it when decoding the next frame.
1532 av_picture_copy(reinterpret_cast<AVPicture *>(_ffmpeg->video_buffered_frames[_video_stream]),
1533 reinterpret_cast<AVPicture *>(_ffmpeg->video_frames[_video_stream]),
1534 static_cast<enum AVPixelFormat>(_ffmpeg->video_codec_ctxs[_video_stream]->pix_fmt),
1535 _ffmpeg->video_codec_ctxs[_video_stream]->width,
1536 _ffmpeg->video_codec_ctxs[_video_stream]->height);
1537 src_frame = _ffmpeg->video_buffered_frames[_video_stream];
1538 }
1539 _frame.data[raw_frame][0] = src_frame->data[0];
1540 _frame.data[raw_frame][1] = src_frame->data[1];
1541 _frame.data[raw_frame][2] = src_frame->data[2];
1542 _frame.line_size[raw_frame][0] = src_frame->linesize[0];
1543 _frame.line_size[raw_frame][1] = src_frame->linesize[1];
1544 _frame.line_size[raw_frame][2] = src_frame->linesize[2];
1545 }
1546
1547 if (_ffmpeg->video_packets[_video_stream].dts != static_cast<int64_t>(AV_NOPTS_VALUE))
1548 {
1549 _frame.presentation_time = handle_timestamp(_ffmpeg->video_packets[_video_stream].dts * 1000000
1550 * _ffmpeg->format_ctx->streams[_ffmpeg->video_streams[_video_stream]]->time_base.num
1551 / _ffmpeg->format_ctx->streams[_ffmpeg->video_streams[_video_stream]]->time_base.den);
1552 }
1553 else if (_ffmpeg->video_last_timestamps[_video_stream] != std::numeric_limits<int64_t>::min())
1554 {
1555 msg::dbg(_url + ": video stream " + str::from(_video_stream)
1556 + ": no timestamp available, using a questionable guess");
1557 _frame.presentation_time = _ffmpeg->video_last_timestamps[_video_stream];
1558 }
1559 else
1560 {
1561 msg::dbg(_url + ": video stream " + str::from(_video_stream)
1562 + ": no timestamp available, using a bad guess");
1563 _frame.presentation_time = _ffmpeg->pos;
1564 }
1565 }
1566 }
1567
start_video_frame_read(int video_stream,int raw_frames)1568 void media_object::start_video_frame_read(int video_stream, int raw_frames)
1569 {
1570 assert(video_stream >= 0);
1571 assert(video_stream < video_streams());
1572 assert(raw_frames == 1 || raw_frames == 2);
1573 _ffmpeg->video_decode_threads[video_stream].set_raw_frames(raw_frames);
1574 _ffmpeg->video_decode_threads[video_stream].start();
1575 }
1576
finish_video_frame_read(int video_stream)1577 video_frame media_object::finish_video_frame_read(int video_stream)
1578 {
1579 assert(video_stream >= 0);
1580 assert(video_stream < video_streams());
1581 _ffmpeg->video_decode_threads[video_stream].finish();
1582 return _ffmpeg->video_decode_threads[video_stream].frame();
1583 }
1584
audio_decode_thread(const std::string & url,struct ffmpeg_stuff * ffmpeg,int audio_stream)1585 audio_decode_thread::audio_decode_thread(const std::string &url, struct ffmpeg_stuff *ffmpeg, int audio_stream) :
1586 _url(url), _ffmpeg(ffmpeg), _audio_stream(audio_stream), _blob()
1587 {
1588 }
1589
handle_timestamp(int64_t timestamp)1590 int64_t audio_decode_thread::handle_timestamp(int64_t timestamp)
1591 {
1592 int64_t ts = timestamp_helper(_ffmpeg->audio_last_timestamps[_audio_stream], timestamp);
1593 _ffmpeg->pos = ts;
1594 return ts;
1595 }
1596
run()1597 void audio_decode_thread::run()
1598 {
1599 size_t size = _ffmpeg->audio_blobs[_audio_stream].size();
1600 void *buffer = _ffmpeg->audio_blobs[_audio_stream].ptr();
1601 int64_t timestamp = std::numeric_limits<int64_t>::min();
1602 size_t i = 0;
1603 while (i < size)
1604 {
1605 if (_ffmpeg->audio_buffers[_audio_stream].size() > 0)
1606 {
1607 // Use available decoded audio data
1608 size_t remaining = std::min(size - i, _ffmpeg->audio_buffers[_audio_stream].size());
1609 memcpy(buffer, &(_ffmpeg->audio_buffers[_audio_stream][0]), remaining);
1610 _ffmpeg->audio_buffers[_audio_stream].erase(
1611 _ffmpeg->audio_buffers[_audio_stream].begin(),
1612 _ffmpeg->audio_buffers[_audio_stream].begin() + remaining);
1613 buffer = reinterpret_cast<unsigned char *>(buffer) + remaining;
1614 i += remaining;
1615 }
1616 if (_ffmpeg->audio_buffers[_audio_stream].size() == 0)
1617 {
1618 // Read more audio data
1619 AVPacket packet, tmppacket;
1620 bool empty;
1621 do
1622 {
1623 _ffmpeg->audio_packet_queue_mutexes[_audio_stream].lock();
1624 empty = _ffmpeg->audio_packet_queues[_audio_stream].empty();
1625 _ffmpeg->audio_packet_queue_mutexes[_audio_stream].unlock();
1626 if (empty)
1627 {
1628 if (_ffmpeg->reader->eof())
1629 {
1630 _blob = audio_blob();
1631 return;
1632 }
1633 msg::dbg(_url + ": audio stream " + str::from(_audio_stream) + ": need to wait for packets...");
1634 _ffmpeg->reader->start();
1635 _ffmpeg->reader->finish();
1636 }
1637 }
1638 while (empty);
1639 _ffmpeg->audio_packet_queue_mutexes[_audio_stream].lock();
1640 packet = _ffmpeg->audio_packet_queues[_audio_stream].front();
1641 _ffmpeg->audio_packet_queues[_audio_stream].pop_front();
1642 _ffmpeg->audio_packet_queue_mutexes[_audio_stream].unlock();
1643 _ffmpeg->reader->start(); // Refill the packet queue
1644 if (timestamp == std::numeric_limits<int64_t>::min() && packet.dts != static_cast<int64_t>(AV_NOPTS_VALUE))
1645 {
1646 timestamp = packet.dts * 1000000
1647 * _ffmpeg->format_ctx->streams[_ffmpeg->audio_streams[_audio_stream]]->time_base.num
1648 / _ffmpeg->format_ctx->streams[_ffmpeg->audio_streams[_audio_stream]]->time_base.den;
1649 }
1650
1651 // Decode audio data
1652 AVFrame audioframe = { { 0 } };
1653 tmppacket = packet;
1654 while (tmppacket.size > 0)
1655 {
1656 int got_frame = 0;
1657 int len = avcodec_decode_audio4(_ffmpeg->audio_codec_ctxs[_audio_stream],
1658 &audioframe, &got_frame, &tmppacket);
1659 if (len < 0)
1660 {
1661 tmppacket.size = 0;
1662 break;
1663 }
1664 tmppacket.data += len;
1665 tmppacket.size -= len;
1666 if (!got_frame)
1667 {
1668 continue;
1669 }
1670 int plane_size;
1671 int tmpbuf_size = av_samples_get_buffer_size(&plane_size,
1672 _ffmpeg->audio_codec_ctxs[_audio_stream]->channels,
1673 audioframe.nb_samples,
1674 _ffmpeg->audio_codec_ctxs[_audio_stream]->sample_fmt, 1);
1675 if (av_sample_fmt_is_planar(_ffmpeg->audio_codec_ctxs[_audio_stream]->sample_fmt)
1676 && _ffmpeg->audio_codec_ctxs[_audio_stream]->channels > 1)
1677 {
1678 int dummy;
1679 int sample_size = av_samples_get_buffer_size(&dummy, 1, 1,
1680 _ffmpeg->audio_codec_ctxs[_audio_stream]->sample_fmt, 1);
1681 uint8_t *out = reinterpret_cast<uint8_t *>(&(_ffmpeg->audio_tmpbufs[_audio_stream][0]));
1682 for (int s = 0; s < audioframe.nb_samples; s++)
1683 {
1684 for (int c = 0; c < _ffmpeg->audio_codec_ctxs[_audio_stream]->channels; c++)
1685 {
1686 std::memcpy(out, audioframe.extended_data[c] + s * sample_size, sample_size);
1687 out += sample_size;
1688 }
1689 }
1690 }
1691 else
1692 {
1693 std::memcpy(&(_ffmpeg->audio_tmpbufs[_audio_stream][0]), audioframe.extended_data[0], plane_size);
1694 }
1695 // Put it in the decoded audio data buffer
1696 if (_ffmpeg->audio_codec_ctxs[_audio_stream]->sample_fmt == AV_SAMPLE_FMT_S32)
1697 {
1698 // we need to convert this to AV_SAMPLE_FMT_FLT
1699 assert(sizeof(int32_t) == sizeof(float));
1700 assert(tmpbuf_size % sizeof(int32_t) == 0);
1701 void *tmpbuf_v = static_cast<void *>(&(_ffmpeg->audio_tmpbufs[_audio_stream][0]));
1702 int32_t *tmpbuf_i32 = static_cast<int32_t *>(tmpbuf_v);
1703 float *tmpbuf_flt = static_cast<float *>(tmpbuf_v);
1704 const float posdiv = +static_cast<float>(std::numeric_limits<int32_t>::max());
1705 const float negdiv = -static_cast<float>(std::numeric_limits<int32_t>::min());
1706 for (size_t j = 0; j < tmpbuf_size / sizeof(int32_t); j++)
1707 {
1708 int32_t sample_i32 = tmpbuf_i32[j];
1709 float sample_flt = sample_i32 / (sample_i32 >= 0 ? posdiv : negdiv);
1710 tmpbuf_flt[j] = sample_flt;
1711 }
1712 }
1713 size_t old_size = _ffmpeg->audio_buffers[_audio_stream].size();
1714 _ffmpeg->audio_buffers[_audio_stream].resize(old_size + tmpbuf_size);
1715 memcpy(&(_ffmpeg->audio_buffers[_audio_stream][old_size]), _ffmpeg->audio_tmpbufs[_audio_stream], tmpbuf_size);
1716 }
1717
1718 av_free_packet(&packet);
1719 }
1720 }
1721 if (timestamp == std::numeric_limits<int64_t>::min())
1722 {
1723 timestamp = _ffmpeg->audio_last_timestamps[_audio_stream];
1724 }
1725 if (timestamp == std::numeric_limits<int64_t>::min())
1726 {
1727 msg::dbg(_url + ": audio stream " + str::from(_audio_stream)
1728 + ": no timestamp available, using a bad guess");
1729 timestamp = _ffmpeg->pos;
1730 }
1731
1732 _blob = _ffmpeg->audio_blob_templates[_audio_stream];
1733 _blob.data = _ffmpeg->audio_blobs[_audio_stream].ptr();
1734 _blob.size = _ffmpeg->audio_blobs[_audio_stream].size();
1735 _blob.presentation_time = handle_timestamp(timestamp);
1736 }
1737
start_audio_blob_read(int audio_stream,size_t size)1738 void media_object::start_audio_blob_read(int audio_stream, size_t size)
1739 {
1740 assert(audio_stream >= 0);
1741 assert(audio_stream < audio_streams());
1742 _ffmpeg->audio_blobs[audio_stream].resize(size);
1743 _ffmpeg->audio_decode_threads[audio_stream].start();
1744 }
1745
finish_audio_blob_read(int audio_stream)1746 audio_blob media_object::finish_audio_blob_read(int audio_stream)
1747 {
1748 assert(audio_stream >= 0);
1749 assert(audio_stream < audio_streams());
1750 _ffmpeg->audio_decode_threads[audio_stream].finish();
1751 return _ffmpeg->audio_decode_threads[audio_stream].blob();
1752 }
1753
subtitle_decode_thread(const std::string & url,struct ffmpeg_stuff * ffmpeg,int subtitle_stream)1754 subtitle_decode_thread::subtitle_decode_thread(const std::string &url, struct ffmpeg_stuff *ffmpeg, int subtitle_stream) :
1755 _url(url), _ffmpeg(ffmpeg), _subtitle_stream(subtitle_stream), _box()
1756 {
1757 }
1758
handle_timestamp(int64_t timestamp)1759 int64_t subtitle_decode_thread::handle_timestamp(int64_t timestamp)
1760 {
1761 int64_t ts = timestamp_helper(_ffmpeg->subtitle_last_timestamps[_subtitle_stream], timestamp);
1762 _ffmpeg->pos = ts;
1763 return ts;
1764 }
1765
run()1766 void subtitle_decode_thread::run()
1767 {
1768 if (_ffmpeg->subtitle_box_buffers[_subtitle_stream].empty())
1769 {
1770 // Read more subtitle data
1771 AVPacket packet, tmppacket;
1772 bool empty;
1773 do
1774 {
1775 _ffmpeg->subtitle_packet_queue_mutexes[_subtitle_stream].lock();
1776 empty = _ffmpeg->subtitle_packet_queues[_subtitle_stream].empty();
1777 _ffmpeg->subtitle_packet_queue_mutexes[_subtitle_stream].unlock();
1778 if (empty)
1779 {
1780 if (_ffmpeg->reader->eof())
1781 {
1782 _box = subtitle_box();
1783 return;
1784 }
1785 msg::dbg(_url + ": subtitle stream " + str::from(_subtitle_stream) + ": need to wait for packets...");
1786 _ffmpeg->reader->start();
1787 _ffmpeg->reader->finish();
1788 }
1789 }
1790 while (empty);
1791 _ffmpeg->subtitle_packet_queue_mutexes[_subtitle_stream].lock();
1792 packet = _ffmpeg->subtitle_packet_queues[_subtitle_stream].front();
1793 _ffmpeg->subtitle_packet_queues[_subtitle_stream].pop_front();
1794 _ffmpeg->subtitle_packet_queue_mutexes[_subtitle_stream].unlock();
1795 _ffmpeg->reader->start(); // Refill the packet queue
1796
1797 // Decode subtitle data
1798 int64_t timestamp = packet.pts * 1000000
1799 * _ffmpeg->format_ctx->streams[_ffmpeg->subtitle_streams[_subtitle_stream]]->time_base.num
1800 / _ffmpeg->format_ctx->streams[_ffmpeg->subtitle_streams[_subtitle_stream]]->time_base.den;
1801 AVSubtitle subtitle;
1802 int got_subtitle;
1803 tmppacket = packet;
1804
1805 // AV_CODEC_ID_TEXT does not have any decoder; it is just UTF-8 text in the packet data.
1806 if (_ffmpeg->subtitle_codec_ctxs[_subtitle_stream]->codec_id == AV_CODEC_ID_TEXT)
1807 {
1808 int64_t duration = packet.convergence_duration * 1000000
1809 * _ffmpeg->format_ctx->streams[_ffmpeg->subtitle_streams[_subtitle_stream]]->time_base.num
1810 / _ffmpeg->format_ctx->streams[_ffmpeg->subtitle_streams[_subtitle_stream]]->time_base.den;
1811
1812 // Put it in the subtitle buffer
1813 subtitle_box box = _ffmpeg->subtitle_box_templates[_subtitle_stream];
1814 box.presentation_start_time = timestamp;
1815 box.presentation_stop_time = timestamp + duration;
1816
1817 box.format = subtitle_box::text;
1818 box.str = reinterpret_cast<const char *>(packet.data);
1819
1820 _ffmpeg->subtitle_box_buffers[_subtitle_stream].push_back(box);
1821
1822 tmppacket.size = 0;
1823 }
1824
1825 while (tmppacket.size > 0)
1826 {
1827 int len = avcodec_decode_subtitle2(_ffmpeg->subtitle_codec_ctxs[_subtitle_stream],
1828 &subtitle, &got_subtitle, &tmppacket);
1829 if (len < 0)
1830 {
1831 tmppacket.size = 0;
1832 break;
1833 }
1834 tmppacket.data += len;
1835 tmppacket.size -= len;
1836 if (!got_subtitle)
1837 {
1838 continue;
1839 }
1840 // Put it in the subtitle buffer
1841 subtitle_box box = _ffmpeg->subtitle_box_templates[_subtitle_stream];
1842 box.presentation_start_time = timestamp + subtitle.start_display_time * 1000;
1843 box.presentation_stop_time = box.presentation_start_time + subtitle.end_display_time * 1000;
1844 for (unsigned int i = 0; i < subtitle.num_rects; i++)
1845 {
1846 AVSubtitleRect *rect = subtitle.rects[i];
1847 switch (rect->type)
1848 {
1849 case SUBTITLE_BITMAP:
1850 box.format = subtitle_box::image;
1851 box.images.push_back(subtitle_box::image_t());
1852 box.images.back().w = rect->w;
1853 box.images.back().h = rect->h;
1854 box.images.back().x = rect->x;
1855 box.images.back().y = rect->y;
1856 box.images.back().palette.resize(4 * rect->nb_colors);
1857 std::memcpy(&(box.images.back().palette[0]), rect->pict.data[1],
1858 box.images.back().palette.size());
1859 box.images.back().linesize = rect->pict.linesize[0];
1860 box.images.back().data.resize(box.images.back().h * box.images.back().linesize);
1861 std::memcpy(&(box.images.back().data[0]), rect->pict.data[0],
1862 box.images.back().data.size() * sizeof(uint8_t));
1863 break;
1864 case SUBTITLE_TEXT:
1865 box.format = subtitle_box::text;
1866 if (!box.str.empty())
1867 {
1868 box.str += '\n';
1869 }
1870 box.str += rect->text;
1871 break;
1872 case SUBTITLE_ASS:
1873 box.format = subtitle_box::ass;
1874 box.style = std::string(reinterpret_cast<const char *>(
1875 _ffmpeg->subtitle_codec_ctxs[_subtitle_stream]->subtitle_header),
1876 _ffmpeg->subtitle_codec_ctxs[_subtitle_stream]->subtitle_header_size);
1877 if (!box.str.empty())
1878 {
1879 box.str += '\n';
1880 }
1881 box.str += rect->ass;
1882 break;
1883 case SUBTITLE_NONE:
1884 // Should never happen, but make sure we have a valid subtitle box anyway.
1885 box.format = subtitle_box::text;
1886 box.str = ' ';
1887 break;
1888 }
1889 }
1890 _ffmpeg->subtitle_box_buffers[_subtitle_stream].push_back(box);
1891 avsubtitle_free(&subtitle);
1892 }
1893
1894 av_free_packet(&packet);
1895 }
1896 if (_ffmpeg->subtitle_box_buffers[_subtitle_stream].empty())
1897 {
1898 _box = subtitle_box();
1899 return;
1900 }
1901 _box = _ffmpeg->subtitle_box_buffers[_subtitle_stream].front();
1902 _ffmpeg->subtitle_box_buffers[_subtitle_stream].pop_front();
1903 }
1904
start_subtitle_box_read(int subtitle_stream)1905 void media_object::start_subtitle_box_read(int subtitle_stream)
1906 {
1907 assert(subtitle_stream >= 0);
1908 assert(subtitle_stream < subtitle_streams());
1909 _ffmpeg->subtitle_decode_threads[subtitle_stream].start();
1910 }
1911
finish_subtitle_box_read(int subtitle_stream)1912 subtitle_box media_object::finish_subtitle_box_read(int subtitle_stream)
1913 {
1914 assert(subtitle_stream >= 0);
1915 assert(subtitle_stream < subtitle_streams());
1916 _ffmpeg->subtitle_decode_threads[subtitle_stream].finish();
1917 return _ffmpeg->subtitle_decode_threads[subtitle_stream].box();
1918 }
1919
tell()1920 int64_t media_object::tell()
1921 {
1922 return _ffmpeg->pos;
1923 }
1924
seek(int64_t dest_pos)1925 void media_object::seek(int64_t dest_pos)
1926 {
1927 msg::dbg(_url + ": Seeking from " + str::from(_ffmpeg->pos / 1e6f) + " to " + str::from(dest_pos / 1e6f) + ".");
1928
1929 // Stop decoder threads
1930 for (size_t i = 0; i < _ffmpeg->video_streams.size(); i++)
1931 {
1932 _ffmpeg->video_decode_threads[i].finish();
1933 }
1934 for (size_t i = 0; i < _ffmpeg->audio_streams.size(); i++)
1935 {
1936 _ffmpeg->audio_decode_threads[i].finish();
1937 }
1938 for (size_t i = 0; i < _ffmpeg->subtitle_streams.size(); i++)
1939 {
1940 _ffmpeg->subtitle_decode_threads[i].finish();
1941 }
1942 // Stop reading packets
1943 _ffmpeg->reader->finish();
1944 // Throw away all queued packets and buffered data
1945 for (size_t i = 0; i < _ffmpeg->video_streams.size(); i++)
1946 {
1947 avcodec_flush_buffers(_ffmpeg->format_ctx->streams[_ffmpeg->video_streams[i]]->codec);
1948 for (size_t j = 0; j < _ffmpeg->video_packet_queues[i].size(); j++)
1949 {
1950 av_free_packet(&_ffmpeg->video_packet_queues[i][j]);
1951 }
1952 _ffmpeg->video_packet_queues[i].clear();
1953 }
1954 for (size_t i = 0; i < _ffmpeg->audio_streams.size(); i++)
1955 {
1956 avcodec_flush_buffers(_ffmpeg->format_ctx->streams[_ffmpeg->audio_streams[i]]->codec);
1957 _ffmpeg->audio_buffers[i].clear();
1958 for (size_t j = 0; j < _ffmpeg->audio_packet_queues[i].size(); j++)
1959 {
1960 av_free_packet(&_ffmpeg->audio_packet_queues[i][j]);
1961 }
1962 _ffmpeg->audio_packet_queues[i].clear();
1963 }
1964 for (size_t i = 0; i < _ffmpeg->subtitle_streams.size(); i++)
1965 {
1966 if (_ffmpeg->format_ctx->streams[_ffmpeg->subtitle_streams[i]]->codec->codec_id != AV_CODEC_ID_TEXT)
1967 {
1968 // AV_CODEC_ID_TEXT has no decoder, so we cannot flush its buffers
1969 avcodec_flush_buffers(_ffmpeg->format_ctx->streams[_ffmpeg->subtitle_streams[i]]->codec);
1970 }
1971 _ffmpeg->subtitle_box_buffers[i].clear();
1972 for (size_t j = 0; j < _ffmpeg->subtitle_packet_queues[i].size(); j++)
1973 {
1974 av_free_packet(&_ffmpeg->subtitle_packet_queues[i][j]);
1975 }
1976 _ffmpeg->subtitle_packet_queues[i].clear();
1977 }
1978 // The next read request must update the position
1979 for (size_t i = 0; i < _ffmpeg->video_streams.size(); i++)
1980 {
1981 _ffmpeg->video_last_timestamps[i] = std::numeric_limits<int64_t>::min();
1982 }
1983 for (size_t i = 0; i < _ffmpeg->audio_streams.size(); i++)
1984 {
1985 _ffmpeg->audio_last_timestamps[i] = std::numeric_limits<int64_t>::min();
1986 }
1987 for (size_t i = 0; i < _ffmpeg->subtitle_streams.size(); i++)
1988 {
1989 _ffmpeg->subtitle_last_timestamps[i] = std::numeric_limits<int64_t>::min();
1990 }
1991 _ffmpeg->pos = std::numeric_limits<int64_t>::min();
1992 // Seek
1993 int e = av_seek_frame(_ffmpeg->format_ctx, -1,
1994 dest_pos * AV_TIME_BASE / 1000000,
1995 dest_pos < _ffmpeg->pos ? AVSEEK_FLAG_BACKWARD : 0);
1996 if (e < 0)
1997 {
1998 msg::err(_("%s: Seeking failed."), _url.c_str());
1999 }
2000 // Restart packet reading
2001 _ffmpeg->reader->reset();
2002 _ffmpeg->reader->start();
2003 }
2004
close()2005 void media_object::close()
2006 {
2007 if (_ffmpeg)
2008 {
2009 try
2010 {
2011 // Stop decoder threads
2012 for (size_t i = 0; i < _ffmpeg->video_decode_threads.size(); i++)
2013 {
2014 _ffmpeg->video_decode_threads[i].finish();
2015 }
2016 for (size_t i = 0; i < _ffmpeg->audio_decode_threads.size(); i++)
2017 {
2018 _ffmpeg->audio_decode_threads[i].finish();
2019 }
2020 for (size_t i = 0; i < _ffmpeg->subtitle_decode_threads.size(); i++)
2021 {
2022 _ffmpeg->subtitle_decode_threads[i].finish();
2023 }
2024 // Stop reading packets
2025 _ffmpeg->reader->finish();
2026 }
2027 catch (...)
2028 {
2029 }
2030 if (_ffmpeg->format_ctx)
2031 {
2032 for (size_t i = 0; i < _ffmpeg->video_frames.size(); i++)
2033 {
2034 av_free(_ffmpeg->video_frames[i]);
2035 }
2036 for (size_t i = 0; i < _ffmpeg->video_buffered_frames.size(); i++)
2037 {
2038 av_free(_ffmpeg->video_buffered_frames[i]);
2039 }
2040 for (size_t i = 0; i < _ffmpeg->video_buffers.size(); i++)
2041 {
2042 av_free(_ffmpeg->video_buffers[i]);
2043 }
2044 for (size_t i = 0; i < _ffmpeg->video_sws_frames.size(); i++)
2045 {
2046 av_free(_ffmpeg->video_sws_frames[i]);
2047 }
2048 for (size_t i = 0; i < _ffmpeg->video_sws_buffers.size(); i++)
2049 {
2050 av_free(_ffmpeg->video_sws_buffers[i]);
2051 }
2052 for (size_t i = 0; i < _ffmpeg->video_codec_ctxs.size(); i++)
2053 {
2054 if (i < _ffmpeg->video_codecs.size() && _ffmpeg->video_codecs[i])
2055 {
2056 avcodec_close(_ffmpeg->video_codec_ctxs[i]);
2057 }
2058 }
2059 for (size_t i = 0; i < _ffmpeg->video_sws_ctxs.size(); i++)
2060 {
2061 sws_freeContext(_ffmpeg->video_sws_ctxs[i]);
2062 }
2063 for (size_t i = 0; i < _ffmpeg->video_packet_queues.size(); i++)
2064 {
2065 if (_ffmpeg->video_packet_queues[i].size() > 0)
2066 {
2067 msg::dbg(_url + ": " + str::from(_ffmpeg->video_packet_queues[i].size())
2068 + " unprocessed packets in video stream " + str::from(i));
2069 }
2070 for (size_t j = 0; j < _ffmpeg->video_packet_queues[i].size(); j++)
2071 {
2072 av_free_packet(&_ffmpeg->video_packet_queues[i][j]);
2073 }
2074 }
2075 for (size_t i = 0; i < _ffmpeg->video_packets.size(); i++)
2076 {
2077 av_free_packet(&(_ffmpeg->video_packets[i]));
2078 }
2079 for (size_t i = 0; i < _ffmpeg->audio_codec_ctxs.size(); i++)
2080 {
2081 if (i < _ffmpeg->audio_codecs.size() && _ffmpeg->audio_codecs[i])
2082 {
2083 avcodec_close(_ffmpeg->audio_codec_ctxs[i]);
2084 }
2085 }
2086 for (size_t i = 0; i < _ffmpeg->audio_packet_queues.size(); i++)
2087 {
2088 if (_ffmpeg->audio_packet_queues[i].size() > 0)
2089 {
2090 msg::dbg(_url + ": " + str::from(_ffmpeg->audio_packet_queues[i].size())
2091 + " unprocessed packets in audio stream " + str::from(i));
2092 }
2093 for (size_t j = 0; j < _ffmpeg->audio_packet_queues[i].size(); j++)
2094 {
2095 av_free_packet(&_ffmpeg->audio_packet_queues[i][j]);
2096 }
2097 }
2098 for (size_t i = 0; i < _ffmpeg->audio_tmpbufs.size(); i++)
2099 {
2100 av_free(_ffmpeg->audio_tmpbufs[i]);
2101 }
2102 for (size_t i = 0; i < _ffmpeg->subtitle_codec_ctxs.size(); i++)
2103 {
2104 if (i < _ffmpeg->subtitle_codecs.size() && _ffmpeg->subtitle_codecs[i])
2105 {
2106 avcodec_close(_ffmpeg->subtitle_codec_ctxs[i]);
2107 }
2108 }
2109 for (size_t i = 0; i < _ffmpeg->subtitle_packet_queues.size(); i++)
2110 {
2111 if (_ffmpeg->subtitle_packet_queues[i].size() > 0)
2112 {
2113 msg::dbg(_url + ": " + str::from(_ffmpeg->subtitle_packet_queues[i].size())
2114 + " unprocessed packets in subtitle stream " + str::from(i));
2115 }
2116 for (size_t j = 0; j < _ffmpeg->subtitle_packet_queues[i].size(); j++)
2117 {
2118 av_free_packet(&_ffmpeg->subtitle_packet_queues[i][j]);
2119 }
2120 }
2121 avformat_close_input(&_ffmpeg->format_ctx);
2122 }
2123 delete _ffmpeg->reader;
2124 delete _ffmpeg;
2125 _ffmpeg = NULL;
2126 }
2127 _url = "";
2128 _is_device = false;
2129 _tag_names.clear();
2130 _tag_values.clear();
2131 }
2132