MythTV  0.26-pre
avformatdecoder.cpp
Go to the documentation of this file.
00001 // C headers
00002 #include <cassert>
00003 #include <unistd.h>
00004 #include <cmath>
00005 #include <stdint.h>
00006 
00007 // C++ headers
00008 #include <algorithm>
00009 #include <iostream>
00010 using namespace std;
00011 
00012 #include <QTextCodec>
00013 
00014 // MythTV headers
00015 #include "mythtvexp.h"
00016 #include "mythconfig.h"
00017 #include "avformatdecoder.h"
00018 #include "privatedecoder.h"
00019 #include "audiooutput.h"
00020 #include "audiooutpututil.h"
00021 #include "ringbuffer.h"
00022 #include "mythplayer.h"
00023 #include "remoteencoder.h"
00024 #include "programinfo.h"
00025 #include "mythcorecontext.h"
00026 #include "mythdbcon.h"
00027 #include "iso639.h"
00028 #include "mpegtables.h"
00029 #include "atscdescriptors.h"
00030 #include "dvbdescriptors.h"
00031 #include "cc608decoder.h"
00032 #include "cc708decoder.h"
00033 #include "teletextdecoder.h"
00034 #include "subtitlereader.h"
00035 #include "interactivetv.h"
00036 #include "videodisplayprofile.h"
00037 #include "mythuihelper.h"
00038 #include "DVD/dvdringbuffer.h"
00039 #include "Bluray/bdringbuffer.h"
00040 
00041 #include "lcddevice.h"
00042 
00043 #include "videoout_quartz.h"  // For VOQ::GetBestSupportedCodec()
00044 
00045 #ifdef USING_VDPAU
00046 #include "videoout_vdpau.h"
00047 extern "C" {
00048 #include "libavcodec/vdpau.h"
00049 }
00050 #endif // USING_VDPAU
00051 
00052 #ifdef USING_DXVA2
00053 #include "videoout_d3d.h"
00054 #endif
00055 
00056 #ifdef USING_GLVAAPI
00057 #include "videoout_openglvaapi.h"
00058 #endif // USING_GLVAAPI
00059 #ifdef USING_VAAPI
00060 #include "vaapicontext.h"
00061 #endif
00062 
00063 extern "C" {
00064 #include "libavutil/avutil.h"
00065 #include "libavutil/log.h"
00066 #include "libavcodec/avcodec.h"
00067 #include "libavcodec/ac3_parser.h"
00068 #include "libavcodec/mpegvideo.h"
00069 #include "libavformat/avformat.h"
00070 #include "libavformat/avio.h"
00071 #include "libavformat/internal.h"
00072 #include "libswscale/swscale.h"
00073 #include "ivtv_myth.h"
00074 }
00075 
00076 #ifdef _MSC_VER
00077 // MSVC isn't C99 compliant...
00078 # ifdef AV_TIME_BASE_Q
00079 #  undef AV_TIME_BASE_Q
00080 # endif
00081 #define AV_TIME_BASE_Q  GetAVTimeBaseQ()
00082 
00083 __inline AVRational GetAVTimeBaseQ()
00084 {
00085     AVRational av = {1, AV_TIME_BASE};
00086     return av;
00087 }
00088 #endif
00089 
00090 #define LOC QString("AFD: ")
00091 
00092 #define MAX_AC3_FRAME_SIZE 6144
00093 
00094 static const float eps = 1E-5;
00095 
00096 static const int max_video_queue_size = 220;
00097 
00098 static int cc608_parity(uint8_t byte);
00099 static int cc608_good_parity(const int *parity_table, uint16_t data);
00100 static void cc608_build_parity_table(int *parity_table);
00101 
00102 static bool silence_ffmpeg_logging = false;
00103 
00104 static QSize get_video_dim(const AVCodecContext &ctx)
00105 {
00106     return QSize(ctx.width >> ctx.lowres, ctx.height >> ctx.lowres);
00107 }
00108 static float get_aspect(const AVCodecContext &ctx)
00109 {
00110     float aspect_ratio = 0.0f;
00111 
00112     if (ctx.sample_aspect_ratio.num && ctx.height)
00113     {
00114         aspect_ratio = av_q2d(ctx.sample_aspect_ratio) * (float) ctx.width;
00115         aspect_ratio /= (float) ctx.height;
00116     }
00117 
00118     if (aspect_ratio <= 0.0f || aspect_ratio > 6.0f)
00119     {
00120         if (ctx.height)
00121             aspect_ratio = (float)ctx.width / (float)ctx.height;
00122         else
00123             aspect_ratio = 4.0f / 3.0f;
00124     }
00125 
00126     return aspect_ratio;
00127 }
00128 
00129 int  get_avf_buffer(struct AVCodecContext *c, AVFrame *pic);
00130 void release_avf_buffer(struct AVCodecContext *c, AVFrame *pic);
00131 int  get_avf_buffer_vdpau(struct AVCodecContext *c, AVFrame *pic);
00132 void release_avf_buffer_vdpau(struct AVCodecContext *c, AVFrame *pic);
00133 void render_slice_vdpau(struct AVCodecContext *s, const AVFrame *src,
00134                         int offset[4], int y, int type, int height);
00135 int  get_avf_buffer_dxva2(struct AVCodecContext *c, AVFrame *pic);
00136 int  get_avf_buffer_vaapi(struct AVCodecContext *c, AVFrame *pic);
00137 
00138 static AVCodec *find_vdpau_decoder(AVCodec *c, enum CodecID id)
00139 {
00140     AVCodec *codec = c;
00141     while (codec)
00142     {
00143         if (codec->id == id && CODEC_IS_VDPAU(codec))
00144             return codec;
00145 
00146         codec = codec->next;
00147     }
00148 
00149     return c;
00150 }
00151 
00152 static void myth_av_log(void *ptr, int level, const char* fmt, va_list vl)
00153 {
00154     if (silence_ffmpeg_logging)
00155         return;
00156 
00157     if (VERBOSE_LEVEL_NONE)
00158         return;
00159 
00160     static QString full_line("");
00161     static const int msg_len = 255;
00162     static QMutex string_lock;
00163     uint64_t   verbose_mask  = VB_GENERAL;
00164     LogLevel_t verbose_level = LOG_DEBUG;
00165 
00166     // determine mythtv debug level from av log level
00167     switch (level)
00168     {
00169         case AV_LOG_PANIC:
00170             verbose_level = LOG_EMERG;
00171             break;
00172         case AV_LOG_FATAL:
00173             verbose_level = LOG_CRIT;
00174             break;
00175         case AV_LOG_ERROR:
00176             verbose_level = LOG_ERR;
00177             verbose_mask |= VB_LIBAV;
00178             break;
00179         case AV_LOG_DEBUG:
00180         case AV_LOG_VERBOSE:
00181         case AV_LOG_INFO:
00182             verbose_level = LOG_DEBUG;
00183             verbose_mask |= VB_LIBAV;
00184             break;
00185         case AV_LOG_WARNING:
00186             verbose_mask |= VB_LIBAV;
00187             break;
00188         default:
00189             return;
00190     }
00191 
00192     if (!VERBOSE_LEVEL_CHECK(verbose_mask, verbose_level))
00193         return;
00194 
00195     string_lock.lock();
00196     if (full_line.isEmpty() && ptr) {
00197         AVClass* avc = *(AVClass**)ptr;
00198         full_line.sprintf("[%s @ %p] ", avc->item_name(ptr), avc);
00199     }
00200 
00201     char str[msg_len+1];
00202     int bytes = vsnprintf(str, msg_len+1, fmt, vl);
00203 
00204     // check for truncated messages and fix them
00205     if (bytes > msg_len)
00206     {
00207         LOG(VB_GENERAL, LOG_WARNING,
00208             QString("Libav log output truncated %1 of %2 bytes written")
00209                 .arg(msg_len).arg(bytes));
00210         str[msg_len-1] = '\n';
00211     }
00212 
00213     full_line += QString(str);
00214     if (full_line.endsWith("\n"))
00215     {
00216         LOG(verbose_mask, verbose_level, full_line.trimmed());
00217         full_line.truncate(0);
00218     }
00219     string_lock.unlock();
00220 }
00221 
00222 static int get_canonical_lang(const char *lang_cstr)
00223 {
00224     if (lang_cstr[0] == '\0' || lang_cstr[1] == '\0')
00225     {
00226         return iso639_str3_to_key("und");
00227     }
00228     else if (lang_cstr[2] == '\0')
00229     {
00230         QString tmp2 = lang_cstr;
00231         QString tmp3 = iso639_str2_to_str3(tmp2);
00232         int lang = iso639_str3_to_key(tmp3);
00233         return iso639_key_to_canonical_key(lang);
00234     }
00235     else
00236     {
00237         int lang = iso639_str3_to_key(lang_cstr);
00238         return iso639_key_to_canonical_key(lang);
00239     }
00240 }
00241 
00242 void AvFormatDecoder::GetDecoders(render_opts &opts)
00243 {
00244     opts.decoders->append("ffmpeg");
00245     (*opts.equiv_decoders)["ffmpeg"].append("nuppel");
00246     (*opts.equiv_decoders)["ffmpeg"].append("dummy");
00247 
00248 #ifdef USING_VDPAU
00249     opts.decoders->append("vdpau");
00250     (*opts.equiv_decoders)["vdpau"].append("dummy");
00251 #endif
00252 #ifdef USING_DXVA2
00253     opts.decoders->append("dxva2");
00254     (*opts.equiv_decoders)["dxva2"].append("dummy");
00255 #endif
00256 
00257 #ifdef USING_VAAPI
00258     opts.decoders->append("vaapi");
00259     (*opts.equiv_decoders)["vaapi"].append("dummy");
00260 #endif
00261 
00262     PrivateDecoder::GetDecoders(opts);
00263 }
00264 
00265 AvFormatDecoder::AvFormatDecoder(MythPlayer *parent,
00266                                  const ProgramInfo &pginfo,
00267                                  PlayerFlags flags)
00268     : DecoderBase(parent, pginfo),
00269       private_dec(NULL),
00270       is_db_ignored(gCoreContext->IsDatabaseIgnored()),
00271       m_h264_parser(new H264Parser()),
00272       ic(NULL),
00273       frame_decoded(0),             decoded_video_frame(NULL),
00274       avfRingBuffer(NULL),          sws_ctx(NULL),
00275       directrendering(false),
00276       no_dts_hack(false),           dorewind(false),
00277       gopset(false),                seen_gop(false),
00278       seq_count(0),
00279       prevgoppos(0),                gotVideoFrame(false),
00280       hasVideo(false),              needDummyVideoFrames(false),
00281       skipaudio(false),             allowedquit(false),
00282       start_code_state(0xffffffff),
00283       lastvpts(0),                  lastapts(0),
00284       lastccptsu(0),
00285       firstvpts(0),                 firstvptsinuse(false),
00286       faulty_pts(0),                faulty_dts(0),
00287       last_pts_for_fault_detection(0),
00288       last_dts_for_fault_detection(0),
00289       pts_detected(false),
00290       reordered_pts_detected(false),
00291       pts_selected(true),
00292       force_dts_timestamps(false),
00293       playerFlags(flags),
00294       video_codec_id(kCodec_NONE),
00295       maxkeyframedist(-1),
00296       // Closed Caption & Teletext decoders
00297       ignore_scte(false),
00298       invert_scte_field(0),
00299       last_scte_field(0),
00300       ccd608(new CC608Decoder(parent->GetCC608Reader())),
00301       ccd708(new CC708Decoder(parent->GetCC708Reader())),
00302       ttd(new TeletextDecoder(parent->GetTeletextReader())),
00303       // Interactive TV
00304       itv(NULL),
00305       // Audio
00306       disable_passthru(false),
00307       m_fps(0.0f),
00308       codec_is_mpeg(false)
00309 {
00310     memset(&readcontext, 0, sizeof(readcontext));
00311     memset(ccX08_in_pmt, 0, sizeof(ccX08_in_pmt));
00312     memset(ccX08_in_tracks, 0, sizeof(ccX08_in_tracks));
00313 
00314     audioSamples = (uint8_t *)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE *
00315                                          sizeof(int32_t));
00316     ccd608->SetIgnoreTimecode(true);
00317 
00318     bool debug = VERBOSE_LEVEL_CHECK(VB_LIBAV, LOG_ANY);
00319     av_log_set_level((debug) ? AV_LOG_DEBUG : AV_LOG_ERROR);
00320     av_log_set_callback(myth_av_log);
00321 
00322     audioIn.sample_size = -32; // force SetupAudioStream to run once
00323     itv = m_parent->GetInteractiveTV();
00324 
00325     cc608_build_parity_table(cc608_parity_table);
00326 
00327     LOG(VB_PLAYBACK, LOG_DEBUG, LOC + QString("PlayerFlags: 0x%1")
00328         .arg(playerFlags, 0, 16));
00329 }
00330 
00331 AvFormatDecoder::~AvFormatDecoder()
00332 {
00333     while (!storedPackets.isEmpty())
00334     {
00335         AVPacket *pkt = storedPackets.takeFirst();
00336         av_free_packet(pkt);
00337         delete pkt;
00338     }
00339 
00340     CloseContext();
00341     delete ccd608;
00342     delete ccd708;
00343     delete ttd;
00344     delete private_dec;
00345     delete m_h264_parser;
00346 
00347     sws_freeContext(sws_ctx);
00348 
00349     av_free(audioSamples);
00350 
00351     if (avfRingBuffer)
00352         delete avfRingBuffer;
00353 
00354     if (LCD *lcd = LCD::Get())
00355     {
00356         lcd->setAudioFormatLEDs(AUDIO_AC3, false);
00357         lcd->setVideoFormatLEDs(VIDEO_MPG, false);
00358         lcd->setVariousLEDs(VARIOUS_HDTV, false);
00359         lcd->setVariousLEDs(VARIOUS_SPDIF, false);
00360         lcd->setSpeakerLEDs(SPEAKER_71, false);    // should clear any and all speaker LEDs
00361     }
00362 }
00363 
00364 void AvFormatDecoder::CloseCodecs()
00365 {
00366     if (ic)
00367     {
00368         for (uint i = 0; i < ic->nb_streams; i++)
00369         {
00370             QMutexLocker locker(avcodeclock);
00371             AVStream *st = ic->streams[i];
00372             if (st->codec->codec)
00373                 avcodec_close(st->codec);
00374         }
00375     }
00376 }
00377 
00378 void AvFormatDecoder::CloseContext()
00379 {
00380     if (ic)
00381     {
00382         CloseCodecs();
00383 
00384         AVInputFormat *fmt = ic->iformat;
00385         ic->iformat->flags |= AVFMT_NOFILE;
00386 
00387         av_free(ic->pb->buffer);
00388         av_free(ic->pb);
00389         avformat_close_input(&ic);
00390         ic = NULL;
00391         fmt->flags &= ~AVFMT_NOFILE;
00392     }
00393 
00394     delete private_dec;
00395     private_dec = NULL;
00396     m_h264_parser->Reset();
00397 }
00398 
00399 static int64_t lsb3full(int64_t lsb, int64_t base_ts, int lsb_bits)
00400 {
00401     int64_t mask = (lsb_bits < 64) ? (1LL<<lsb_bits)-1 : -1LL;
00402     return  ((lsb - base_ts)&mask);
00403 }
00404 
00405 int64_t AvFormatDecoder::NormalizeVideoTimecode(int64_t timecode)
00406 {
00407     int64_t start_pts = 0, pts;
00408 
00409     AVStream *st = NULL;
00410     for (uint i = 0; i < ic->nb_streams; i++)
00411     {
00412         AVStream *st1 = ic->streams[i];
00413         if (st1 && st1->codec->codec_type == AVMEDIA_TYPE_VIDEO)
00414         {
00415             st = st1;
00416             break;
00417         }
00418     }
00419     if (!st)
00420         return false;
00421 
00422     if (ic->start_time != (int64_t)AV_NOPTS_VALUE)
00423         start_pts = av_rescale(ic->start_time,
00424                                st->time_base.den,
00425                                AV_TIME_BASE * (int64_t)st->time_base.num);
00426 
00427     pts = av_rescale(timecode / 1000.0,
00428                      st->time_base.den,
00429                      st->time_base.num);
00430 
00431     // adjust for start time and wrap
00432     pts = lsb3full(pts, start_pts, st->pts_wrap_bits);
00433 
00434     return (int64_t)(av_q2d(st->time_base) * pts * 1000);
00435 }
00436 
00437 int64_t AvFormatDecoder::NormalizeVideoTimecode(AVStream *st,
00438                                                 int64_t timecode)
00439 {
00440     int64_t start_pts = 0, pts;
00441 
00442     if (ic->start_time != (int64_t)AV_NOPTS_VALUE)
00443         start_pts = av_rescale(ic->start_time,
00444                                st->time_base.den,
00445                                AV_TIME_BASE * (int64_t)st->time_base.num);
00446 
00447     pts = av_rescale(timecode / 1000.0,
00448                      st->time_base.den,
00449                      st->time_base.num);
00450 
00451     // adjust for start time and wrap
00452     pts = lsb3full(pts, start_pts, st->pts_wrap_bits);
00453 
00454     return (int64_t)(av_q2d(st->time_base) * pts * 1000);
00455 }
00456 
00457 int AvFormatDecoder::GetNumChapters()
00458 {
00459     if (ic->nb_chapters > 1)
00460         return ic->nb_chapters;
00461     return 0;
00462 }
00463 
00464 void AvFormatDecoder::GetChapterTimes(QList<long long> &times)
00465 {
00466     int total = GetNumChapters();
00467     if (!total)
00468         return;
00469 
00470     for (int i = 0; i < total; i++)
00471     {
00472         int num = ic->chapters[i]->time_base.num;
00473         int den = ic->chapters[i]->time_base.den;
00474         int64_t start = ic->chapters[i]->start;
00475         long double total_secs = (long double)start * (long double)num /
00476                                  (long double)den;
00477         times.push_back((long long)total_secs);
00478     }
00479 }
00480 
00481 int AvFormatDecoder::GetCurrentChapter(long long framesPlayed)
00482 {
00483     if (!GetNumChapters())
00484         return 0;
00485 
00486     for (int i = (ic->nb_chapters - 1); i > -1 ; i--)
00487     {
00488         int num = ic->chapters[i]->time_base.num;
00489         int den = ic->chapters[i]->time_base.den;
00490         int64_t start = ic->chapters[i]->start;
00491         long double total_secs = (long double)start * (long double)num /
00492                                  (long double)den;
00493         long long framenum = (long long)(total_secs * fps);
00494         if (framesPlayed >= framenum)
00495         {
00496             LOG(VB_PLAYBACK, LOG_INFO, LOC +
00497                 QString("GetCurrentChapter(selected chapter %1 framenum %2)")
00498                     .arg(i + 1).arg(framenum));
00499             return i + 1;
00500         }
00501     }
00502     return 0;
00503 }
00504 
00505 long long AvFormatDecoder::GetChapter(int chapter)
00506 {
00507     if (chapter < 1 || chapter > GetNumChapters())
00508         return -1;
00509 
00510     int num = ic->chapters[chapter - 1]->time_base.num;
00511     int den = ic->chapters[chapter - 1]->time_base.den;
00512     int64_t start = ic->chapters[chapter - 1]->start;
00513     long double total_secs = (long double)start * (long double)num /
00514                              (long double)den;
00515     long long framenum = (long long)(total_secs * fps);
00516     LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("GetChapter %1: framenum %2")
00517                                    .arg(chapter).arg(framenum));
00518     return framenum;
00519 }
00520 
00521 bool AvFormatDecoder::DoRewind(long long desiredFrame, bool discardFrames)
00522 {
00523     LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("DoRewind(%1, %2 discard frames)")
00524             .arg(desiredFrame).arg( discardFrames ? "do" : "don't" ));
00525 
00526     if (recordingHasPositionMap || livetv)
00527         return DecoderBase::DoRewind(desiredFrame, discardFrames);
00528 
00529     dorewind = true;
00530 
00531     // avformat-based seeking
00532     return DoFastForward(desiredFrame, discardFrames);
00533 }
00534 
00535 bool AvFormatDecoder::DoFastForward(long long desiredFrame, bool discardFrames)
00536 {
00537     LOG(VB_PLAYBACK, LOG_INFO, LOC +
00538         QString("DoFastForward(%1 (%2), %3 discard frames)")
00539             .arg(desiredFrame).arg(framesPlayed)
00540             .arg((discardFrames) ? "do" : "don't"));
00541 
00542     if (recordingHasPositionMap || livetv)
00543         return DecoderBase::DoFastForward(desiredFrame, discardFrames);
00544 
00545     bool oldrawstate = getrawframes;
00546     getrawframes = false;
00547 
00548     AVStream *st = NULL;
00549     for (uint i = 0; i < ic->nb_streams; i++)
00550     {
00551         AVStream *st1 = ic->streams[i];
00552         if (st1 && st1->codec->codec_type == AVMEDIA_TYPE_VIDEO)
00553         {
00554             st = st1;
00555             break;
00556         }
00557     }
00558     if (!st)
00559         return false;
00560 
00561     int seekDelta = desiredFrame - framesPlayed;
00562 
00563     // avoid using av_frame_seek if we are seeking frame-by-frame when paused
00564     if (seekDelta >= 0 && seekDelta < 2 && !dorewind && m_parent->GetPlaySpeed() == 0.0f)
00565     {
00566         SeekReset(framesPlayed, seekDelta, false, true);
00567         m_parent->SetFramesPlayed(framesPlayed + 1);
00568         return true;
00569     }
00570 
00571     long long ts = 0;
00572     if (ic->start_time != (int64_t)AV_NOPTS_VALUE)
00573         ts = ic->start_time;
00574 
00575     // convert framenumber to normalized timestamp
00576     long double seekts = desiredFrame * AV_TIME_BASE / fps;
00577     ts += (long long)seekts;
00578 
00579     bool exactseeks = DecoderBase::getExactSeeks();
00580 
00581     int flags = (dorewind || exactseeks) ? AVSEEK_FLAG_BACKWARD : 0;
00582 
00583     if (av_seek_frame(ic, -1, ts, flags) < 0)
00584     {
00585         LOG(VB_GENERAL, LOG_ERR, LOC +
00586             QString("av_seek_frame(ic, -1, %1, 0) -- error").arg(ts));
00587         return false;
00588     }
00589 
00590     int normalframes = 0;
00591 
00592     if (st->cur_dts != (int64_t)AV_NOPTS_VALUE)
00593     {
00594 
00595         int64_t adj_cur_dts = st->cur_dts;
00596 
00597         if (ic->start_time != (int64_t)AV_NOPTS_VALUE)
00598         {
00599             int64_t st1 = av_rescale(ic->start_time,
00600                                     st->time_base.den,
00601                                     AV_TIME_BASE * (int64_t)st->time_base.num);
00602             adj_cur_dts = lsb3full(adj_cur_dts, st1, st->pts_wrap_bits);
00603         }
00604 
00605         int64_t adj_seek_dts = av_rescale(seekts,
00606                                           st->time_base.den,
00607                                           AV_TIME_BASE * (int64_t)st->time_base.num);
00608 
00609         int64_t max_dts = (st->pts_wrap_bits < 64) ? (1LL<<st->pts_wrap_bits)-1 : -1LL;
00610 
00611         // When seeking near the start of a stream the current dts is sometimes
00612         // less than the start time which causes lsb3full to return adj_cur_dts
00613         // close to the maximum dts value. If so, set adj_cur_dts to zero.
00614         if (adj_seek_dts < max_dts / 64 && adj_cur_dts > max_dts / 2)
00615             adj_cur_dts = 0;
00616 
00617         long long newts = av_rescale(adj_cur_dts,
00618                                 (int64_t)AV_TIME_BASE *
00619                                 (int64_t)st->time_base.num,
00620                                 st->time_base.den);
00621 
00622         lastKey = (long long)((newts*(long double)fps)/AV_TIME_BASE);
00623         framesPlayed = lastKey;
00624         framesRead = lastKey;
00625 
00626         normalframes = (exactseeks) ? desiredFrame - framesPlayed : 0;
00627         normalframes = max(normalframes, 0);
00628         no_dts_hack = false;
00629     }
00630     else
00631     {
00632         LOG(VB_GENERAL, LOG_INFO, LOC + "No DTS Seeking Hack!");
00633         no_dts_hack = true;
00634         framesPlayed = desiredFrame;
00635         framesRead = desiredFrame;
00636         normalframes = 0;
00637     }
00638 
00639     SeekReset(lastKey, normalframes, true, discardFrames);
00640 
00641     if (discardFrames)
00642         m_parent->SetFramesPlayed(framesPlayed + 1);
00643 
00644     dorewind = false;
00645 
00646     getrawframes = oldrawstate;
00647 
00648     return true;
00649 }
00650 
00651 void AvFormatDecoder::SeekReset(long long newKey, uint skipFrames,
00652                                 bool doflush, bool discardFrames)
00653 {
00654     if (ringBuffer->IsInDiscMenuOrStillFrame() || newKey == 0)
00655         return;
00656 
00657     LOG(VB_PLAYBACK, LOG_INFO, LOC +
00658         QString("SeekReset(%1, %2, %3 flush, %4 discard)")
00659             .arg(newKey).arg(skipFrames)
00660             .arg((doflush) ? "do" : "don't")
00661             .arg((discardFrames) ? "do" : "don't"));
00662 
00663     DecoderBase::SeekReset(newKey, skipFrames, doflush, discardFrames);
00664 
00665     if (doflush)
00666     {
00667         lastapts = 0;
00668         lastvpts = 0;
00669         lastccptsu = 0;
00670         faulty_pts = faulty_dts = 0;
00671         last_pts_for_fault_detection = 0;
00672         last_dts_for_fault_detection = 0;
00673         pts_detected = false;
00674         reordered_pts_detected = false;
00675 
00676         ff_read_frame_flush(ic);
00677 
00678         // Only reset the internal state if we're using our seeking,
00679         // not when using libavformat's seeking
00680         if (recordingHasPositionMap || livetv)
00681         {
00682             ic->pb->pos = ringBuffer->GetReadPosition();
00683             ic->pb->buf_ptr = ic->pb->buffer;
00684             ic->pb->buf_end = ic->pb->buffer;
00685             ic->pb->eof_reached = 0;
00686         }
00687 
00688         // Flush the avcodec buffers
00689         LOG(VB_PLAYBACK, LOG_INFO, LOC + "SeekReset() flushing");
00690         for (uint i = 0; i < ic->nb_streams; i++)
00691         {
00692             AVCodecContext *enc = ic->streams[i]->codec;
00693             if (enc->codec)
00694                 avcodec_flush_buffers(enc);
00695         }
00696         if (private_dec)
00697             private_dec->Reset();
00698     }
00699 
00700     // Discard all the queued up decoded frames
00701     if (discardFrames)
00702         m_parent->DiscardVideoFrames(doflush);
00703 
00704     if (doflush)
00705     {
00706         // Free up the stored up packets
00707         while (!storedPackets.isEmpty())
00708         {
00709             AVPacket *pkt = storedPackets.takeFirst();
00710             av_free_packet(pkt);
00711             delete pkt;
00712         }
00713 
00714         prevgoppos = 0;
00715         gopset = false;
00716         if (!ringBuffer->IsDVD())
00717         {
00718             if (!no_dts_hack)
00719             {
00720                 framesPlayed = lastKey;
00721                 framesRead = lastKey;
00722             }
00723 
00724             no_dts_hack = false;
00725         }
00726     }
00727 
00728     // Skip all the desired number of skipFrames
00729     for (;skipFrames > 0 && !ateof; skipFrames--)
00730     {
00731         GetFrame(kDecodeVideo);
00732         if (decoded_video_frame)
00733         {
00734             m_parent->DiscardVideoFrame(decoded_video_frame);
00735             decoded_video_frame = NULL;
00736         }
00737     }
00738 
00739     if (doflush)
00740     {
00741         firstvpts = 0;
00742         firstvptsinuse = true;
00743     }
00744 }
00745 
00746 void AvFormatDecoder::SetEof(bool eof)
00747 {
00748     if (!eof && ic && ic->pb)
00749     {
00750         LOG(VB_GENERAL, LOG_NOTICE, LOC +
00751             QString("Resetting byte context eof (livetv %1 was eof %2)")
00752                 .arg(livetv).arg(ic->pb->eof_reached));
00753         ic->pb->eof_reached = 0;
00754     }
00755     DecoderBase::SetEof(eof);
00756 }
00757 
00758 void AvFormatDecoder::Reset(bool reset_video_data, bool seek_reset,
00759                             bool reset_file)
00760 {
00761     LOG(VB_PLAYBACK, LOG_INFO, LOC +
00762         QString("Reset: Video %1, Seek %2, File %3")
00763             .arg(reset_video_data).arg(seek_reset).arg(reset_file));
00764 
00765     if (seek_reset)
00766         SeekReset(0, 0, true, false);
00767 
00768     DecoderBase::Reset(reset_video_data, false, reset_file);
00769 
00770     if (reset_video_data)
00771     {
00772         seen_gop = false;
00773         seq_count = 0;
00774     }
00775 }
00776 
00777 bool AvFormatDecoder::CanHandle(char testbuf[kDecoderProbeBufferSize],
00778                                 const QString &filename, int testbufsize)
00779 {
00780     {
00781         QMutexLocker locker(avcodeclock);
00782         av_register_all();
00783     }
00784 
00785     AVProbeData probe;
00786 
00787     QByteArray fname = filename.toAscii();
00788     probe.filename = fname.constData();
00789     probe.buf = (unsigned char *)testbuf;
00790     probe.buf_size = testbufsize;
00791 
00792     int score = AVPROBE_SCORE_MAX/4;
00793 
00794     if (testbufsize + AVPROBE_PADDING_SIZE > kDecoderProbeBufferSize)
00795     {
00796         probe.buf_size = kDecoderProbeBufferSize - AVPROBE_PADDING_SIZE;
00797         score = 0;
00798     }
00799     else if (testbufsize*2 >= kDecoderProbeBufferSize)
00800     {
00801         score--;
00802     }
00803 
00804     if (av_probe_input_format2(&probe, true, &score))
00805         return true;
00806     return false;
00807 }
00808 
00809 void AvFormatDecoder::InitByteContext(void)
00810 {
00811     int buf_size = ringBuffer->BestBufferSize();
00812     int streamed = ringBuffer->IsStreamed();
00813     LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Buffer size: %1, streamed %2")
00814                                    .arg(buf_size).arg(streamed));
00815 
00816     readcontext.prot = &AVF_RingBuffer_Protocol;
00817     readcontext.flags = AVIO_FLAG_READ;
00818     readcontext.is_streamed = streamed;
00819     readcontext.max_packet_size = 0;
00820     readcontext.priv_data = avfRingBuffer;
00821     unsigned char* buffer = (unsigned char *)av_malloc(buf_size);
00822     ic->pb = avio_alloc_context(buffer, buf_size, 0,
00823                                 &readcontext,
00824                                 AVF_Read_Packet,
00825                                 AVF_Write_Packet,
00826                                 AVF_Seek_Packet);
00827 
00828     ic->pb->seekable = !streamed;
00829 }
00830 
00831 extern "C" void HandleStreamChange(void *data)
00832 {
00833     AvFormatDecoder *decoder =
00834         reinterpret_cast<AvFormatDecoder*>(data);
00835 
00836     int cnt = decoder->ic->nb_streams;
00837 
00838     LOG(VB_PLAYBACK, LOG_INFO, LOC +
00839         QString("streams_changed 0x%1 -- stream count %2")
00840             .arg((uint64_t)data,0,16).arg(cnt));
00841 
00842     QMutexLocker locker(avcodeclock);
00843     decoder->SeekReset(0, 0, true, true);
00844     decoder->ScanStreams(false);
00845 }
00846 
00847 extern "C" void HandleDVDStreamChange(void *data)
00848 {
00849     AvFormatDecoder *decoder =
00850         reinterpret_cast<AvFormatDecoder*>(data);
00851 
00852     int cnt = decoder->ic->nb_streams;
00853 
00854     LOG(VB_PLAYBACK, LOG_INFO, LOC +
00855         QString("streams_changed 0x%1 -- stream count %2")
00856             .arg((uint64_t)data,0,16).arg(cnt));
00857 
00858     QMutexLocker locker(avcodeclock);
00859     //decoder->SeekReset(0, 0, true, true);
00860     decoder->ScanStreams(true);
00861 }
00862 
00863 extern "C" void HandleBDStreamChange(void *data)
00864 {
00865     AvFormatDecoder *decoder =
00866         reinterpret_cast<AvFormatDecoder*>(data);
00867 
00868     LOG(VB_PLAYBACK, LOG_INFO, LOC + "resetting");
00869 
00870     QMutexLocker locker(avcodeclock);
00871     decoder->Reset(true, false, false);
00872     decoder->CloseCodecs();
00873     decoder->FindStreamInfo();
00874     decoder->ScanStreams(false);
00875 }
00876 
00877 int AvFormatDecoder::FindStreamInfo(void)
00878 {
00879     QMutexLocker lock(avcodeclock);
00880     silence_ffmpeg_logging = true;
00881     int retval = avformat_find_stream_info(ic, NULL);
00882     silence_ffmpeg_logging = false;
00883     return retval;
00884 }
00885 
00898 int AvFormatDecoder::OpenFile(RingBuffer *rbuffer, bool novideo,
00899                               char testbuf[kDecoderProbeBufferSize],
00900                               int testbufsize)
00901 {
00902     CloseContext();
00903 
00904     ringBuffer = rbuffer;
00905 
00906     if (avfRingBuffer)
00907         delete avfRingBuffer;
00908     avfRingBuffer = new AVFRingBuffer(rbuffer);
00909 
00910     AVInputFormat *fmt      = NULL;
00911     QString        fnames   = ringBuffer->GetFilename();
00912     QByteArray     fnamea   = fnames.toAscii();
00913     const char    *filename = fnamea.constData();
00914 
00915     AVProbeData probe;
00916     probe.filename = filename;
00917     probe.buf = (unsigned char *)testbuf;
00918     if (testbufsize + AVPROBE_PADDING_SIZE <= kDecoderProbeBufferSize)
00919         probe.buf_size = testbufsize;
00920     else
00921         probe.buf_size = kDecoderProbeBufferSize - AVPROBE_PADDING_SIZE;
00922 
00923     fmt = av_probe_input_format(&probe, true);
00924     if (!fmt)
00925     {
00926         LOG(VB_GENERAL, LOG_ERR, LOC +
00927                 QString("Probe failed for file: \"%1\".").arg(filename));
00928         return -1;
00929     }
00930 
00931 #if 0
00932     fmt->flags |= AVFMT_NOFILE;
00933 #endif
00934 
00935     ic = avformat_alloc_context();
00936     if (!ic)
00937     {
00938         LOG(VB_GENERAL, LOG_ERR, LOC + "Could not allocate format context.");
00939         return -1;
00940     }
00941 
00942     InitByteContext();
00943 
00944     int err = avformat_open_input(&ic, filename, fmt, NULL);
00945     if (err < 0)
00946     {
00947         LOG(VB_GENERAL, LOG_ERR, LOC +
00948             QString("avformat err(%1) on avformat_open_input call.").arg(err));
00949         return -1;
00950     }
00951 
00952     int ret = FindStreamInfo();
00953 
00954     // Reset DVD/bluray ringbuffers
00955     if (!ringBuffer->StartFromBeginning())
00956         return -1;
00957     ringBuffer->IgnoreWaitStates(false);
00958 
00959     if (ret < 0)
00960     {
00961         LOG(VB_GENERAL, LOG_ERR, LOC + "Could not find codec parameters. " +
00962                 QString("file was \"%1\".").arg(filename));
00963         avformat_close_input(&ic);
00964         ic = NULL;
00965         return -1;
00966     }
00967     ic->streams_changed = HandleStreamChange;
00968     if (ringBuffer->IsDVD())
00969         ic->streams_changed = HandleDVDStreamChange;
00970     else if (ringBuffer->IsBD())
00971         ic->streams_changed = HandleBDStreamChange;
00972 
00973     ic->stream_change_data = this;
00974 
00975     fmt->flags &= ~AVFMT_NOFILE;
00976 
00977     if (!livetv && !ringBuffer->IsDisc())
00978     {
00979         // generate timings based on the video stream to avoid bogus ffmpeg
00980         // values for duration and bitrate
00981         av_update_stream_timings_video(ic);
00982     }
00983 
00984     // Scan for the initial A/V streams
00985     ret = ScanStreams(novideo);
00986     if (-1 == ret)
00987         return ret;
00988 
00989     AutoSelectTracks(); // This is needed for transcoder
00990 
00991 #ifdef USING_MHEG
00992     {
00993         int initialAudio = -1, initialVideo = -1;
00994         if (itv || (itv = m_parent->GetInteractiveTV()))
00995             itv->GetInitialStreams(initialAudio, initialVideo);
00996         if (initialAudio >= 0)
00997             SetAudioByComponentTag(initialAudio);
00998         if (initialVideo >= 0)
00999             SetVideoByComponentTag(initialVideo);
01000     }
01001 #endif // USING_MHEG
01002 
01003     // Try to get a position map from the recorder if we don't have one yet.
01004     if (!recordingHasPositionMap && !is_db_ignored)
01005     {
01006         if ((m_playbackinfo) || livetv || watchingrecording)
01007         {
01008             recordingHasPositionMap |= SyncPositionMap();
01009             if (recordingHasPositionMap && !livetv && !watchingrecording)
01010             {
01011                 hasFullPositionMap = true;
01012                 gopset = true;
01013             }
01014         }
01015     }
01016 
01017     // If watching pre-recorded television or video use the marked duration
01018     // from the db if it exists, else ffmpeg duration
01019     int64_t dur = 0, frames = 0;
01020 
01021     if (m_playbackinfo)
01022     {
01023         dur = m_playbackinfo->QueryTotalDuration();
01024         dur /= 1000000;
01025         frames = m_playbackinfo->QueryTotalFrames();
01026     }
01027 
01028     if (dur == 0)
01029     {
01030         if ((ic->duration == (int64_t)AV_NOPTS_VALUE) &&
01031             (!livetv && !ringBuffer->IsDisc()))
01032             av_estimate_timings(ic, 0);
01033 
01034         dur = ic->duration / (int64_t)AV_TIME_BASE;
01035     }
01036 
01037     if (dur > 0 && !livetv && !watchingrecording)
01038     {
01039         m_parent->SetDuration((int)dur);
01040     }
01041 
01042     // If we don't have a position map, set up ffmpeg for seeking
01043     if (!recordingHasPositionMap && !livetv)
01044     {
01045         LOG(VB_PLAYBACK, LOG_INFO, LOC +
01046             "Recording has no position -- using libavformat seeking.");
01047 
01048         if (dur > 0)
01049         {
01050             m_parent->SetFileLength((int)(dur), (int)(dur * fps));
01051         }
01052         else
01053         {
01054             // the pvr-250 seems to over report the bitrate by * 2
01055             float bytespersec = (float)bitrate / 8 / 2;
01056             float secs = ringBuffer->GetRealFileSize() * 1.0 / bytespersec;
01057             m_parent->SetFileLength((int)(secs), (int)(secs * fps));
01058         }
01059 
01060         // we will not see a position map from db or remote encoder,
01061         // set the gop interval to 15 frames.  if we guess wrong, the
01062         // auto detection will change it.
01063         keyframedist = 15;
01064         positionMapType = MARK_GOP_BYFRAME;
01065 
01066         if (!strcmp(fmt->name, "avi"))
01067         {
01068             // avi keyframes are too irregular
01069             keyframedist = 1;
01070         }
01071 
01072         dontSyncPositionMap = true;
01073         ic->build_index = 1;
01074     }
01075     // we have a position map, disable libavformat's seek index
01076     else
01077         ic->build_index = 0;
01078 
01079     av_dump_format(ic, 0, filename, 0);
01080 
01081     // print some useful information if playback debugging is on
01082     if (hasFullPositionMap)
01083         LOG(VB_PLAYBACK, LOG_INFO, LOC + "Position map found");
01084     else if (recordingHasPositionMap)
01085         LOG(VB_PLAYBACK, LOG_INFO, LOC + "Partial position map found");
01086     LOG(VB_PLAYBACK, LOG_INFO, LOC +
01087         QString("Successfully opened decoder for file: \"%1\". novideo(%2)")
01088             .arg(filename).arg(novideo));
01089 
01090     // Print AVChapter information
01091     for (unsigned int i=0; i < ic->nb_chapters; i++)
01092     {
01093         int num = ic->chapters[i]->time_base.num;
01094         int den = ic->chapters[i]->time_base.den;
01095         int64_t start = ic->chapters[i]->start;
01096         long double total_secs = (long double)start * (long double)num /
01097                                  (long double)den;
01098         int hours = (int)total_secs / 60 / 60;
01099         int minutes = ((int)total_secs / 60) - (hours * 60);
01100         double secs = (double)total_secs -
01101                       (double)(hours * 60 * 60 + minutes * 60);
01102         long long framenum = (long long)(total_secs * fps);
01103         LOG(VB_PLAYBACK, LOG_INFO, LOC +
01104             QString("Chapter %1 found @ [%2:%3:%4]->%5")
01105                 .arg(QString().sprintf("%02d", i + 1))
01106                 .arg(QString().sprintf("%02d", hours))
01107                 .arg(QString().sprintf("%02d", minutes))
01108                 .arg(QString().sprintf("%06.3f", secs))
01109                 .arg(framenum));
01110     }
01111 
01112     if (getenv("FORCE_DTS_TIMESTAMPS"))
01113         force_dts_timestamps = true;
01114 
01115     // Return true if recording has position map
01116     return recordingHasPositionMap;
01117 }
01118 
01119 float AvFormatDecoder::normalized_fps(AVStream *stream, AVCodecContext *enc)
01120 {
01121     float fps, avg_fps, codec_fps, container_fps, estimated_fps;
01122     avg_fps = codec_fps = container_fps = estimated_fps = 0.0f;
01123 
01124     if (stream->avg_frame_rate.den && stream->avg_frame_rate.num)
01125         avg_fps = av_q2d(stream->avg_frame_rate); // MKV default_duration
01126 
01127     if (enc->time_base.den && enc->time_base.num) // tbc
01128         codec_fps = 1.0f / av_q2d(enc->time_base) / enc->ticks_per_frame;
01129     // Some formats report fps waaay too high. (wrong time_base)
01130     if (codec_fps > 121.0f && (enc->time_base.den > 10000) &&
01131         (enc->time_base.num == 1))
01132     {
01133         enc->time_base.num = 1001;  // seems pretty standard
01134         if (av_q2d(enc->time_base) > 0)
01135             codec_fps = 1.0f / av_q2d(enc->time_base);
01136     }
01137     if (stream->time_base.den && stream->time_base.num) // tbn
01138         container_fps = 1.0f / av_q2d(stream->time_base);
01139     if (stream->r_frame_rate.den && stream->r_frame_rate.num) // tbr
01140         estimated_fps = av_q2d(stream->r_frame_rate);
01141 
01142     // matroska demuxer sets the default_duration to avg_frame_rate
01143     // mov,mp4,m4a,3gp,3g2,mj2 demuxer sets avg_frame_rate
01144     if ((QString(ic->iformat->name).contains("matroska") ||
01145         QString(ic->iformat->name).contains("mov")) &&
01146         avg_fps < 121.0f && avg_fps > 3.0f)
01147         fps = avg_fps;
01148     else if (QString(ic->iformat->name).contains("avi") &&
01149         container_fps < 121.0f && container_fps > 3.0f)
01150         fps = container_fps; // avi uses container fps for timestamps
01151     else if (codec_fps < 121.0f && codec_fps > 3.0f)
01152         fps = codec_fps;
01153     else if (container_fps < 121.0f && container_fps > 3.0f)
01154         fps = container_fps;
01155     else if (estimated_fps < 121.0f && estimated_fps > 3.0f)
01156         fps = estimated_fps;
01157     else if (avg_fps < 121.0f && avg_fps > 3.0f)
01158         fps = avg_fps;
01159     else
01160         fps = 30000.0f / 1001.0f; // 29.97 fps
01161 
01162     if (fps != m_fps)
01163     {
01164         LOG(VB_PLAYBACK, LOG_INFO, LOC +
01165             QString("Selected FPS is %1 (avg %2 codec %3 "
01166                     "container %4 estimated %5)").arg(fps).arg(avg_fps)
01167                 .arg(codec_fps).arg(container_fps).arg(estimated_fps));
01168         m_fps = fps;
01169     }
01170 
01171     return fps;
01172 }
01173 
01174 static bool IS_VDPAU_PIX_FMT(enum PixelFormat fmt)
01175 {
01176     return
01177         fmt == PIX_FMT_VDPAU_H264  ||
01178         fmt == PIX_FMT_VDPAU_MPEG1 ||
01179         fmt == PIX_FMT_VDPAU_MPEG2 ||
01180         fmt == PIX_FMT_VDPAU_MPEG4 ||
01181         fmt == PIX_FMT_VDPAU_WMV3  ||
01182         fmt == PIX_FMT_VDPAU_VC1;
01183 }
01184 
01185 static enum PixelFormat get_format_vdpau(struct AVCodecContext *avctx,
01186                                          const enum PixelFormat *fmt)
01187 {
01188     int i = 0;
01189 
01190     for(i=0; fmt[i]!=PIX_FMT_NONE; i++)
01191         if (IS_VDPAU_PIX_FMT(fmt[i]))
01192             break;
01193 
01194     return fmt[i];
01195 }
01196 
01197 // Declared seperately to allow attribute
01198 static enum PixelFormat get_format_dxva2(struct AVCodecContext *,
01199                                          const enum PixelFormat *) MUNUSED;
01200 
01201 enum PixelFormat get_format_dxva2(struct AVCodecContext *avctx,
01202                                          const enum PixelFormat *fmt)
01203 {
01204     if (!fmt)
01205         return PIX_FMT_NONE;
01206     int i = 0;
01207     for (; fmt[i] != PIX_FMT_NONE ; i++)
01208         if (PIX_FMT_DXVA2_VLD == fmt[i])
01209             break;
01210     return fmt[i];
01211 }
01212 
01213 static bool IS_VAAPI_PIX_FMT(enum PixelFormat fmt)
01214 {
01215     return fmt == PIX_FMT_VAAPI_MOCO ||
01216            fmt == PIX_FMT_VAAPI_IDCT ||
01217            fmt == PIX_FMT_VAAPI_VLD;
01218 }
01219 
01220 // Declared seperately to allow attribute
01221 static enum PixelFormat get_format_vaapi(struct AVCodecContext *,
01222                                          const enum PixelFormat *) MUNUSED;
01223 
01224 enum PixelFormat get_format_vaapi(struct AVCodecContext *avctx,
01225                                          const enum PixelFormat *fmt)
01226 {
01227     if (!fmt)
01228         return PIX_FMT_NONE;
01229     int i = 0;
01230     for (; fmt[i] != PIX_FMT_NONE ; i++)
01231         if (IS_VAAPI_PIX_FMT(fmt[i]))
01232             break;
01233     return fmt[i];
01234 }
01235 
01236 static bool IS_DR1_PIX_FMT(const enum PixelFormat fmt)
01237 {
01238     switch (fmt)
01239     {
01240         case PIX_FMT_YUV420P:
01241         case PIX_FMT_YUVJ420P:
01242             return true;
01243         default:
01244             return false;
01245     }
01246 }
01247 
01248 void AvFormatDecoder::InitVideoCodec(AVStream *stream, AVCodecContext *enc,
01249                                      bool selectedStream)
01250 {
01251     LOG(VB_PLAYBACK, LOG_INFO, LOC +
01252         QString("InitVideoCodec() 0x%1 id(%2) type (%3).")
01253             .arg((uint64_t)enc,0,16)
01254             .arg(ff_codec_id_string(enc->codec_id))
01255             .arg(ff_codec_type_string(enc->codec_type)));
01256 
01257     if (ringBuffer && ringBuffer->IsDVD())
01258         directrendering = false;
01259 
01260     enc->opaque = (void *)this;
01261     enc->get_buffer = get_avf_buffer;
01262     enc->release_buffer = release_avf_buffer;
01263     enc->draw_horiz_band = NULL;
01264     enc->slice_flags = 0;
01265 
01266     enc->err_recognition = AV_EF_COMPLIANT;
01267     enc->workaround_bugs = FF_BUG_AUTODETECT;
01268     enc->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
01269     enc->idct_algo = FF_IDCT_AUTO;
01270     enc->debug = 0;
01271     enc->error_rate = 0;
01272 
01273     AVCodec *codec = avcodec_find_decoder(enc->codec_id);
01274     // look for a vdpau capable codec
01275     if (codec_is_vdpau(video_codec_id) && !CODEC_IS_VDPAU(codec))
01276         codec = find_vdpau_decoder(codec, enc->codec_id);
01277 
01278     if (selectedStream)
01279     {
01280         directrendering = true;
01281         if (!gCoreContext->GetNumSetting("DecodeExtraAudio", 0) &&
01282             !CODEC_IS_HWACCEL(codec, enc))
01283         {
01284             SetLowBuffers(false);
01285         }
01286     }
01287 
01288     if (CODEC_IS_VDPAU(codec))
01289     {
01290         enc->get_buffer      = get_avf_buffer_vdpau;
01291         enc->get_format      = get_format_vdpau;
01292         enc->release_buffer  = release_avf_buffer_vdpau;
01293         enc->draw_horiz_band = render_slice_vdpau;
01294         enc->slice_flags     = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
01295     }
01296     else if (CODEC_IS_DXVA2(codec, enc))
01297     {
01298         enc->get_buffer      = get_avf_buffer_dxva2;
01299         enc->get_format      = get_format_dxva2;
01300         enc->release_buffer  = release_avf_buffer;
01301     }
01302     else if (CODEC_IS_VAAPI(codec, enc))
01303     {
01304         enc->get_buffer      = get_avf_buffer_vaapi;
01305         enc->get_format      = get_format_vaapi;
01306         enc->release_buffer  = release_avf_buffer;
01307         enc->slice_flags     = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
01308     }
01309     else if (codec && codec->capabilities & CODEC_CAP_DR1)
01310     {
01311         enc->flags          |= CODEC_FLAG_EMU_EDGE;
01312     }
01313     else
01314     {
01315         if (selectedStream)
01316             directrendering = false;
01317         LOG(VB_PLAYBACK, LOG_INFO, LOC +
01318             QString("Using software scaling to convert pixel format %1 for "
01319                     "codec %2").arg(enc->pix_fmt)
01320                 .arg(ff_codec_id_string(enc->codec_id)));
01321     }
01322 
01323     if (FlagIsSet(kDecodeLowRes)    || FlagIsSet(kDecodeSingleThreaded) ||
01324         FlagIsSet(kDecodeFewBlocks) || FlagIsSet(kDecodeNoLoopFilter)   ||
01325         FlagIsSet(kDecodeNoDecode))
01326     {
01327         enc->flags2 |= CODEC_FLAG2_FAST;
01328 
01329         if ((CODEC_ID_MPEG2VIDEO == codec->id) ||
01330             (CODEC_ID_MPEG1VIDEO == codec->id))
01331         {
01332             if (FlagIsSet(kDecodeFewBlocks))
01333             {
01334                 uint total_blocks = (enc->height+15) / 16;
01335                 enc->skip_top     = (total_blocks+3) / 4;
01336                 enc->skip_bottom  = (total_blocks+3) / 4;
01337             }
01338 
01339             if (FlagIsSet(kDecodeLowRes))
01340                 enc->lowres = 2; // 1 = 1/2 size, 2 = 1/4 size
01341         }
01342         else if (CODEC_ID_H264 == codec->id)
01343         {
01344             if (FlagIsSet(kDecodeNoLoopFilter))
01345             {
01346                 enc->flags &= ~CODEC_FLAG_LOOP_FILTER;
01347                 enc->skip_loop_filter = AVDISCARD_ALL;
01348             }
01349         }
01350 
01351         if (FlagIsSet(kDecodeNoDecode))
01352         {
01353             enc->skip_idct = AVDISCARD_ALL;
01354         }
01355     }
01356 
01357     if (selectedStream)
01358     {
01359         fps = normalized_fps(stream, enc);
01360         QSize dim    = get_video_dim(*enc);
01361         int   width  = current_width  = dim.width();
01362         int   height = current_height = dim.height();
01363         current_aspect = get_aspect(*enc);
01364 
01365         if (!width || !height)
01366         {
01367             LOG(VB_PLAYBACK, LOG_INFO, LOC +
01368                 "InitVideoCodec invalid dimensions, resetting decoder.");
01369             width  = 640;
01370             height = 480;
01371             fps    = 29.97f;
01372             current_aspect = 4.0f / 3.0f;
01373         }
01374 
01375         m_parent->SetKeyframeDistance(keyframedist);
01376         m_parent->SetVideoParams(width, height, fps, kScan_Detect);
01377         if (LCD *lcd = LCD::Get())
01378         {
01379             LCDVideoFormatSet video_format;
01380 
01381             switch (enc->codec_id)
01382             {
01383                 case CODEC_ID_H263:
01384                 case CODEC_ID_MPEG4:
01385                 case CODEC_ID_MSMPEG4V1:
01386                 case CODEC_ID_MSMPEG4V2:
01387                 case CODEC_ID_MSMPEG4V3:
01388                 case CODEC_ID_H263P:
01389                 case CODEC_ID_H263I:
01390                     video_format = VIDEO_DIVX;
01391                     break;
01392                 case CODEC_ID_WMV1:
01393                 case CODEC_ID_WMV2:
01394                     video_format = VIDEO_WMV;
01395                     break;
01396 #if 0
01397                 case CODEC_ID_XVID:
01398                     video_format = VIDEO_XVID;
01399                     break;
01400 #endif
01401                 default:
01402                     video_format = VIDEO_MPG;
01403                     break;
01404             }
01405 
01406             lcd->setVideoFormatLEDs(video_format, true);
01407 
01408             if(height >= 720)
01409                 lcd->setVariousLEDs(VARIOUS_HDTV, true);
01410             else
01411                 lcd->setVariousLEDs(VARIOUS_HDTV, false);
01412         }
01413     }
01414 }
01415 
01416 // CC Parity checking
01417 // taken from xine-lib libspucc
01418 
01419 static int cc608_parity(uint8_t byte)
01420 {
01421     int ones = 0;
01422 
01423     for (int i = 0; i < 7; i++)
01424     {
01425         if (byte & (1 << i))
01426             ones++;
01427     }
01428 
01429     return ones & 1;
01430 }
01431 
01432 // CC Parity checking
01433 // taken from xine-lib libspucc
01434 
01435 static void cc608_build_parity_table(int *parity_table)
01436 {
01437     uint8_t byte;
01438     int parity_v;
01439     for (byte = 0; byte <= 127; byte++)
01440     {
01441         parity_v = cc608_parity(byte);
01442         /* CC uses odd parity (i.e., # of 1's in byte is odd.) */
01443         parity_table[byte] = parity_v;
01444         parity_table[byte | 0x80] = !parity_v;
01445     }
01446 }
01447 
01448 // CC Parity checking
01449 // taken from xine-lib libspucc
01450 
01451 static int cc608_good_parity(const int *parity_table, uint16_t data)
01452 {
01453     int ret = parity_table[data & 0xff] && parity_table[(data & 0xff00) >> 8];
01454     if (!ret)
01455     {
01456         LOG(VB_VBI, LOG_ERR, LOC +
01457             QString("VBI: Bad parity in EIA-608 data (%1)") .arg(data,0,16));
01458     }
01459     return ret;
01460 }
01461 
01462 void AvFormatDecoder::ScanATSCCaptionStreams(int av_index)
01463 {
01464     memset(ccX08_in_pmt, 0, sizeof(ccX08_in_pmt));
01465     pmt_tracks.clear();
01466     pmt_track_types.clear();
01467 
01468     // Figure out languages of ATSC captions
01469     if (!ic->cur_pmt_sect)
01470     {
01471         LOG(VB_GENERAL, LOG_DEBUG, LOC +
01472             "ScanATSCCaptionStreams() called with no PMT");
01473         return;
01474     }
01475 
01476     const PESPacket pes = PESPacket::ViewData(ic->cur_pmt_sect);
01477     const PSIPTable psip(pes);
01478     const ProgramMapTable pmt(psip);
01479 
01480     uint i;
01481     for (i = 0; i < pmt.StreamCount(); i++)
01482     {
01483         // MythTV remaps OpenCable Video to normal video during recording
01484         // so "dvb" is the safest choice for system info type, since this
01485         // will ignore other uses of the same stream id in DVB countries.
01486         if (pmt.IsVideo(i, "dvb"))
01487             break;
01488     }
01489 
01490     if (!pmt.IsVideo(i, "dvb"))
01491         return;
01492 
01493     desc_list_t desc_list = MPEGDescriptor::ParseOnlyInclude(
01494         pmt.StreamInfo(i), pmt.StreamInfoLength(i),
01495         DescriptorID::caption_service);
01496 
01497     const desc_list_t desc_list2 = MPEGDescriptor::ParseOnlyInclude(
01498         pmt.ProgramInfo(), pmt.ProgramInfoLength(),
01499         DescriptorID::caption_service);
01500 
01501     desc_list.insert(desc_list.end(), desc_list2.begin(), desc_list2.end());
01502 
01503     for (uint j = 0; j < desc_list.size(); j++)
01504     {
01505         const CaptionServiceDescriptor csd(desc_list[j]);
01506         for (uint k = 0; k < csd.ServicesCount(); k++)
01507         {
01508             int lang = csd.CanonicalLanguageKey(k);
01509             int type = csd.Type(k) ? 1 : 0;
01510             if (type)
01511             {
01512                 StreamInfo si(av_index, lang, 0/*lang_idx*/,
01513                               csd.CaptionServiceNumber(k),
01514                               csd.EasyReader(k),
01515                               csd.WideAspectRatio(k));
01516                 uint key = csd.CaptionServiceNumber(k) + 4;
01517                 ccX08_in_pmt[key] = true;
01518                 pmt_tracks.push_back(si);
01519                 pmt_track_types.push_back(kTrackTypeCC708);
01520             }
01521             else
01522             {
01523                 int line21 = csd.Line21Field(k) ? 3 : 1;
01524                 StreamInfo si(av_index, lang, 0/*lang_idx*/, line21, 0);
01525                 ccX08_in_pmt[line21-1] = true;
01526                 pmt_tracks.push_back(si);
01527                 pmt_track_types.push_back(kTrackTypeCC608);
01528             }
01529         }
01530     }
01531 }
01532 
01533 void AvFormatDecoder::UpdateATSCCaptionTracks(void)
01534 {
01535     tracks[kTrackTypeCC608].clear();
01536     tracks[kTrackTypeCC708].clear();
01537     memset(ccX08_in_tracks, 0, sizeof(ccX08_in_tracks));
01538 
01539     uint pidx = 0, sidx = 0;
01540     map<int,uint> lang_cc_cnt[2];
01541     while (true)
01542     {
01543         bool pofr = pidx >= (uint)pmt_tracks.size();
01544         bool sofr = sidx >= (uint)stream_tracks.size();
01545         if (pofr && sofr)
01546             break;
01547 
01548         // choose lowest available next..
01549         // stream_id's of 608 and 708 streams alias, but this
01550         // is ok as we just want each list to be ordered.
01551         StreamInfo const *si = NULL;
01552         int type = 0; // 0 if 608, 1 if 708
01553         bool isp = true; // if true use pmt_tracks next, else stream_tracks
01554 
01555         if (pofr && !sofr)
01556             isp = false;
01557         else if (!pofr && sofr)
01558             isp = true;
01559         else if (stream_tracks[sidx] < pmt_tracks[pidx])
01560             isp = false;
01561 
01562         if (isp)
01563         {
01564             si = &pmt_tracks[pidx];
01565             type = kTrackTypeCC708 == pmt_track_types[pidx] ? 1 : 0;
01566             pidx++;
01567         }
01568         else
01569         {
01570             si = &stream_tracks[sidx];
01571             type = kTrackTypeCC708 == stream_track_types[sidx] ? 1 : 0;
01572             sidx++;
01573         }
01574 
01575         StreamInfo nsi(*si);
01576         int lang_indx = lang_cc_cnt[type][nsi.language];
01577         lang_cc_cnt[type][nsi.language]++;
01578         nsi.language_index = lang_indx;
01579         tracks[(type) ? kTrackTypeCC708 : kTrackTypeCC608].push_back(nsi);
01580         int key = (int)nsi.stream_id + ((type) ? 4 : -1);
01581         if (key < 0)
01582         {
01583             LOG(VB_GENERAL, LOG_ERR, LOC + "in_tracks key too small");
01584         }
01585         else
01586         {
01587             ccX08_in_tracks[key] = true;
01588         }
01589         LOG(VB_PLAYBACK, LOG_INFO, LOC +
01590             QString("%1 caption service #%2 is in the %3 language.")
01591                 .arg((type) ? "EIA-708" : "EIA-608")
01592                 .arg(nsi.stream_id)
01593                 .arg(iso639_key_toName(nsi.language)));
01594     }
01595 }
01596 
01597 void AvFormatDecoder::ScanTeletextCaptions(int av_index)
01598 {
01599     // ScanStreams() calls tracks[kTrackTypeTeletextCaptions].clear()
01600     if (!ic->cur_pmt_sect || tracks[kTrackTypeTeletextCaptions].size())
01601         return;
01602 
01603     const PESPacket pes = PESPacket::ViewData(ic->cur_pmt_sect);
01604     const PSIPTable psip(pes);
01605     const ProgramMapTable pmt(psip);
01606 
01607     for (uint i = 0; i < pmt.StreamCount(); i++)
01608     {
01609         if (pmt.StreamType(i) != StreamID::PrivData)
01610             continue;
01611 
01612         const desc_list_t desc_list = MPEGDescriptor::ParseOnlyInclude(
01613             pmt.StreamInfo(i), pmt.StreamInfoLength(i),
01614             DescriptorID::teletext);
01615 
01616         for (uint j = 0; j < desc_list.size(); j++)
01617         {
01618             const TeletextDescriptor td(desc_list[j]);
01619             for (uint k = 0; k < td.StreamCount(); k++)
01620             {
01621                 int type = td.TeletextType(k);
01622                 int language = td.CanonicalLanguageKey(k);
01623                 int magazine = td.TeletextMagazineNum(k);
01624                 if (magazine == 0)
01625                     magazine = 8;
01626                 int pagenum  = td.TeletextPageNum(k);
01627                 int lang_idx = (magazine << 8) | pagenum;
01628                 StreamInfo si(av_index, language, lang_idx, 0, 0);
01629                 if (type == 2 || type == 1)
01630                 {
01631                     TrackType track = (type == 2) ? kTrackTypeTeletextCaptions :
01632                                                     kTrackTypeTeletextMenu;
01633                     tracks[track].push_back(si);
01634                     LOG(VB_PLAYBACK, LOG_INFO, LOC +
01635                         QString("Teletext stream #%1 (%2) is in the %3 language"
01636                                 " on page %4 %5.")
01637                             .arg(k).arg((type == 2) ? "Caption" : "Menu")
01638                             .arg(iso639_key_toName(language))
01639                             .arg(magazine).arg(pagenum));
01640                 }
01641             }
01642         }
01643 
01644         // Assume there is only one multiplexed teletext stream in PMT..
01645         if (tracks[kTrackTypeTeletextCaptions].size())
01646             break;
01647     }
01648 }
01649 
01650 void AvFormatDecoder::ScanRawTextCaptions(int av_stream_index)
01651 {
01652     AVDictionaryEntry *metatag =
01653         av_dict_get(ic->streams[av_stream_index]->metadata, "language", NULL,
01654                     0);
01655     int lang = metatag ? get_canonical_lang(metatag->value) :
01656                          iso639_str3_to_key("und");
01657     LOG(VB_PLAYBACK, LOG_INFO, LOC +
01658         QString("Text Subtitle track #%1 is A/V stream #%2 "
01659                 "and is in the %3 language(%4).")
01660                     .arg(tracks[kTrackTypeRawText].size()).arg(av_stream_index)
01661                     .arg(iso639_key_toName(lang)).arg(lang));
01662     StreamInfo si(av_stream_index, lang, 0, 0, 0);
01663     tracks[kTrackTypeRawText].push_back(si);
01664 }
01665 
01670 void AvFormatDecoder::ScanDSMCCStreams(void)
01671 {
01672     if (!ic || !ic->cur_pmt_sect)
01673         return;
01674 
01675     if (!itv && ! (itv = m_parent->GetInteractiveTV()))
01676         return;
01677 
01678     const PESPacket pes = PESPacket::ViewData(ic->cur_pmt_sect);
01679     const PSIPTable psip(pes);
01680     const ProgramMapTable pmt(psip);
01681 
01682     for (uint i = 0; i < pmt.StreamCount(); i++)
01683     {
01684         if (! StreamID::IsObjectCarousel(pmt.StreamType(i)))
01685             continue;
01686 
01687         const desc_list_t desc_list = MPEGDescriptor::ParseOnlyInclude(
01688             pmt.StreamInfo(i), pmt.StreamInfoLength(i),
01689             DescriptorID::data_broadcast_id);
01690 
01691         for (uint j = 0; j < desc_list.size(); j++)
01692         {
01693             const unsigned char *desc = desc_list[j];
01694             desc++; // Skip tag
01695             uint length = *desc++;
01696             const unsigned char *endDesc = desc+length;
01697             uint dataBroadcastId = desc[0]<<8 | desc[1];
01698             if (dataBroadcastId != 0x0106) // ETSI/UK Profile
01699                 continue;
01700             desc += 2; // Skip data ID
01701             while (desc != endDesc)
01702             {
01703                 uint appTypeCode = desc[0]<<8 | desc[1];
01704                 desc += 3; // Skip app type code and boot priority hint
01705                 uint appSpecDataLen = *desc++;
01706 #ifdef USING_MHEG
01707                 if (appTypeCode == 0x101) // UK MHEG profile
01708                 {
01709                     const unsigned char *subDescEnd = desc + appSpecDataLen;
01710                     while (desc < subDescEnd)
01711                     {
01712                         uint sub_desc_tag = *desc++;
01713                         uint sub_desc_len = *desc++;
01714                         // Network boot info sub-descriptor.
01715                         if (sub_desc_tag == 1)
01716                             itv->SetNetBootInfo(desc, sub_desc_len);
01717                         desc += sub_desc_len;
01718                     }
01719                 }
01720                 else
01721 #else
01722                 (void) appTypeCode;
01723 #endif // USING_MHEG
01724                 {
01725                     desc += appSpecDataLen;
01726                 }
01727             }
01728         }
01729     }
01730 }
01731 
01732 int AvFormatDecoder::ScanStreams(bool novideo)
01733 {
01734     bool unknownbitrate = false;
01735     int scanerror = 0;
01736     bitrate       = 0;
01737     fps           = 0;
01738 
01739     tracks[kTrackTypeAttachment].clear();
01740     tracks[kTrackTypeAudio].clear();
01741     tracks[kTrackTypeSubtitle].clear();
01742     tracks[kTrackTypeTeletextCaptions].clear();
01743     tracks[kTrackTypeTeletextMenu].clear();
01744     tracks[kTrackTypeRawText].clear();
01745     tracks[kTrackTypeVideo].clear();
01746     selectedTrack[kTrackTypeVideo].av_stream_index = -1;
01747 
01748     map<int,uint> lang_sub_cnt;
01749     uint subtitleStreamCount = 0;
01750     map<int,uint> lang_aud_cnt;
01751     uint audioStreamCount = 0;
01752 
01753     if (ringBuffer && ringBuffer->IsDVD() &&
01754         ringBuffer->DVD()->AudioStreamsChanged())
01755     {
01756         ringBuffer->DVD()->AudioStreamsChanged(false);
01757         RemoveAudioStreams();
01758     }
01759 
01760     for (uint i = 0; i < ic->nb_streams; i++)
01761     {
01762         AVCodecContext *enc = ic->streams[i]->codec;
01763         LOG(VB_PLAYBACK, LOG_INFO, LOC +
01764             QString("Stream #%1, has id 0x%2 codec id %3, "
01765                     "type %4, bitrate %5 at 0x%6")
01766                 .arg(i).arg((uint64_t)ic->streams[i]->id,0,16)
01767                 .arg(ff_codec_id_string(enc->codec_id))
01768                 .arg(ff_codec_type_string(enc->codec_type))
01769                 .arg(enc->bit_rate).arg((uint64_t)ic->streams[i],0,16));
01770 
01771         switch (enc->codec_type)
01772         {
01773             case AVMEDIA_TYPE_VIDEO:
01774             {
01775                 //assert(enc->codec_id);
01776                 if (!enc->codec_id)
01777                 {
01778                     LOG(VB_GENERAL, LOG_ERR, LOC +
01779                         QString("Stream #%1 has an unknown video "
01780                                 "codec id, skipping.").arg(i));
01781                     continue;
01782                 }
01783 
01784                 codec_is_mpeg = CODEC_IS_FFMPEG_MPEG(enc->codec_id);
01785 
01786                 // ffmpeg does not return a bitrate for several codecs and
01787                 // formats. Forcing it to 500000 ensures the ringbuffer does not
01788                 // use optimisations for low bitrate (audio and data) streams.
01789                 if (enc->bit_rate == 0)
01790                 {
01791                     enc->bit_rate = 500000;
01792                     unknownbitrate = true;
01793                 }
01794 
01795                 StreamInfo si(i, 0, 0, 0, 0);
01796                 tracks[kTrackTypeVideo].push_back(si);
01797                 bitrate += enc->bit_rate;
01798                 if (novideo)
01799                     break;
01800 
01801                 delete private_dec;
01802                 private_dec = NULL;
01803                 m_h264_parser->Reset();
01804 
01805                 QSize dim = get_video_dim(*enc);
01806                 uint width  = max(dim.width(),  16);
01807                 uint height = max(dim.height(), 16);
01808                 QString dec = "ffmpeg";
01809                 uint thread_count = 1;
01810 
01811                 if (!is_db_ignored)
01812                 {
01813                     VideoDisplayProfile vdp;
01814                     vdp.SetInput(QSize(width, height));
01815                     dec = vdp.GetDecoder();
01816                     thread_count = vdp.GetMaxCPUs();
01817                     bool skip_loop_filter = vdp.IsSkipLoopEnabled();
01818                     if  (!skip_loop_filter)
01819                     {
01820                         enc->skip_loop_filter = AVDISCARD_NONKEY;
01821                     }
01822                 }
01823 
01824                 video_codec_id = kCodec_NONE;
01825                 int version = mpeg_version(enc->codec_id);
01826                 if (version)
01827                     video_codec_id = (MythCodecID)(kCodec_MPEG1 + version - 1);
01828 
01829                 if (version)
01830                 {
01831 #if defined(USING_VDPAU)
01832                     // HACK -- begin
01833                     // Force MPEG2 decoder on MPEG1 streams.
01834                     // Needed for broken transmitters which mark
01835                     // MPEG2 streams as MPEG1 streams, and should
01836                     // be harmless for unbroken ones.
01837                     if (enc->codec_id == CODEC_ID_MPEG1VIDEO)
01838                         enc->codec_id = CODEC_ID_MPEG2VIDEO;
01839                     // HACK -- end
01840 #endif // USING_VDPAU
01841 #ifdef USING_VDPAU
01842                     MythCodecID vdpau_mcid;
01843                     vdpau_mcid = VideoOutputVDPAU::GetBestSupportedCodec(
01844                         width, height, dec,
01845                         mpeg_version(enc->codec_id),
01846                         !FlagIsSet(kDecodeAllowGPU));
01847 
01848                     if (vdpau_mcid >= video_codec_id)
01849                     {
01850                         enc->codec_id = (CodecID) myth2av_codecid(vdpau_mcid);
01851                         video_codec_id = vdpau_mcid;
01852                     }
01853 #endif // USING_VDPAU
01854 #ifdef USING_GLVAAPI
01855                     MythCodecID vaapi_mcid;
01856                     PixelFormat pix_fmt = PIX_FMT_YUV420P;
01857                     vaapi_mcid = VideoOutputOpenGLVAAPI::GetBestSupportedCodec(
01858                             width, height, dec, mpeg_version(enc->codec_id),
01859                             !FlagIsSet(kDecodeAllowGPU), pix_fmt);
01860 
01861                     if (vaapi_mcid >= video_codec_id)
01862                     {
01863                         enc->codec_id = (CodecID)myth2av_codecid(vaapi_mcid);
01864                         video_codec_id = vaapi_mcid;
01865                         if (FlagIsSet(kDecodeAllowGPU) &&
01866                             codec_is_vaapi(video_codec_id))
01867                         {
01868                             enc->pix_fmt = pix_fmt;
01869                         }
01870                     }
01871 #endif // USING_GLVAAPI
01872 #ifdef USING_DXVA2
01873                     MythCodecID dxva2_mcid;
01874                     PixelFormat pix_fmt = PIX_FMT_YUV420P;
01875                     dxva2_mcid = VideoOutputD3D::GetBestSupportedCodec(
01876                         width, height, dec, mpeg_version(enc->codec_id),
01877                         !FlagIsSet(kDecodeAllowGPU), pix_fmt);
01878 
01879                     if (dxva2_mcid >= video_codec_id)
01880                     {
01881                         enc->codec_id = (CodecID)myth2av_codecid(dxva2_mcid);
01882                         video_codec_id = dxva2_mcid;
01883                         if (FlagIsSet(kDecodeAllowGPU) &&
01884                             codec_is_dxva2(video_codec_id))
01885                         {
01886                             enc->pix_fmt = pix_fmt;
01887                         }
01888                     }
01889 #endif // USING_DXVA2
01890                 }
01891 
01892                 // default to mpeg2
01893                 if (video_codec_id == kCodec_NONE)
01894                 {
01895                     LOG(VB_GENERAL, LOG_ERR, LOC +
01896                         "Unknown video codec - defaulting to MPEG2");
01897                     video_codec_id = kCodec_MPEG2;
01898                 }
01899 
01900                 if (enc->codec)
01901                 {
01902                     LOG(VB_GENERAL, LOG_WARNING, LOC +
01903                         QString("Warning, video codec 0x%1 id(%2) type (%3) "
01904                                 "already open.")
01905                             .arg((uint64_t)enc,0,16)
01906                             .arg(ff_codec_id_string(enc->codec_id))
01907                             .arg(ff_codec_type_string(enc->codec_type)));
01908                 }
01909 
01910                 // Set the default stream to the stream
01911                 // that is found first in the PMT
01912                 if (selectedTrack[kTrackTypeVideo].av_stream_index < 0)
01913                     selectedTrack[kTrackTypeVideo] = si;
01914 
01915                 // Use a PrivateDecoder if allowed in playerFlags AND matched
01916                 // via the decoder name
01917                 if (selectedTrack[kTrackTypeVideo].av_stream_index == (int) i)
01918                 {
01919                     private_dec = PrivateDecoder::Create(dec, playerFlags, enc);
01920                     if (private_dec)
01921                         thread_count = 1;
01922                 }
01923 
01924                 if (!codec_is_std(video_codec_id))
01925                     thread_count = 1;
01926 
01927                 if (FlagIsSet(kDecodeSingleThreaded))
01928                     thread_count = 1;
01929 
01930                 LOG(VB_PLAYBACK, LOG_INFO, LOC +
01931                     QString("Using %1 CPUs for decoding")
01932                         .arg(HAVE_THREADS ? thread_count : 1));
01933 
01934                 if (HAVE_THREADS)
01935                     enc->thread_count = thread_count;
01936 
01937                 InitVideoCodec(ic->streams[i], enc,
01938                     selectedTrack[kTrackTypeVideo].av_stream_index == (int) i);
01939 
01940                 ScanATSCCaptionStreams(i);
01941                 UpdateATSCCaptionTracks();
01942 
01943                 LOG(VB_PLAYBACK, LOG_INFO, LOC +
01944                     QString("Using %1 for video decoding")
01945                         .arg(GetCodecDecoderName()));
01946 
01947                 break;
01948             }
01949             case AVMEDIA_TYPE_AUDIO:
01950             {
01951                 if (enc->codec)
01952                 {
01953                     LOG(VB_GENERAL, LOG_WARNING, LOC +
01954                         QString("Warning, audio codec 0x%1 id(%2) "
01955                                 "type (%3) already open, leaving it alone.")
01956                             .arg((uint64_t)enc,0,16)
01957                             .arg(ff_codec_id_string(enc->codec_id))
01958                             .arg(ff_codec_type_string(enc->codec_type)));
01959                 }
01960                 //assert(enc->codec_id);
01961                 LOG(VB_GENERAL, LOG_INFO, LOC +
01962                     QString("codec %1 has %2 channels")
01963                         .arg(ff_codec_id_string(enc->codec_id))
01964                         .arg(enc->channels));
01965 
01966                 bitrate += enc->bit_rate;
01967                 break;
01968             }
01969             case AVMEDIA_TYPE_SUBTITLE:
01970             {
01971                 if (enc->codec_id == CODEC_ID_DVB_TELETEXT)
01972                     ScanTeletextCaptions(i);
01973                 if (enc->codec_id == CODEC_ID_TEXT)
01974                     ScanRawTextCaptions(i);
01975                 bitrate += enc->bit_rate;
01976 
01977                 LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("subtitle codec (%1)")
01978                         .arg(ff_codec_type_string(enc->codec_type)));
01979                 break;
01980             }
01981             case AVMEDIA_TYPE_DATA:
01982             {
01983                 ScanTeletextCaptions(i);
01984                 bitrate += enc->bit_rate;
01985                 LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("data codec (%1)")
01986                         .arg(ff_codec_type_string(enc->codec_type)));
01987                 break;
01988             }
01989             case AVMEDIA_TYPE_ATTACHMENT:
01990             {
01991                 if (enc->codec_id == CODEC_ID_TTF)
01992                    tracks[kTrackTypeAttachment].push_back(
01993                        StreamInfo(i, 0, 0, ic->streams[i]->id, 0));
01994                 bitrate += enc->bit_rate;
01995                 LOG(VB_PLAYBACK, LOG_INFO, LOC +
01996                     QString("Attachment codec (%1)")
01997                         .arg(ff_codec_type_string(enc->codec_type)));
01998                 break;
01999             }
02000             default:
02001             {
02002                 bitrate += enc->bit_rate;
02003                 LOG(VB_PLAYBACK, LOG_ERR, LOC +
02004                     QString("Unknown codec type (%1)")
02005                         .arg(ff_codec_type_string(enc->codec_type)));
02006                 break;
02007             }
02008         }
02009 
02010         if (enc->codec_type != AVMEDIA_TYPE_AUDIO &&
02011             enc->codec_type != AVMEDIA_TYPE_VIDEO &&
02012             enc->codec_type != AVMEDIA_TYPE_SUBTITLE)
02013             continue;
02014 
02015         // skip DVB teletext and text subs, there is no libavcodec decoder
02016         if (enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
02017            (enc->codec_id   == CODEC_ID_DVB_TELETEXT ||
02018             enc->codec_id   == CODEC_ID_TEXT))
02019             continue;
02020 
02021         LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Looking for decoder for %1")
02022                 .arg(ff_codec_id_string(enc->codec_id)));
02023 
02024         if (enc->codec_id == CODEC_ID_PROBE)
02025         {
02026             LOG(VB_GENERAL, LOG_ERR, LOC +
02027                 QString("Probing of stream #%1 unsuccesful, ignoring.").arg(i));
02028             continue;
02029         }
02030 
02031         AVCodec *codec = avcodec_find_decoder(enc->codec_id);
02032         if (!codec)
02033         {
02034             LOG(VB_GENERAL, LOG_ERR, LOC +
02035                 QString("Could not find decoder for codec (%1), ignoring.")
02036                     .arg(ff_codec_id_string(enc->codec_id)));
02037 
02038             // Nigel's bogus codec-debug. Dump the list of codecs & decoders,
02039             // and have one last attempt to find a decoder. This is usually
02040             // only caused by build problems, where libavcodec needs a rebuild
02041             if (VERBOSE_LEVEL_CHECK(VB_LIBAV, LOG_ANY))
02042             {
02043                 AVCodec *p = av_codec_next(NULL);
02044                 int      i = 1;
02045                 while (p)
02046                 {
02047                     QString msg;
02048 
02049                     if (p->name[0] != '\0')
02050                         msg = QString("Codec %1:").arg(p->name);
02051                     else
02052                         msg = QString("Codec %1, null name,").arg(i);
02053 
02054                     if (p->decode == NULL)
02055                         msg += "decoder is null";
02056 
02057                     LOG(VB_LIBAV, LOG_INFO, LOC + msg);
02058 
02059                     if (p->id == enc->codec_id)
02060                     {
02061                         codec = p;
02062                         break;
02063                     }
02064 
02065                     LOG(VB_LIBAV, LOG_INFO, LOC +
02066                         QString("Codec 0x%1 != 0x%2") .arg(p->id, 0, 16)
02067                             .arg(enc->codec_id, 0, 16));
02068                     p = av_codec_next(p);
02069                     ++i;
02070                 }
02071             }
02072             if (!codec)
02073                 continue;
02074         }
02075         // select vdpau capable decoder if needed
02076         else if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
02077                  codec_is_vdpau(video_codec_id) && !CODEC_IS_VDPAU(codec))
02078         {
02079             codec = find_vdpau_decoder(codec, enc->codec_id);
02080         }
02081 
02082         if (!enc->codec)
02083         {
02084             QMutexLocker locker(avcodeclock);
02085 
02086             int open_val = avcodec_open2(enc, codec, NULL);
02087             if (open_val < 0)
02088             {
02089                 LOG(VB_GENERAL, LOG_ERR, LOC +
02090                     QString("Could not open codec 0x%1, id(%2) type(%3) "
02091                             "aborting. reason %4").arg((uint64_t)enc,0,16)
02092                         .arg(ff_codec_id_string(enc->codec_id))
02093                         .arg(ff_codec_type_string(enc->codec_type))
02094                         .arg(open_val));
02095                 //av_close_input_file(ic); // causes segfault
02096                 ic = NULL;
02097                 scanerror = -1;
02098                 break;
02099             }
02100             else
02101             {
02102                 LOG(VB_GENERAL, LOG_INFO, LOC +
02103                     QString("Opened codec 0x%1, id(%2) type(%3)")
02104                         .arg((uint64_t)enc,0,16)
02105                         .arg(ff_codec_id_string(enc->codec_id))
02106                         .arg(ff_codec_type_string(enc->codec_type)));
02107             }
02108         }
02109 
02110         if (enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
02111         {
02112             bool forced = ic->streams[i]->disposition & AV_DISPOSITION_FORCED;
02113             int lang = GetSubtitleLanguage(subtitleStreamCount, i);
02114             int lang_indx = lang_sub_cnt[lang]++;
02115             subtitleStreamCount++;
02116 
02117             tracks[kTrackTypeSubtitle].push_back(
02118                 StreamInfo(i, lang, lang_indx, ic->streams[i]->id, 0, 0, false, false, forced));
02119 
02120             LOG(VB_PLAYBACK, LOG_INFO, LOC +
02121                 QString("Subtitle track #%1 is A/V stream #%2 "
02122                         "and is in the %3 language(%4).")
02123                     .arg(tracks[kTrackTypeSubtitle].size()).arg(i)
02124                     .arg(iso639_key_toName(lang)).arg(lang));
02125         }
02126 
02127         if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
02128         {
02129             int lang = GetAudioLanguage(audioStreamCount, i);
02130             int channels  = ic->streams[i]->codec->channels;
02131             int lang_indx = lang_aud_cnt[lang]++;
02132             audioStreamCount++;
02133             AudioTrackType type = kAudioTypeNormal;
02134 
02135             if (ic->streams[i]->codec->avcodec_dual_language)
02136             {
02137                 tracks[kTrackTypeAudio].push_back(
02138                     StreamInfo(i, lang, lang_indx, ic->streams[i]->id, channels,
02139                                false, false, false, type));
02140                 lang_indx = lang_aud_cnt[lang]++;
02141                 tracks[kTrackTypeAudio].push_back(
02142                     StreamInfo(i, lang, lang_indx, ic->streams[i]->id, channels,
02143                                true, false, false, type));
02144             }
02145             else
02146             {
02147                 int logical_stream_id;
02148                 if (ringBuffer && ringBuffer->IsDVD())
02149                 {
02150                     logical_stream_id =
02151                         ringBuffer->DVD()->GetAudioTrackNum(ic->streams[i]->id);
02152                     type = (AudioTrackType)(ringBuffer->DVD()->GetAudioTrackType(ic->streams[i]->id));
02153                 }
02154                 else
02155                     logical_stream_id = ic->streams[i]->id;
02156 
02157                 tracks[kTrackTypeAudio].push_back(
02158                    StreamInfo(i, lang, lang_indx, logical_stream_id, channels,
02159                               false, false, false, type));
02160             }
02161 
02162             LOG(VB_AUDIO, LOG_INFO, LOC +
02163                 QString("Audio Track #%1, with type %2 is A/V stream #%3 "
02164                         "and has %4 channels in the %5 language(%6).")
02165                     .arg(tracks[kTrackTypeAudio].size()).arg((int)type).arg(i)
02166                     .arg(enc->channels)
02167                     .arg(iso639_key_toName(lang)).arg(lang));
02168         }
02169     }
02170 
02171     if (bitrate > 0)
02172     {
02173         bitrate = (bitrate + 999) / 1000;
02174         if (ringBuffer)
02175             ringBuffer->UpdateRawBitrate(bitrate);
02176     }
02177 
02178     // update RingBuffer buffer size
02179     if (ringBuffer)
02180     {
02181         ringBuffer->SetBufferSizeFactors(unknownbitrate,
02182                             QString(ic->iformat->name).contains("matroska"));
02183     }
02184 
02185     PostProcessTracks();
02186 
02187     // Select a new track at the next opportunity.
02188     ResetTracks();
02189 
02190     // We have to do this here to avoid the NVP getting stuck
02191     // waiting on audio.
02192     if (m_audio->HasAudioIn() && tracks[kTrackTypeAudio].empty())
02193     {
02194         m_audio->SetAudioParams(FORMAT_NONE, -1, -1, CODEC_ID_NONE, -1, false);
02195         m_audio->ReinitAudio();
02196         if (ringBuffer && ringBuffer->IsDVD())
02197             audioIn = AudioInfo();
02198     }
02199 
02200     // if we don't have a video stream we still need to make sure some
02201     // video params are set properly
02202     if (selectedTrack[kTrackTypeVideo].av_stream_index == -1)
02203     {
02204         LOG(VB_PLAYBACK, LOG_INFO, LOC +
02205             QString("No video track found/selected."));
02206         QString tvformat = gCoreContext->GetSetting("TVFormat").toLower();
02207         if (tvformat == "ntsc" || tvformat == "ntsc-jp" ||
02208             tvformat == "pal-m" || tvformat == "atsc")
02209         {
02210             fps = 29.97;
02211             m_parent->SetVideoParams(-1, -1, 29.97);
02212         }
02213         else
02214         {
02215             fps = 25.0;
02216             m_parent->SetVideoParams(-1, -1, 25.0);
02217         }
02218     }
02219 
02220     if (m_parent->IsErrored())
02221         scanerror = -1;
02222 
02223     ScanDSMCCStreams();
02224 
02225     return scanerror;
02226 }
02227 
02228 void AvFormatDecoder::UpdateFramesPlayed(void)
02229 {
02230     return DecoderBase::UpdateFramesPlayed();
02231 }
02232 
02233 bool AvFormatDecoder::DoRewindSeek(long long desiredFrame)
02234 {
02235     return DecoderBase::DoRewindSeek(desiredFrame);
02236 }
02237 
02238 void AvFormatDecoder::DoFastForwardSeek(long long desiredFrame, bool &needflush)
02239 {
02240     DecoderBase::DoFastForwardSeek(desiredFrame, needflush);
02241     return;
02242 }
02243 
02245 int AvFormatDecoder::GetSubtitleLanguage(uint subtitle_index, uint stream_index)
02246 {
02247     (void)subtitle_index;
02248     AVDictionaryEntry *metatag =
02249         av_dict_get(ic->streams[stream_index]->metadata, "language", NULL, 0);
02250     return metatag ? get_canonical_lang(metatag->value) :
02251                      iso639_str3_to_key("und");
02252 }
02253 
02255 int AvFormatDecoder::GetCaptionLanguage(TrackTypes trackType, int service_num)
02256 {
02257     int ret = -1;
02258     for (uint i = 0; i < (uint) pmt_track_types.size(); i++)
02259     {
02260         if ((pmt_track_types[i] == trackType) &&
02261             (pmt_tracks[i].stream_id == service_num))
02262         {
02263             ret = pmt_tracks[i].language;
02264             if (!iso639_is_key_undefined(ret))
02265                 return ret;
02266         }
02267     }
02268 
02269     for (uint i = 0; i < (uint) stream_track_types.size(); i++)
02270     {
02271         if ((stream_track_types[i] == trackType) &&
02272             (stream_tracks[i].stream_id == service_num))
02273         {
02274             ret = stream_tracks[i].language;
02275             if (!iso639_is_key_undefined(ret))
02276                 return ret;
02277         }
02278     }
02279 
02280     return ret;
02281 }
02282 
02283 int AvFormatDecoder::GetAudioLanguage(uint audio_index, uint stream_index)
02284 {
02285     return GetSubtitleLanguage(audio_index, stream_index);
02286 }
02287 
02300 void AvFormatDecoder::SetupAudioStreamSubIndexes(int streamIndex)
02301 {
02302     QMutexLocker locker(avcodeclock);
02303 
02304     // Find the position of the streaminfo in tracks[kTrackTypeAudio]
02305     sinfo_vec_t::iterator current = tracks[kTrackTypeAudio].begin();
02306     for (; current != tracks[kTrackTypeAudio].end(); ++current)
02307     {
02308         if (current->av_stream_index == streamIndex)
02309             break;
02310     }
02311 
02312     if (current == tracks[kTrackTypeAudio].end())
02313     {
02314         LOG(VB_GENERAL, LOG_WARNING, LOC +
02315             QString("Invalid stream index passed to "
02316                     "SetupAudioStreamSubIndexes: %1").arg(streamIndex));
02317 
02318         return;
02319     }
02320 
02321     // Remove the extra substream or duplicate the current substream
02322     sinfo_vec_t::iterator next = current + 1;
02323     if (current->av_substream_index == -1)
02324     {
02325         // Split stream in two (Language I + Language II)
02326         StreamInfo lang1 = *current;
02327         StreamInfo lang2 = *current;
02328         lang1.av_substream_index = 0;
02329         lang2.av_substream_index = 1;
02330         *current = lang1;
02331         tracks[kTrackTypeAudio].insert(next, lang2);
02332         return;
02333     }
02334 
02335     if ((next == tracks[kTrackTypeAudio].end()) ||
02336         (next->av_stream_index != streamIndex))
02337     {
02338         QString msg = QString(
02339             "Expected substream 1 (Language I) of stream %1\n\t\t\t"
02340             "following substream 0, found end of list or another stream.")
02341             .arg(streamIndex);
02342 
02343         LOG(VB_GENERAL, LOG_WARNING, LOC + msg);
02344 
02345         return;
02346     }
02347 
02348     // Remove extra stream info
02349     StreamInfo stream = *current;
02350     stream.av_substream_index = -1;
02351     *current = stream;
02352     tracks[kTrackTypeAudio].erase(next);
02353 }
02354 
02355 int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic)
02356 {
02357     AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
02358 
02359     if (!IS_DR1_PIX_FMT(c->pix_fmt))
02360     {
02361         nd->directrendering = false;
02362         return avcodec_default_get_buffer(c, pic);
02363     }
02364     nd->directrendering = true;
02365 
02366     VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
02367 
02368     if (!frame)
02369         return -1;
02370 
02371     for (int i = 0; i < 3; i++)
02372     {
02373         pic->data[i]     = frame->buf + frame->offsets[i];
02374         pic->linesize[i] = frame->pitches[i];
02375     }
02376 
02377     pic->opaque = frame;
02378     pic->type = FF_BUFFER_TYPE_USER;
02379 
02380     pic->reordered_opaque = c->reordered_opaque;
02381 
02382     return 0;
02383 }
02384 
02389 void AvFormatDecoder::RemoveAudioStreams()
02390 {
02391     if (!m_audio->HasAudioIn())
02392         return;
02393 
02394     QMutexLocker locker(avcodeclock);
02395     for (uint i = 0; i < ic->nb_streams;)
02396     {
02397         AVStream *st = ic->streams[i];
02398         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
02399         {
02400             av_remove_stream(ic, st->id, 0);
02401             i--;
02402         }
02403         else
02404             i++;
02405     }
02406 }
02407 
02408 void release_avf_buffer(struct AVCodecContext *c, AVFrame *pic)
02409 {
02410     (void)c;
02411 
02412     if (pic->type == FF_BUFFER_TYPE_INTERNAL)
02413     {
02414         avcodec_default_release_buffer(c, pic);
02415         return;
02416     }
02417 
02418     AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
02419     if (nd && nd->GetPlayer())
02420         nd->GetPlayer()->DeLimboFrame((VideoFrame*)pic->opaque);
02421 
02422     assert(pic->type == FF_BUFFER_TYPE_USER);
02423 
02424     for (uint i = 0; i < 4; i++)
02425         pic->data[i] = NULL;
02426 }
02427 
02428 int get_avf_buffer_vdpau(struct AVCodecContext *c, AVFrame *pic)
02429 {
02430     AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
02431     VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
02432 
02433     pic->data[0] = frame->buf;
02434     pic->data[1] = frame->priv[0];
02435     pic->data[2] = frame->priv[1];
02436 
02437     pic->linesize[0] = 0;
02438     pic->linesize[1] = 0;
02439     pic->linesize[2] = 0;
02440 
02441     pic->opaque = frame;
02442     pic->type = FF_BUFFER_TYPE_USER;
02443 
02444     frame->pix_fmt = c->pix_fmt;
02445 
02446 #ifdef USING_VDPAU
02447     struct vdpau_render_state *render = (struct vdpau_render_state *)frame->buf;
02448     render->state |= FF_VDPAU_STATE_USED_FOR_REFERENCE;
02449 #endif
02450 
02451     pic->reordered_opaque = c->reordered_opaque;
02452 
02453     return 0;
02454 }
02455 
02456 void release_avf_buffer_vdpau(struct AVCodecContext *c, AVFrame *pic)
02457 {
02458     assert(pic->type == FF_BUFFER_TYPE_USER);
02459 
02460 #ifdef USING_VDPAU
02461     struct vdpau_render_state *render = (struct vdpau_render_state *)pic->data[0];
02462     render->state &= ~FF_VDPAU_STATE_USED_FOR_REFERENCE;
02463 #endif
02464 
02465     AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
02466     if (nd && nd->GetPlayer())
02467         nd->GetPlayer()->DeLimboFrame((VideoFrame*)pic->opaque);
02468 
02469     for (uint i = 0; i < 4; i++)
02470         pic->data[i] = NULL;
02471 }
02472 
02473 void render_slice_vdpau(struct AVCodecContext *s, const AVFrame *src,
02474                         int offset[4], int y, int type, int height)
02475 {
02476     if (!src)
02477         return;
02478 
02479     (void)offset;
02480     (void)type;
02481 
02482     if (s && src && s->opaque && src->opaque)
02483     {
02484         AvFormatDecoder *nd = (AvFormatDecoder *)(s->opaque);
02485 
02486         int width = s->width;
02487 
02488         VideoFrame *frame = (VideoFrame *)src->opaque;
02489         nd->GetPlayer()->DrawSlice(frame, 0, y, width, height);
02490     }
02491     else
02492     {
02493         LOG(VB_GENERAL, LOG_ERR, LOC +
02494             "render_slice_vdpau called with bad avctx or src");
02495     }
02496 }
02497 
02498 int get_avf_buffer_dxva2(struct AVCodecContext *c, AVFrame *pic)
02499 {
02500     AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
02501     VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
02502     for (int i = 0; i < 4; i++)
02503     {
02504         pic->data[i]     = NULL;
02505         pic->linesize[i] = 0;
02506     }
02507     pic->reordered_opaque = c->reordered_opaque;
02508     pic->opaque      = frame;
02509     pic->type        = FF_BUFFER_TYPE_USER;
02510     frame->pix_fmt   = c->pix_fmt;
02511 
02512 #ifdef USING_DXVA2
02513     if (nd->GetPlayer())
02514     {
02515         static uint8_t *dummy[1] = { 0 };
02516         c->hwaccel_context =
02517             (dxva_context*)nd->GetPlayer()->GetDecoderContext(NULL, dummy[0]);
02518         pic->data[0] = (uint8_t*)frame->buf;
02519         pic->data[3] = (uint8_t*)frame->buf;
02520     }
02521 #endif
02522 
02523     return 0;
02524 }
02525 
02526 int get_avf_buffer_vaapi(struct AVCodecContext *c, AVFrame *pic)
02527 {
02528     AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
02529     VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
02530 
02531     pic->data[0]     = frame->buf;
02532     pic->data[1]     = NULL;
02533     pic->data[2]     = NULL;
02534     pic->data[3]     = NULL;
02535     pic->linesize[0] = 0;
02536     pic->linesize[1] = 0;
02537     pic->linesize[2] = 0;
02538     pic->linesize[3] = 0;
02539     pic->opaque      = frame;
02540     pic->type        = FF_BUFFER_TYPE_USER;
02541     frame->pix_fmt   = c->pix_fmt;
02542 
02543 #ifdef USING_VAAPI
02544     if (nd->GetPlayer())
02545         c->hwaccel_context = (vaapi_context*)nd->GetPlayer()->GetDecoderContext(frame->buf, pic->data[3]);
02546 #endif
02547 
02548     return 0;
02549 }
02550 
02551 void AvFormatDecoder::DecodeDTVCC(const uint8_t *buf, uint len, bool scte)
02552 {
02553     if (!len)
02554         return;
02555 
02556     // closed caption data
02557     //cc_data() {
02558     // reserved                1 0.0   1
02559     // process_cc_data_flag    1 0.1   bslbf
02560     bool process_cc_data = buf[0] & 0x40;
02561     if (!process_cc_data)
02562         return; // early exit if process_cc_data_flag false
02563 
02564     // additional_data_flag    1 0.2   bslbf
02565     //bool additional_data = buf[0] & 0x20;
02566     // cc_count                5 0.3   uimsbf
02567     uint cc_count = buf[0] & 0x1f;
02568     // em_data                 8 1.0
02569 
02570     if (len < 2+(3*cc_count))
02571         return;
02572 
02573     bool had_608 = false, had_708 = false;
02574     for (uint cur = 0; cur < cc_count; cur++)
02575     {
02576         uint cc_code  = buf[2+(cur*3)];
02577         bool cc_valid = cc_code & 0x04;
02578 
02579         uint data1    = buf[3+(cur*3)];
02580         uint data2    = buf[4+(cur*3)];
02581         uint data     = (data2 << 8) | data1;
02582         uint cc_type  = cc_code & 0x03;
02583         uint field;
02584 
02585         if (!cc_valid)
02586         {
02587             if (cc_type >= 0x2)
02588                 ccd708->decode_cc_null();
02589             continue;
02590         }
02591 
02592         if (scte || cc_type <= 0x1) // EIA-608 field-1/2
02593         {
02594             if (cc_type == 0x2)
02595             {
02596                 // SCTE repeated field
02597                 field = !last_scte_field;
02598                 invert_scte_field = !invert_scte_field;
02599             }
02600             else
02601             {
02602                 field = cc_type ^ invert_scte_field;
02603             }
02604 
02605             if (cc608_good_parity(cc608_parity_table, data))
02606             {
02607                 // in film mode, we may start at the wrong field;
02608                 // correct if XDS start/cont/end code is detected
02609                 // (must be field 2)
02610                 if (scte && field == 0 &&
02611                     (data1 & 0x7f) <= 0x0f && (data1 & 0x7f) != 0x00)
02612                 {
02613                     if (cc_type == 1)
02614                         invert_scte_field = 0;
02615                     field = 1;
02616 
02617                     // flush decoder
02618                     ccd608->FormatCC(0, -1, -1);
02619                 }
02620 
02621                 had_608 = true;
02622                 ccd608->FormatCCField(lastccptsu / 1000, field, data);
02623 
02624                 last_scte_field = field;
02625             }
02626         }
02627         else
02628         {
02629             had_708 = true;
02630             ccd708->decode_cc_data(cc_type, data1, data2);
02631         }
02632     }
02633     UpdateCaptionTracksFromStreams(had_608, had_708);
02634 }
02635 
02636 void AvFormatDecoder::UpdateCaptionTracksFromStreams(
02637     bool check_608, bool check_708)
02638 {
02639     bool need_change_608 = false;
02640     bool seen_608[4];
02641     if (check_608)
02642     {
02643         ccd608->GetServices(15/*seconds*/, seen_608);
02644         for (uint i = 0; i < 4; i++)
02645         {
02646             need_change_608 |= (seen_608[i] && !ccX08_in_tracks[i]) ||
02647                 (!seen_608[i] && ccX08_in_tracks[i] && !ccX08_in_pmt[i]);
02648         }
02649     }
02650 
02651     bool need_change_708 = false;
02652     bool seen_708[64];
02653     if (check_708 || need_change_608)
02654     {
02655         ccd708->services(15/*seconds*/, seen_708);
02656         for (uint i = 1; i < 64 && !need_change_608 && !need_change_708; i++)
02657         {
02658             need_change_708 |= (seen_708[i] && !ccX08_in_tracks[i+4]) ||
02659                 (!seen_708[i] && ccX08_in_tracks[i+4] && !ccX08_in_pmt[i+4]);
02660         }
02661         if (need_change_708 && !check_608)
02662             ccd608->GetServices(15/*seconds*/, seen_608);
02663     }
02664 
02665     if (!need_change_608 && !need_change_708)
02666         return;
02667 
02668     ScanATSCCaptionStreams(selectedTrack[kTrackTypeVideo].av_stream_index);
02669 
02670     stream_tracks.clear();
02671     stream_track_types.clear();
02672     int av_index = selectedTrack[kTrackTypeVideo].av_stream_index;
02673     int lang = iso639_str3_to_key("und");
02674     for (uint i = 1; i < 64; i++)
02675     {
02676         if (seen_708[i] && !ccX08_in_pmt[i+4])
02677         {
02678             StreamInfo si(av_index, lang, 0/*lang_idx*/,
02679                           i, false/*easy*/, true/*wide*/);
02680             stream_tracks.push_back(si);
02681             stream_track_types.push_back(kTrackTypeCC708);
02682         }
02683     }
02684     for (uint i = 0; i < 4; i++)
02685     {
02686         if (seen_608[i] && !ccX08_in_pmt[i])
02687         {
02688             if (0==i)
02689                 lang = GetCaptionLanguage(kTrackTypeCC708, 1);
02690             else if (2==i)
02691                 lang = GetCaptionLanguage(kTrackTypeCC708, 2);
02692             else
02693                 lang = iso639_str3_to_key("und");
02694 
02695             StreamInfo si(av_index, lang, 0/*lang_idx*/,
02696                           i+1, false/*easy*/, false/*wide*/);
02697             stream_tracks.push_back(si);
02698             stream_track_types.push_back(kTrackTypeCC608);
02699         }
02700     }
02701     UpdateATSCCaptionTracks();
02702 }
02703 
02704 void AvFormatDecoder::HandleGopStart(
02705     AVPacket *pkt, bool can_reliably_parse_keyframes)
02706 {
02707     if (prevgoppos != 0 && keyframedist != 1)
02708     {
02709         int tempKeyFrameDist = framesRead - 1 - prevgoppos;
02710         bool reset_kfd = false;
02711 
02712         if (!gopset || livetv) // gopset: we've seen 2 keyframes
02713         {
02714             LOG(VB_PLAYBACK, LOG_INFO, LOC +
02715                 "gopset not set, syncing positionMap");
02716             SyncPositionMap();
02717             if (tempKeyFrameDist > 0 && !livetv)
02718             {
02719                 LOG(VB_PLAYBACK, LOG_INFO, LOC +
02720                     QString("Initial key frame distance: %1.")
02721                         .arg(keyframedist));
02722                 gopset       = true;
02723                 reset_kfd    = true;
02724             }
02725         }
02726         else if (keyframedist != tempKeyFrameDist && tempKeyFrameDist > 0)
02727         {
02728             LOG(VB_PLAYBACK, LOG_INFO, LOC +
02729                 QString("Key frame distance changed from %1 to %2.")
02730                     .arg(keyframedist).arg(tempKeyFrameDist));
02731             reset_kfd = true;
02732         }
02733 
02734         if (reset_kfd)
02735         {
02736             keyframedist    = tempKeyFrameDist;
02737             maxkeyframedist = max(keyframedist, maxkeyframedist);
02738 
02739             m_parent->SetKeyframeDistance(keyframedist);
02740 
02741 #if 0
02742             // also reset length
02743             QMutexLocker locker(&m_positionMapLock);
02744             if (!m_positionMap.empty())
02745             {
02746                 long long index       = m_positionMap.back().index;
02747                 long long totframes   = index * keyframedist;
02748                 uint length = (uint)((totframes * 1.0f) / fps);
02749                 m_parent->SetFileLength(length, totframes);
02750             }
02751 #endif
02752         }
02753     }
02754 
02755     lastKey = prevgoppos = framesRead - 1;
02756 
02757     if (can_reliably_parse_keyframes &&
02758         !hasFullPositionMap && !livetv && !watchingrecording)
02759     {
02760         long long last_frame = 0;
02761         {
02762             QMutexLocker locker(&m_positionMapLock);
02763             if (!m_positionMap.empty())
02764                 last_frame = m_positionMap.back().index;
02765         }
02766 
02767 #if 0
02768         LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
02769             QString("framesRead: %1 last_frame: %2 keyframedist: %3")
02770                 .arg(framesRead) .arg(last_frame) .arg(keyframedist));
02771 #endif
02772 
02773         // if we don't have an entry, fill it in with what we've just parsed
02774         if (framesRead > last_frame && keyframedist > 0)
02775         {
02776             long long startpos = pkt->pos;
02777 
02778             LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
02779                 QString("positionMap[ %1 ] == %2.")
02780                     .arg(framesRead).arg(startpos));
02781 
02782             PosMapEntry entry = {framesRead, framesRead, startpos};
02783 
02784             QMutexLocker locker(&m_positionMapLock);
02785             m_positionMap.push_back(entry);
02786         }
02787 
02788 #if 0
02789         // If we are > 150 frames in and saw no positionmap at all, reset
02790         // length based on the actual bitrate seen so far
02791         if (framesRead > 150 && !recordingHasPositionMap && !livetv)
02792         {
02793             bitrate = (int)((pkt->pos * 8 * fps) / (framesRead - 1));
02794             float bytespersec = (float)bitrate / 8;
02795             float secs = ringBuffer->GetRealFileSize() * 1.0 / bytespersec;
02796             m_parent->SetFileLength((int)(secs), (int)(secs * fps));
02797         }
02798 #endif
02799     }
02800 }
02801 
02802 #define SEQ_START     0x000001b3
02803 #define GOP_START     0x000001b8
02804 #define PICTURE_START 0x00000100
02805 #define SLICE_MIN     0x00000101
02806 #define SLICE_MAX     0x000001af
02807 #define SEQ_END_CODE  0x000001b7
02808 
02809 void AvFormatDecoder::MpegPreProcessPkt(AVStream *stream, AVPacket *pkt)
02810 {
02811     AVCodecContext *context = stream->codec;
02812     const uint8_t *bufptr = pkt->data;
02813     const uint8_t *bufend = pkt->data + pkt->size;
02814 
02815     while (bufptr < bufend)
02816     {
02817         bufptr = avpriv_mpv_find_start_code(bufptr, bufend, &start_code_state);
02818 
02819         float aspect_override = -1.0f;
02820         if (ringBuffer->IsDVD())
02821         {
02822             if (start_code_state == SEQ_END_CODE)
02823                 ringBuffer->DVD()->NewSequence(true);
02824             aspect_override = ringBuffer->DVD()->GetAspectOverride();
02825         }
02826 
02827         if (start_code_state >= SLICE_MIN && start_code_state <= SLICE_MAX)
02828             continue;
02829         else if (SEQ_START == start_code_state)
02830         {
02831             if (bufptr + 11 >= pkt->data + pkt->size)
02832                 continue; // not enough valid data...
02833             SequenceHeader *seq = reinterpret_cast<SequenceHeader*>(
02834                 const_cast<uint8_t*>(bufptr));
02835 
02836             uint  width  = seq->width()  >> context->lowres;
02837             uint  height = seq->height() >> context->lowres;
02838             current_aspect = seq->aspect(context->codec_id ==
02839                                          CODEC_ID_MPEG1VIDEO);
02840             if (aspect_override > 0.0f)
02841                 current_aspect = aspect_override;
02842             float seqFPS = seq->fps();
02843 
02844             bool changed = (seqFPS > fps+0.01f) || (seqFPS < fps-0.01f);
02845             changed |= (width  != (uint)current_width );
02846             changed |= (height != (uint)current_height);
02847 
02848             if (changed)
02849             {
02850                 m_parent->SetVideoParams(width, height, seqFPS, kScan_Detect);
02851 
02852                 current_width  = width;
02853                 current_height = height;
02854                 fps            = seqFPS;
02855 
02856                 if (private_dec)
02857                     private_dec->Reset();
02858 
02859                 gopset = false;
02860                 prevgoppos = 0;
02861                 firstvpts = lastapts = lastvpts = lastccptsu = 0;
02862                 firstvptsinuse = true;
02863                 faulty_pts = faulty_dts = 0;
02864                 last_pts_for_fault_detection = 0;
02865                 last_dts_for_fault_detection = 0;
02866                 pts_detected = false;
02867                 reordered_pts_detected = false;
02868 
02869                 // fps debugging info
02870                 float avFPS = normalized_fps(stream, context);
02871                 if ((seqFPS > avFPS+0.01f) || (seqFPS < avFPS-0.01f))
02872                 {
02873                     LOG(VB_PLAYBACK, LOG_INFO, LOC +
02874                         QString("avFPS(%1) != seqFPS(%2)")
02875                             .arg(avFPS).arg(seqFPS));
02876                 }
02877             }
02878 
02879             seq_count++;
02880 
02881             if (!seen_gop && seq_count > 1)
02882             {
02883                 HandleGopStart(pkt, true);
02884                 pkt->flags |= AV_PKT_FLAG_KEY;
02885             }
02886         }
02887         else if (GOP_START == start_code_state)
02888         {
02889             HandleGopStart(pkt, true);
02890             seen_gop = true;
02891             pkt->flags |= AV_PKT_FLAG_KEY;
02892         }
02893     }
02894 }
02895 
02896 bool AvFormatDecoder::H264PreProcessPkt(AVStream *stream, AVPacket *pkt)
02897 {
02898     AVCodecContext *context = stream->codec;
02899     const uint8_t  *buf     = pkt->data;
02900     const uint8_t  *buf_end = pkt->data + pkt->size;
02901     bool on_frame = false;
02902 
02903     // crude NAL unit vs Annex B detection.
02904     // the parser only understands Annex B
02905     if (context->extradata && context->extradata_size >= 4)
02906     {
02907         int nal_size    = 0;
02908         int size_length = (context->extradata[4] & 0x3) + 1;
02909 
02910         for (int i = 0; i < size_length; i++)
02911             nal_size += buf[i];
02912 
02913         if (nal_size)
02914         {
02915             if (pkt->flags & AV_PKT_FLAG_KEY)
02916                 HandleGopStart(pkt, false);
02917             return true;
02918         }
02919     }
02920 
02921     while (buf < buf_end)
02922     {
02923         buf += m_h264_parser->addBytes(buf, buf_end - buf, 0);
02924 
02925         if (m_h264_parser->stateChanged())
02926         {
02927             if (m_h264_parser->FieldType() != H264Parser::FIELD_BOTTOM)
02928             {
02929                 if (m_h264_parser->onFrameStart())
02930                     on_frame = true;
02931 
02932                 if (!m_h264_parser->onKeyFrameStart())
02933                     continue;
02934             }
02935             else
02936             {
02937                 continue;
02938             }
02939         }
02940         else
02941         {
02942             continue;
02943         }
02944 
02945         current_aspect = get_aspect(*context);
02946         QSize dim    = get_video_dim(*context);
02947         uint  width  = dim.width();
02948         uint  height = dim.height();
02949         float seqFPS = normalized_fps(stream, context);
02950 
02951         bool changed = (seqFPS > fps+0.01f) || (seqFPS < fps-0.01f);
02952         changed |= (width  != (uint)current_width );
02953         changed |= (height != (uint)current_height);
02954 
02955         if (changed)
02956         {
02957             m_parent->SetVideoParams(width, height, seqFPS, kScan_Detect);
02958 
02959             current_width  = width;
02960             current_height = height;
02961             fps            = seqFPS;
02962 
02963             gopset = false;
02964             prevgoppos = 0;
02965             firstvpts = lastapts = lastvpts = lastccptsu = 0;
02966             firstvptsinuse = true;
02967             faulty_pts = faulty_dts = 0;
02968             last_pts_for_fault_detection = 0;
02969             last_dts_for_fault_detection = 0;
02970             pts_detected = false;
02971             reordered_pts_detected = false;
02972 
02973             // fps debugging info
02974             float avFPS = normalized_fps(stream, context);
02975             if ((seqFPS > avFPS+0.01f) || (seqFPS < avFPS-0.01f))
02976             {
02977                 LOG(VB_PLAYBACK, LOG_INFO, LOC +
02978                     QString("avFPS(%1) != seqFPS(%2)")
02979                         .arg(avFPS).arg(seqFPS));
02980             }
02981         }
02982 
02983         HandleGopStart(pkt, true);
02984         pkt->flags |= AV_PKT_FLAG_KEY;
02985     }
02986 
02987     return on_frame;
02988 }
02989 
02990 bool AvFormatDecoder::PreProcessVideoPacket(AVStream *curstream, AVPacket *pkt)
02991 {
02992     AVCodecContext *context = curstream->codec;
02993     bool on_frame = true;
02994 
02995     if (CODEC_IS_FFMPEG_MPEG(context->codec_id))
02996     {
02997         MpegPreProcessPkt(curstream, pkt);
02998     }
02999     else if (CODEC_IS_H264(context->codec_id))
03000     {
03001         on_frame = H264PreProcessPkt(curstream, pkt);
03002     }
03003     else
03004     {
03005         if (pkt->flags & AV_PKT_FLAG_KEY)
03006         {
03007             HandleGopStart(pkt, false);
03008             seen_gop = true;
03009         }
03010         else
03011         {
03012             seq_count++;
03013             if (!seen_gop && seq_count > 1)
03014             {
03015                 HandleGopStart(pkt, false);
03016             }
03017         }
03018     }
03019 
03020     if (framesRead == 0 && !justAfterChange &&
03021         !(pkt->flags & AV_PKT_FLAG_KEY))
03022     {
03023         av_free_packet(pkt);
03024         return false;
03025     }
03026 
03027     if (on_frame)
03028         framesRead++;
03029 
03030     totalDuration += av_q2d(curstream->time_base) * pkt->duration * 1000000; // usec
03031 
03032     justAfterChange = false;
03033 
03034     if (exitafterdecoded)
03035         gotVideoFrame = 1;
03036 
03037     return true;
03038 }
03039 
03040 bool AvFormatDecoder::ProcessVideoPacket(AVStream *curstream, AVPacket *pkt)
03041 {
03042     int ret = 0, gotpicture = 0;
03043     int64_t pts = 0;
03044     AVCodecContext *context = curstream->codec;
03045     AVFrame mpa_pic;
03046     avcodec_get_frame_defaults(&mpa_pic);
03047     mpa_pic.reordered_opaque = AV_NOPTS_VALUE;
03048 
03049     if (pkt->pts != (int64_t)AV_NOPTS_VALUE)
03050         pts_detected = true;
03051 
03052     avcodeclock->lock();
03053     if (private_dec)
03054     {
03055         if (QString(ic->iformat->name).contains("avi") || !pts_detected)
03056             pkt->pts = pkt->dts;
03057         // TODO disallow private decoders for dvd playback
03058         // N.B. we do not reparse the frame as it breaks playback for
03059         // everything but libmpeg2
03060         ret = private_dec->GetFrame(curstream, &mpa_pic, &gotpicture, pkt);
03061     }
03062     else
03063     {
03064         context->reordered_opaque = pkt->pts;
03065         ret = avcodec_decode_video2(context, &mpa_pic, &gotpicture, pkt);
03066         // Reparse it to not drop the DVD still frame
03067         if (ringBuffer->IsDVD() && ringBuffer->DVD()->NeedsStillFrame())
03068             ret = avcodec_decode_video2(context, &mpa_pic, &gotpicture, pkt);
03069     }
03070     avcodeclock->unlock();
03071 
03072     if (ret < 0)
03073     {
03074         LOG(VB_GENERAL, LOG_ERR, LOC + "Unknown decoding error");
03075         return false;
03076     }
03077 
03078     if (!gotpicture)
03079     {
03080         return true;
03081     }
03082 
03083     // Detect faulty video timestamps using logic from ffplay.
03084     if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
03085     {
03086         faulty_dts += (pkt->dts <= last_dts_for_fault_detection);
03087         last_dts_for_fault_detection = pkt->dts;
03088     }
03089     if (mpa_pic.reordered_opaque != (int64_t)AV_NOPTS_VALUE)
03090     {
03091         faulty_pts += (mpa_pic.reordered_opaque <= last_pts_for_fault_detection);
03092         last_pts_for_fault_detection = mpa_pic.reordered_opaque;
03093         reordered_pts_detected = true;
03094     }
03095 
03096     // Explicity use DTS for DVD since they should always be valid for every
03097     // frame and fixups aren't enabled for DVD.
03098     // Select reordered_opaque (PTS) timestamps if they are less faulty or the
03099     // the DTS timestamp is missing. Also use fixups for missing PTS instead of
03100     // DTS to avoid oscillating between PTS and DTS. Only select DTS if PTS is
03101     // more faulty or never detected.
03102     if (force_dts_timestamps)
03103     {
03104         if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
03105             pts = pkt->dts;
03106         pts_selected = false;
03107     }
03108     else if (ringBuffer->IsDVD())
03109     {
03110         if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
03111             pts = pkt->dts;
03112         pts_selected = false;
03113     }
03114     else if (private_dec && private_dec->NeedsReorderedPTS() &&
03115              mpa_pic.reordered_opaque != (int64_t)AV_NOPTS_VALUE)
03116     {
03117         pts = mpa_pic.reordered_opaque;
03118         pts_selected = true;
03119     }
03120     else if (faulty_pts <= faulty_dts && reordered_pts_detected)
03121     {
03122         if (mpa_pic.reordered_opaque != (int64_t)AV_NOPTS_VALUE)
03123             pts = mpa_pic.reordered_opaque;
03124         pts_selected = true;
03125     }
03126     else if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
03127     {
03128         pts = pkt->dts;
03129         pts_selected = false;
03130     }
03131 
03132     LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_DEBUG, LOC +
03133         QString("video packet timestamps reordered %1 pts %2 dts %3 (%4)")
03134             .arg(mpa_pic.reordered_opaque).arg(pkt->pts).arg(pkt->dts)
03135             .arg((force_dts_timestamps) ? "dts forced" :
03136                  (pts_selected) ? "reordered" : "dts"));
03137 
03138     mpa_pic.reordered_opaque = pts;
03139 
03140     ProcessVideoFrame(curstream, &mpa_pic);
03141 
03142     return true;
03143 }
03144 
03145 bool AvFormatDecoder::ProcessVideoFrame(AVStream *stream, AVFrame *mpa_pic)
03146 {
03147     AVCodecContext *context = stream->codec;
03148 
03149     uint cc_len = (uint) max(mpa_pic->scte_cc_len,0);
03150     uint8_t *cc_buf = mpa_pic->scte_cc_buf;
03151     bool scte = true;
03152 
03153     // If both ATSC and SCTE caption data are available, prefer ATSC
03154     if ((mpa_pic->atsc_cc_len > 0) || ignore_scte)
03155     {
03156         ignore_scte = true;
03157         cc_len = (uint) max(mpa_pic->atsc_cc_len, 0);
03158         cc_buf = mpa_pic->atsc_cc_buf;
03159         scte = false;
03160     }
03161 
03162     // Decode CEA-608 and CEA-708 captions
03163     for (uint i = 0; i < cc_len; i += ((cc_buf[i] & 0x1f) * 3) + 2)
03164         DecodeDTVCC(cc_buf + i, cc_len - i, scte);
03165 
03166     VideoFrame *picframe = (VideoFrame *)(mpa_pic->opaque);
03167 
03168     if (FlagIsSet(kDecodeNoDecode))
03169     {
03170         // Do nothing, we just want the pts, captions, subtites, etc.
03171         // So we can release the unconverted blank video frame to the
03172         // display queue.
03173     }
03174     else if (!directrendering)
03175     {
03176         AVPicture tmppicture;
03177 
03178         VideoFrame *xf = picframe;
03179         picframe = m_parent->GetNextVideoFrame();
03180 
03181         unsigned char *buf = picframe->buf;
03182         avpicture_fill(&tmppicture, buf, PIX_FMT_YUV420P, context->width,
03183                        context->height);
03184         tmppicture.data[0] = buf + picframe->offsets[0];
03185         tmppicture.data[1] = buf + picframe->offsets[1];
03186         tmppicture.data[2] = buf + picframe->offsets[2];
03187         tmppicture.linesize[0] = picframe->pitches[0];
03188         tmppicture.linesize[1] = picframe->pitches[1];
03189         tmppicture.linesize[2] = picframe->pitches[2];
03190 
03191         QSize dim = get_video_dim(*context);
03192         sws_ctx = sws_getCachedContext(sws_ctx, context->width,
03193                                        context->height, context->pix_fmt,
03194                                        context->width, context->height,
03195                                        PIX_FMT_YUV420P, SWS_FAST_BILINEAR,
03196                                        NULL, NULL, NULL);
03197         if (!sws_ctx)
03198         {
03199             LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to allocate sws context");
03200             return false;
03201         }
03202         sws_scale(sws_ctx, mpa_pic->data, mpa_pic->linesize, 0, dim.height(),
03203                   tmppicture.data, tmppicture.linesize);
03204 
03205         if (xf)
03206         {
03207             // Set the frame flags, but then discard it
03208             // since we are not using it for display.
03209             xf->interlaced_frame = mpa_pic->interlaced_frame;
03210             xf->top_field_first = mpa_pic->top_field_first;
03211             xf->frameNumber = framesPlayed;
03212             xf->aspect = current_aspect;
03213             m_parent->DiscardVideoFrame(xf);
03214         }
03215     }
03216     else if (!picframe)
03217     {
03218         LOG(VB_GENERAL, LOG_ERR, LOC + "NULL videoframe - direct rendering not"
03219                                        "correctly initialized.");
03220         return false;
03221     }
03222 
03223     long long pts = (long long)(av_q2d(stream->time_base) *
03224                                 mpa_pic->reordered_opaque * 1000);
03225 
03226     long long temppts = pts;
03227     // Validate the video pts against the last pts. If it's
03228     // a little bit smaller, equal or missing, compute
03229     // it from the last. Otherwise assume a wraparound.
03230     if (!ringBuffer->IsDVD() &&
03231         temppts <= lastvpts &&
03232         (temppts + (1000 / fps) > lastvpts || temppts <= 0))
03233     {
03234         temppts = lastvpts;
03235         temppts += (long long)(1000 / fps);
03236         // MPEG2/H264 frames can be repeated, update pts accordingly
03237         temppts += (long long)(mpa_pic->repeat_pict * 500 / fps);
03238     }
03239 
03240     LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
03241         QString("video timecode %1 %2 %3 %4%5")
03242             .arg(mpa_pic->reordered_opaque).arg(pts).arg(temppts).arg(lastvpts)
03243             .arg((pts != temppts) ? " fixup" : ""));
03244 
03245     picframe->interlaced_frame = mpa_pic->interlaced_frame;
03246     picframe->top_field_first  = mpa_pic->top_field_first;
03247     picframe->repeat_pict      = mpa_pic->repeat_pict;
03248     picframe->disp_timecode    = NormalizeVideoTimecode(stream, temppts);
03249     picframe->frameNumber      = framesPlayed;
03250     picframe->aspect           = current_aspect;
03251     picframe->dummy            = 0;
03252 
03253     m_parent->ReleaseNextVideoFrame(picframe, temppts);
03254     if (private_dec)
03255         context->release_buffer(context, mpa_pic);
03256 
03257     decoded_video_frame = picframe;
03258     gotVideoFrame = 1;
03259     framesPlayed++;
03260 
03261     lastvpts = temppts;
03262     if (!firstvpts && firstvptsinuse)
03263         firstvpts = temppts;
03264 
03265     return true;
03266 }
03267 
03273 void AvFormatDecoder::ProcessVBIDataPacket(
03274     const AVStream *stream, const AVPacket *pkt)
03275 {
03276     (void) stream;
03277 
03278     const uint8_t *buf     = pkt->data;
03279     uint64_t linemask      = 0;
03280     unsigned long long utc = lastccptsu;
03281 
03282     // [i]tv0 means there is a linemask
03283     // [I]TV0 means there is no linemask and all lines are present
03284     if ((buf[0]=='t') && (buf[1]=='v') && (buf[2] == '0'))
03285     {
03287         memcpy(&linemask, buf + 3, 8);
03288         buf += 11;
03289     }
03290     else if ((buf[0]=='T') && (buf[1]=='V') && (buf[2] == '0'))
03291     {
03292         linemask = 0xffffffffffffffffLL;
03293         buf += 3;
03294     }
03295     else
03296     {
03297         LOG(VB_VBI, LOG_ERR, LOC + QString("Unknown VBI data stream '%1%2%3'")
03298                 .arg(QChar(buf[0])).arg(QChar(buf[1])).arg(QChar(buf[2])));
03299         return;
03300     }
03301 
03302     static const uint min_blank = 6;
03303     for (uint i = 0; i < 36; i++)
03304     {
03305         if (!((linemask >> i) & 0x1))
03306             continue;
03307 
03308         const uint line  = ((i < 18) ? i : i-18) + min_blank;
03309         const uint field = (i<18) ? 0 : 1;
03310         const uint id2 = *buf & 0xf;
03311         switch (id2)
03312         {
03313             case VBI_TYPE_TELETEXT:
03314                 // SECAM lines  6-23
03315                 // PAL   lines  6-22
03316                 // NTSC  lines 10-21 (rare)
03317                 if (tracks[kTrackTypeTeletextMenu].empty())
03318                 {
03319                     StreamInfo si(pkt->stream_index, 0, 0, 0, 0);
03320                     tracks[kTrackTypeTeletextMenu].push_back(si);
03321                 }
03322                 ttd->Decode(buf+1, VBI_IVTV);
03323                 break;
03324             case VBI_TYPE_CC:
03325                 // PAL   line 22 (rare)
03326                 // NTSC  line 21
03327                 if (21 == line)
03328                 {
03329                     int data = (buf[2] << 8) | buf[1];
03330                     if (cc608_good_parity(cc608_parity_table, data))
03331                         ccd608->FormatCCField(utc/1000, field, data);
03332                     utc += 33367;
03333                 }
03334                 break;
03335             case VBI_TYPE_VPS: // Video Programming System
03336                 // PAL   line 16
03337                 ccd608->DecodeVPS(buf+1); // a.k.a. PDC
03338                 break;
03339             case VBI_TYPE_WSS: // Wide Screen Signal
03340                 // PAL   line 23
03341                 // NTSC  line 20
03342                 ccd608->DecodeWSS(buf+1);
03343                 break;
03344         }
03345         buf += 43;
03346     }
03347     lastccptsu = utc;
03348     UpdateCaptionTracksFromStreams(true, false);
03349 }
03350 
03355 void AvFormatDecoder::ProcessDVBDataPacket(
03356     const AVStream*, const AVPacket *pkt)
03357 {
03358     const uint8_t *buf     = pkt->data;
03359     const uint8_t *buf_end = pkt->data + pkt->size;
03360 
03361 
03362     while (buf < buf_end)
03363     {
03364         if (*buf == 0x10)
03365         {
03366             buf++; // skip
03367         }
03368         else if (*buf == 0x02)
03369         {
03370             buf += 4;
03371             if ((buf_end - buf) >= 42)
03372                 ttd->Decode(buf, VBI_DVB);
03373             buf += 42;
03374         }
03375         else if (*buf == 0x03)
03376         {
03377             buf += 4;
03378             if ((buf_end - buf) >= 42)
03379                 ttd->Decode(buf, VBI_DVB_SUBTITLE);
03380             buf += 42;
03381         }
03382         else if (*buf == 0xff)
03383         {
03384             buf += 3;
03385         }
03386         else
03387         {
03388             LOG(VB_VBI, LOG_ERR, LOC +
03389                 QString("VBI: Unknown descriptor: %1").arg(*buf));
03390             buf += 46;
03391         }
03392     }
03393 }
03394 
03398 void AvFormatDecoder::ProcessDSMCCPacket(
03399     const AVStream *str, const AVPacket *pkt)
03400 {
03401 #ifdef USING_MHEG
03402     if (!itv && ! (itv = m_parent->GetInteractiveTV()))
03403         return;
03404 
03405     // The packet may contain several tables.
03406     uint8_t *data = pkt->data;
03407     int length = pkt->size;
03408     int componentTag, dataBroadcastId;
03409     unsigned carouselId;
03410     {
03411         QMutexLocker locker(avcodeclock);
03412         componentTag    = str->component_tag;
03413         dataBroadcastId = str->codec->flags;
03414         carouselId      = (unsigned) str->codec->sub_id;
03415     }
03416     while (length > 3)
03417     {
03418         uint16_t sectionLen = (((data[1] & 0xF) << 8) | data[2]) + 3;
03419 
03420         if (sectionLen > length) // This may well be filler
03421             return;
03422 
03423         itv->ProcessDSMCCSection(data, sectionLen,
03424                                  componentTag, carouselId,
03425                                  dataBroadcastId);
03426         length -= sectionLen;
03427         data += sectionLen;
03428     }
03429 #endif // USING_MHEG
03430 }
03431 
03432 bool AvFormatDecoder::ProcessSubtitlePacket(AVStream *curstream, AVPacket *pkt)
03433 {
03434     if (!m_parent->GetSubReader(pkt->stream_index))
03435         return true;
03436 
03437     long long pts = 0;
03438 
03439     if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
03440         pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000);
03441 
03442     avcodeclock->lock();
03443     int subIdx = selectedTrack[kTrackTypeSubtitle].av_stream_index;
03444     avcodeclock->unlock();
03445 
03446     int gotSubtitles = 0;
03447     AVSubtitle subtitle;
03448     memset(&subtitle, 0, sizeof(AVSubtitle));
03449 
03450     if (ringBuffer->IsDVD())
03451     {
03452         if (ringBuffer->DVD()->NumMenuButtons() > 0)
03453         {
03454             ringBuffer->DVD()->GetMenuSPUPkt(pkt->data, pkt->size,
03455                                              curstream->id);
03456         }
03457         else
03458         {
03459             if (pkt->stream_index == subIdx)
03460             {
03461                 QMutexLocker locker(avcodeclock);
03462                 ringBuffer->DVD()->DecodeSubtitles(&subtitle, &gotSubtitles,
03463                                                    pkt->data, pkt->size);
03464             }
03465         }
03466     }
03467     else if (decodeAllSubtitles || pkt->stream_index == subIdx)
03468     {
03469         QMutexLocker locker(avcodeclock);
03470         avcodec_decode_subtitle2(curstream->codec, &subtitle, &gotSubtitles,
03471                                  pkt);
03472     }
03473 
03474     if (gotSubtitles)
03475     {
03476         subtitle.start_display_time += pts;
03477         subtitle.end_display_time += pts;
03478         LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
03479             QString("subtl timecode %1 %2 %3 %4")
03480                 .arg(pkt->pts).arg(pkt->dts)
03481                 .arg(subtitle.start_display_time)
03482                 .arg(subtitle.end_display_time));
03483 
03484         bool forcedon = m_parent->GetSubReader(pkt->stream_index)->AddAVSubtitle(
03485                subtitle, curstream->codec->codec_id == CODEC_ID_XSUB,
03486                m_parent->GetAllowForcedSubtitles());
03487         m_parent->EnableForcedSubtitles(forcedon);
03488     }
03489 
03490     return true;
03491 }
03492 
03493 bool AvFormatDecoder::ProcessRawTextPacket(AVPacket *pkt)
03494 {
03495     if (!(decodeAllSubtitles ||
03496         selectedTrack[kTrackTypeRawText].av_stream_index == pkt->stream_index))
03497     {
03498         return false;
03499     }
03500 
03501     if (!m_parent->GetSubReader(pkt->stream_index+0x2000))
03502         return false;
03503 
03504     QTextCodec *codec = QTextCodec::codecForName("utf-8");
03505     QTextDecoder *dec = codec->makeDecoder();
03506     QString text      = dec->toUnicode((const char*)pkt->data, pkt->size);
03507     QStringList list  = text.split('\n', QString::SkipEmptyParts);
03508     delete dec;
03509 
03510     m_parent->GetSubReader(pkt->stream_index+0x2000)->
03511         AddRawTextSubtitle(list, pkt->convergence_duration);
03512 
03513     return true;
03514 }
03515 
03516 bool AvFormatDecoder::ProcessDataPacket(AVStream *curstream, AVPacket *pkt,
03517                                         DecodeType decodetype)
03518 {
03519     enum CodecID codec_id = curstream->codec->codec_id;
03520 
03521     switch (codec_id)
03522     {
03523         case CODEC_ID_MPEG2VBI:
03524             ProcessVBIDataPacket(curstream, pkt);
03525             break;
03526         case CODEC_ID_DVB_VBI:
03527             ProcessDVBDataPacket(curstream, pkt);
03528             break;
03529         case CODEC_ID_DSMCC_B:
03530         {
03531             ProcessDSMCCPacket(curstream, pkt);
03532             GenerateDummyVideoFrames();
03533             // Have to return regularly to ensure that the OSD is updated.
03534             // This applies both to MHEG and also channel browsing.
03535 #ifdef USING_MHEG
03536             if (!(decodetype & kDecodeVideo))
03537                 allowedquit |= (itv && itv->ImageHasChanged());
03538 #endif // USING_MHEG:
03539             break;
03540         }
03541     }
03542     return true;
03543 }
03544 
03545 int AvFormatDecoder::SetTrack(uint type, int trackNo)
03546 {
03547     bool ret = DecoderBase::SetTrack(type, trackNo);
03548 
03549     if (kTrackTypeAudio == type)
03550     {
03551         QString msg = SetupAudioStream() ? "" : "not ";
03552         LOG(VB_AUDIO, LOG_INFO, LOC + "Audio stream type "+msg+"changed.");
03553     }
03554 
03555     return ret;
03556 }
03557 
03558 QString AvFormatDecoder::GetTrackDesc(uint type, uint trackNo) const
03559 {
03560     if (trackNo >= tracks[type].size())
03561         return "";
03562 
03563     bool forced = tracks[type][trackNo].forced;
03564     int lang_key = tracks[type][trackNo].language;
03565     if (kTrackTypeAudio == type)
03566     {
03567         if (ringBuffer->IsDVD())
03568             lang_key = ringBuffer->DVD()->GetAudioLanguage(trackNo);
03569 
03570         QString msg = iso639_key_toName(lang_key);
03571 
03572         switch (tracks[type][trackNo].audio_type)
03573         {
03574             case kAudioTypeAudioDescription :
03575                 msg += QObject::tr(" (Audio Description)",
03576                                    "Audio described for the visually impaired");
03577                 break;
03578             case kAudioTypeCommentary :
03579                 msg += QObject::tr(" (Commentary)", "Audio commentary track");
03580                 break;
03581             case kAudioTypeNormal : default :
03582                 int av_index = tracks[kTrackTypeAudio][trackNo].av_stream_index;
03583                 AVStream *s = ic->streams[av_index];
03584 
03585                 if (s)
03586                 {
03587                     if (s->codec->codec_id == CODEC_ID_MP3)
03588                         msg += QString(" MP%1").arg(s->codec->sub_id);
03589                     else if (s->codec->codec)
03590                         msg += QString(" %1").arg(s->codec->codec->name).toUpper();
03591 
03592                     int channels = 0;
03593                     if (ringBuffer->IsDVD())
03594                         channels = ringBuffer->DVD()->GetNumAudioChannels(trackNo);
03595                     else if (s->codec->channels)
03596                         channels = tracks[kTrackTypeAudio][trackNo].orig_num_channels;
03597 
03598                     if (channels == 0)
03599                         msg += QString(" ?ch");
03600                     else if((channels > 4) && !(channels & 1))
03601                         msg += QString(" %1.1ch").arg(channels - 1);
03602                     else
03603                         msg += QString(" %1ch").arg(channels);
03604                 }
03605 
03606                 break;
03607         }
03608 
03609         return QString("%1: %2").arg(trackNo + 1).arg(msg);
03610     }
03611     else if (kTrackTypeSubtitle == type)
03612     {
03613         if (ringBuffer->IsDVD())
03614             lang_key = ringBuffer->DVD()->GetSubtitleLanguage(trackNo);
03615 
03616         return QObject::tr("Subtitle") + QString(" %1: %2%3")
03617             .arg(trackNo + 1).arg(iso639_key_toName(lang_key))
03618             .arg(forced ? " (forced)" : "");
03619     }
03620     else
03621     {
03622         return DecoderBase::GetTrackDesc(type, trackNo);
03623     }
03624 }
03625 
03626 int AvFormatDecoder::GetTeletextDecoderType(void) const
03627 {
03628     return ttd->GetDecoderType();
03629 }
03630 
03631 QString AvFormatDecoder::GetXDS(const QString &key) const
03632 {
03633     return ccd608->GetXDS(key);
03634 }
03635 
03636 QByteArray AvFormatDecoder::GetSubHeader(uint trackNo) const
03637 {
03638     if (trackNo >= tracks[kTrackTypeSubtitle].size())
03639         return QByteArray();
03640 
03641     int index = tracks[kTrackTypeSubtitle][trackNo].av_stream_index;
03642     if (!ic->streams[index]->codec)
03643         return QByteArray();
03644 
03645     return QByteArray((char *)ic->streams[index]->codec->subtitle_header,
03646                       ic->streams[index]->codec->subtitle_header_size);
03647 }
03648 
03649 void AvFormatDecoder::GetAttachmentData(uint trackNo, QByteArray &filename,
03650                                         QByteArray &data)
03651 {
03652     if (trackNo >= tracks[kTrackTypeAttachment].size())
03653         return;
03654 
03655     int index = tracks[kTrackTypeAttachment][trackNo].av_stream_index;
03656     AVDictionaryEntry *tag = av_dict_get(ic->streams[index]->metadata,
03657                                          "filename", NULL, 0);
03658     if (tag)
03659         filename  = QByteArray(tag->value);
03660     data      = QByteArray((char *)ic->streams[index]->codec->extradata,
03661                            ic->streams[index]->codec->extradata_size);
03662 }
03663 
03664 bool AvFormatDecoder::SetAudioByComponentTag(int tag)
03665 {
03666     for (uint i = 0; i < tracks[kTrackTypeAudio].size(); i++)
03667     {
03668         AVStream *s  = ic->streams[tracks[kTrackTypeAudio][i].av_stream_index];
03669         if (s)
03670         {
03671             if ((s->component_tag == tag) ||
03672                 ((tag <= 0) && s->component_tag <= 0))
03673             {
03674                 return SetTrack(kTrackTypeAudio, i);
03675             }
03676         }
03677     }
03678     return false;
03679 }
03680 
03681 bool AvFormatDecoder::SetVideoByComponentTag(int tag)
03682 {
03683     for (uint i = 0; i < ic->nb_streams; i++)
03684     {
03685         AVStream *s  = ic->streams[i];
03686         if (s)
03687         {
03688             if (s->component_tag == tag)
03689             {
03690                 StreamInfo si(i, 0, 0, 0, 0);
03691                 selectedTrack[kTrackTypeVideo] = si;
03692                 return true;
03693             }
03694         }
03695     }
03696     return false;
03697 }
03698 
03699 // documented in decoderbase.cpp
03700 int AvFormatDecoder::AutoSelectTrack(uint type)
03701 {
03702     if (kTrackTypeAudio == type)
03703         return AutoSelectAudioTrack();
03704 
03705     if (ringBuffer->IsInDiscMenuOrStillFrame())
03706         return -1;
03707 
03708     return DecoderBase::AutoSelectTrack(type);
03709 }
03710 
03711 static vector<int> filter_lang(const sinfo_vec_t &tracks, int lang_key)
03712 {
03713     vector<int> ret;
03714 
03715     for (uint i = 0; i < tracks.size(); i++)
03716         if ((lang_key < 0) || tracks[i].language == lang_key)
03717             ret.push_back(i);
03718 
03719     return ret;
03720 }
03721 
03722 static sinfo_vec_t filter_type(const sinfo_vec_t &tracks, AudioTrackType type)
03723 {
03724     sinfo_vec_t ret;
03725 
03726     for (uint i = 0; i < tracks.size(); i++)
03727     {
03728         if (tracks[i].audio_type == type)
03729             ret.push_back(tracks[i]);
03730     }
03731 
03732     return ret;
03733 }
03734 
03735 int AvFormatDecoder::filter_max_ch(const AVFormatContext *ic,
03736                                    const sinfo_vec_t     &tracks,
03737                                    const vector<int>     &fs,
03738                                    enum CodecID           codecId,
03739                                    int                    profile)
03740 {
03741     int selectedTrack = -1, max_seen = -1;
03742 
03743     vector<int>::const_iterator it = fs.begin();
03744     for (; it != fs.end(); ++it)
03745     {
03746         const int stream_index = tracks[*it].av_stream_index;
03747         const AVCodecContext *ctx = ic->streams[stream_index]->codec;
03748         if ((codecId == CODEC_ID_NONE || codecId == ctx->codec_id) &&
03749             (max_seen < ctx->channels))
03750         {
03751             if (codecId == CODEC_ID_DTS && profile > 0)
03752             {
03753                 // we cannot decode dts-hd, so only select it if passthrough
03754                 if (!DoPassThrough(ctx, true) || ctx->profile != profile)
03755                     continue;
03756             }
03757             selectedTrack = *it;
03758             max_seen = ctx->channels;
03759         }
03760     }
03761 
03762     return selectedTrack;
03763 }
03764 
03811 int AvFormatDecoder::AutoSelectAudioTrack(void)
03812 {
03813     const sinfo_vec_t &atracks = tracks[kTrackTypeAudio];
03814     StreamInfo        &wtrack  = wantedTrack[kTrackTypeAudio];
03815     StreamInfo        &strack  = selectedTrack[kTrackTypeAudio];
03816     int               &ctrack  = currentTrack[kTrackTypeAudio];
03817 
03818     uint numStreams = atracks.size();
03819     if ((ctrack >= 0) && (ctrack < (int)numStreams))
03820         return ctrack; // audio already selected
03821 
03822 #if 0
03823     // enable this to print streams
03824     for (uint i = 0; i < atracks.size(); i++)
03825     {
03826         int idx = atracks[i].av_stream_index;
03827         AVCodecContext *codec_ctx = ic->streams[idx]->codec;
03828         AudioInfo item(codec_ctx->codec_id, codec_ctx->bps,
03829                        codec_ctx->sample_rate, codec_ctx->channels,
03830                        DoPassThrough(codec_ctx, true));
03831         LOG(VB_AUDIO, LOG_DEBUG, LOC + " * " + item.toString());
03832     }
03833 #endif
03834 
03835     int selTrack = (1 == numStreams) ? 0 : -1;
03836     int wlang    = wtrack.language;
03837 
03838     if (selTrack < 0 && numStreams)
03839     {
03840         LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to select default track");
03841         for (uint i = 0; i < atracks.size(); i++) {
03842             int idx = atracks[i].av_stream_index;
03843             if (ic->streams[idx]->disposition & AV_DISPOSITION_DEFAULT)
03844             {
03845                 selTrack = i;
03846                 break;
03847             }
03848         }
03849     }
03850 
03851     if ((selTrack < 0) && (wtrack.av_substream_index >= 0))
03852     {
03853         LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to reselect audio sub-stream");
03854         // Dual stream without language information: choose
03855         // the previous substream that was kept in wtrack,
03856         // ignoring the stream index (which might have changed).
03857         int substream_index = wtrack.av_substream_index;
03858 
03859         for (uint i = 0; i < numStreams; i++)
03860         {
03861             if (atracks[i].av_substream_index == substream_index)
03862             {
03863                 selTrack = i;
03864                 break;
03865             }
03866         }
03867     }
03868 
03869     if ((selTrack < 0) && wlang >= -1 && numStreams)
03870     {
03871         LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to reselect audio track");
03872         // Try to reselect user selected audio stream.
03873         // This should find the stream after a commercial
03874         // break and in some cases after a channel change.
03875         uint windx = wtrack.language_index;
03876         for (uint i = 0; i < numStreams; i++)
03877         {
03878             if (wlang == atracks[i].language)
03879             {
03880                 selTrack = i;
03881 
03882                 if (windx == atracks[i].language_index)
03883                     break;
03884             }
03885         }
03886     }
03887 
03888     if (selTrack < 0 && numStreams)
03889     {
03890         LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to select audio track (w/lang)");
03891 
03892         // Filter out commentary and audio description tracks
03893         sinfo_vec_t ftracks = filter_type(atracks, kAudioTypeNormal);
03894 
03895         if (ftracks.empty())
03896         {
03897             LOG(VB_AUDIO, LOG_WARNING, "No audio tracks matched the filter so trying without filter.");
03898             ftracks = atracks;
03899         }
03900 
03901         // try to get the language track matching the frontend language.
03902         QString language_key_convert = iso639_str2_to_str3(gCoreContext->GetLanguage());
03903         uint language_key = iso639_str3_to_key(language_key_convert);
03904         uint canonical_key = iso639_key_to_canonical_key(language_key);
03905 
03906         vector<int> flang = filter_lang(ftracks, canonical_key);
03907 
03908         if (m_audio->CanDTSHD())
03909             selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_DTS,
03910                                      FF_PROFILE_DTS_HD_MA);
03911         if (selTrack < 0)
03912             selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_TRUEHD);
03913 
03914         if (selTrack < 0 && m_audio->CanDTSHD())
03915             selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_DTS,
03916                                      FF_PROFILE_DTS_HD_HRA);
03917         if (selTrack < 0)
03918             selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_EAC3);
03919 
03920         if (selTrack < 0)
03921             selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_DTS);
03922 
03923         if (selTrack < 0)
03924             selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_AC3);
03925 
03926         if (selTrack < 0)
03927             selTrack = filter_max_ch(ic, ftracks, flang);
03928 
03929         // try to get best track for most preferred language
03930         // Set by the "Guide Data" language prefs in Appearance.
03931         if (selTrack < 0)
03932         {
03933             vector<int>::const_iterator it = languagePreference.begin();
03934             for (; it !=  languagePreference.end() && selTrack < 0; ++it)
03935             {
03936                 vector<int> flang = filter_lang(ftracks, *it);
03937 
03938                 if (m_audio->CanDTSHD())
03939                     selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_DTS,
03940                                              FF_PROFILE_DTS_HD_MA);
03941                 if (selTrack < 0)
03942                     selTrack = filter_max_ch(ic, ftracks, flang,
03943                                              CODEC_ID_TRUEHD);
03944 
03945                 if (selTrack < 0 && m_audio->CanDTSHD())
03946                     selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_DTS,
03947                                              FF_PROFILE_DTS_HD_HRA);
03948 
03949                 if (selTrack < 0)
03950                     selTrack = filter_max_ch(ic, ftracks, flang,
03951                                              CODEC_ID_EAC3);
03952 
03953                 if (selTrack < 0)
03954                     selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_DTS);
03955 
03956                 if (selTrack < 0)
03957                     selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_AC3);
03958 
03959                 if (selTrack < 0)
03960                     selTrack = filter_max_ch(ic, ftracks, flang);
03961             }
03962         }
03963         // try to get best track for any language
03964         if (selTrack < 0)
03965         {
03966             LOG(VB_AUDIO, LOG_INFO, LOC +
03967                 "Trying to select audio track (wo/lang)");
03968             vector<int> flang = filter_lang(ftracks, -1);
03969 
03970             if (m_audio->CanDTSHD())
03971                 selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_DTS,
03972                                          FF_PROFILE_DTS_HD_MA);
03973             if (selTrack < 0)
03974                 selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_TRUEHD);
03975 
03976             if (selTrack < 0 && m_audio->CanDTSHD())
03977                 selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_DTS,
03978                                          FF_PROFILE_DTS_HD_HRA);
03979 
03980             if (selTrack < 0)
03981                 selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_EAC3);
03982 
03983             if (selTrack < 0)
03984                 selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_DTS);
03985 
03986             if (selTrack < 0)
03987                 selTrack = filter_max_ch(ic, ftracks, flang, CODEC_ID_AC3);
03988 
03989             if (selTrack < 0)
03990                 selTrack = filter_max_ch(ic, ftracks, flang);
03991         }
03992     }
03993 
03994     if (selTrack < 0)
03995     {
03996         strack.av_stream_index = -1;
03997         if (ctrack != selTrack)
03998         {
03999             LOG(VB_AUDIO, LOG_INFO, LOC + "No suitable audio track exists.");
04000             ctrack = selTrack;
04001         }
04002     }
04003     else
04004     {
04005         ctrack = selTrack;
04006         strack = atracks[selTrack];
04007 
04008         if (wtrack.av_stream_index < 0)
04009             wtrack = strack;
04010 
04011         LOG(VB_AUDIO, LOG_INFO, LOC +
04012             QString("Selected track %1 (A/V Stream #%2)")
04013                 .arg(GetTrackDesc(kTrackTypeAudio, ctrack))
04014                 .arg(strack.av_stream_index));
04015     }
04016 
04017     SetupAudioStream();
04018     return selTrack;
04019 }
04020 
04021 static void extract_mono_channel(uint channel, AudioInfo *audioInfo,
04022                                  char *buffer, int bufsize)
04023 {
04024     // Only stereo -> mono (left or right) is supported
04025     if (audioInfo->channels != 2)
04026         return;
04027 
04028     if (channel >= (uint)audioInfo->channels)
04029         return;
04030 
04031     const uint samplesize = audioInfo->sample_size;
04032     const uint samples    = bufsize / samplesize;
04033     const uint halfsample = samplesize >> 1;
04034 
04035     const char *from = (channel == 1) ? buffer + halfsample : buffer;
04036     char *to         = (channel == 0) ? buffer + halfsample : buffer;
04037 
04038     for (uint sample = 0; sample < samples;
04039          (sample++), (from += samplesize), (to += samplesize))
04040     {
04041         memmove(to, from, halfsample);
04042     }
04043 }
04044 
04045 bool AvFormatDecoder::ProcessAudioPacket(AVStream *curstream, AVPacket *pkt,
04046                                          DecodeType decodetype)
04047 {
04048     AVCodecContext *ctx = curstream->codec;
04049     int ret             = 0;
04050     int data_size       = 0;
04051     bool firstloop      = true;
04052     int decoded_size    = -1;
04053 
04054     avcodeclock->lock();
04055     int audIdx = selectedTrack[kTrackTypeAudio].av_stream_index;
04056     int audSubIdx = selectedTrack[kTrackTypeAudio].av_substream_index;
04057     avcodeclock->unlock();
04058 
04059     AVPacket tmp_pkt;
04060     av_init_packet(&tmp_pkt);
04061     tmp_pkt.data = pkt->data;
04062     tmp_pkt.size = pkt->size;
04063 
04064     while (tmp_pkt.size > 0)
04065     {
04066         bool reselectAudioTrack = false;
04067 
04069         if (!m_audio->HasAudioIn())
04070         {
04071             LOG(VB_AUDIO, LOG_INFO, LOC +
04072                 "Audio is disabled - trying to restart it");
04073             reselectAudioTrack = true;
04074         }
04076 
04077         // detect switches between stereo and dual languages
04078         bool wasDual = audSubIdx != -1;
04079         bool isDual = ctx->avcodec_dual_language;
04080         if ((wasDual && !isDual) || (!wasDual &&  isDual))
04081         {
04082             SetupAudioStreamSubIndexes(audIdx);
04083             reselectAudioTrack = true;
04084         }
04085 
04086         // detect channels on streams that need
04087         // to be decoded before we can know this
04088         bool already_decoded = false;
04089         if (!ctx->channels)
04090         {
04091             LOG(VB_GENERAL, LOG_INFO, LOC + QString("Setting channels to %1")
04092                     .arg(audioOut.channels));
04093 
04094             QMutexLocker locker(avcodeclock);
04095 
04096             if (DoPassThrough(ctx, false) || !DecoderWillDownmix(ctx))
04097             {
04098                 // for passthru or codecs for which the decoder won't downmix
04099                 // let the decoder set the number of channels. For other codecs
04100                 // we downmix if necessary in audiooutputbase
04101                 ctx->request_channels = 0;
04102             }
04103             else // No passthru, the decoder will downmix
04104             {
04105                 ctx->request_channels = m_audio->GetMaxChannels();
04106                 if (ctx->codec_id == CODEC_ID_AC3)
04107                     ctx->channels = m_audio->GetMaxChannels();
04108             }
04109 
04110             ret = DecodeAudio(ctx, audioSamples, data_size, &tmp_pkt);
04111             decoded_size = data_size;
04112             already_decoded = true;
04113             reselectAudioTrack |= ctx->channels;
04114         }
04115 
04116         if (reselectAudioTrack)
04117         {
04118             QMutexLocker locker(avcodeclock);
04119             currentTrack[kTrackTypeAudio] = -1;
04120             selectedTrack[kTrackTypeAudio].av_stream_index = -1;
04121             audIdx = -1;
04122             audSubIdx = -1;
04123             AutoSelectAudioTrack();
04124             audIdx = selectedTrack[kTrackTypeAudio].av_stream_index;
04125             audSubIdx = selectedTrack[kTrackTypeAudio].av_substream_index;
04126         }
04127 
04128         if (!(decodetype & kDecodeAudio) || (pkt->stream_index != audIdx))
04129             break;
04130 
04131         if (firstloop && pkt->pts != (int64_t)AV_NOPTS_VALUE)
04132             lastapts = (long long)(av_q2d(curstream->time_base) * pkt->pts * 1000);
04133 
04134         if (skipaudio && selectedTrack[kTrackTypeVideo].av_stream_index > -1)
04135         {
04136             if ((lastapts < lastvpts - (10.0 / fps)) || lastvpts == 0)
04137                 break;
04138             else
04139                 skipaudio = false;
04140         }
04141 
04142         // skip any audio frames preceding first video frame
04143         if (firstvptsinuse && firstvpts && (lastapts < firstvpts))
04144         {
04145             LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
04146                 QString("discarding early audio timecode %1 %2 %3")
04147                     .arg(pkt->pts).arg(pkt->dts).arg(lastapts));
04148             break;
04149         }
04150         firstvptsinuse = false;
04151 
04152         avcodeclock->lock();
04153         data_size = 0;
04154 
04155         if (audioOut.do_passthru)
04156         {
04157             if (!already_decoded)
04158             {
04159                 if (m_audio->NeedDecodingBeforePassthrough())
04160                 {
04161                     ret = DecodeAudio(ctx, audioSamples, data_size, &tmp_pkt);
04162                     decoded_size = data_size;
04163                 }
04164                 else
04165                     decoded_size = -1;
04166             }
04167             memcpy(audioSamples, tmp_pkt.data, tmp_pkt.size);
04168             data_size = tmp_pkt.size;
04169             // We have processed all the data, there can't be any left
04170             tmp_pkt.size = 0;
04171         }
04172         else
04173         {
04174             if (!already_decoded)
04175             {
04176                 if (DecoderWillDownmix(ctx))
04177                 {
04178                     ctx->request_channels = m_audio->GetMaxChannels();
04179                     if (ctx->codec_id == CODEC_ID_AC3)
04180                         ctx->channels = m_audio->GetMaxChannels();
04181                 }
04182                 else
04183                     ctx->request_channels = 0;
04184 
04185                 ret = DecodeAudio(ctx, audioSamples, data_size, &tmp_pkt);
04186                 decoded_size = data_size;
04187             }
04188 
04189             // When decoding some audio streams the number of
04190             // channels, etc isn't known until we try decoding it.
04191             if (ctx->sample_rate != audioOut.sample_rate ||
04192                 ctx->channels    != audioOut.channels)
04193             {
04194                 LOG(VB_GENERAL, LOG_INFO, LOC + "Audio stream changed");
04195                 currentTrack[kTrackTypeAudio] = -1;
04196                 selectedTrack[kTrackTypeAudio].av_stream_index = -1;
04197                 audIdx = -1;
04198                 AutoSelectAudioTrack();
04199                 data_size = 0;
04200             }
04201         }
04202         avcodeclock->unlock();
04203 
04204         if (ret < 0)
04205         {
04206             LOG(VB_GENERAL, LOG_ERR, LOC + "Unknown audio decoding error");
04207             return false;
04208         }
04209 
04210         if (data_size <= 0)
04211         {
04212             tmp_pkt.data += ret;
04213             tmp_pkt.size -= ret;
04214             continue;
04215         }
04216 
04217         long long temppts = lastapts;
04218 
04219         if (audSubIdx != -1 && !audioOut.do_passthru)
04220             extract_mono_channel(audSubIdx, &audioOut,
04221                                  (char *)audioSamples, data_size);
04222 
04223         int frames = (ctx->channels <= 0 || decoded_size < 0) ? -1 :
04224             decoded_size / (ctx->channels *
04225                             av_get_bytes_per_sample(ctx->sample_fmt));
04226         m_audio->AddAudioData((char *)audioSamples, data_size, temppts, frames);
04227         if (audioOut.do_passthru && !m_audio->NeedDecodingBeforePassthrough())
04228         {
04229             lastapts += m_audio->LengthLastData();
04230         }
04231         else
04232         {
04233             lastapts += (long long)
04234                 ((double)(frames * 1000) / ctx->sample_rate);
04235         }
04236 
04237         LOG(VB_TIMESTAMP, LOG_INFO, LOC + QString("audio timecode %1 %2 %3 %4")
04238                 .arg(pkt->pts).arg(pkt->dts).arg(temppts).arg(lastapts));
04239 
04240         allowedquit |= ringBuffer->IsInDiscMenuOrStillFrame() ||
04241                        m_audio->IsBufferAlmostFull();
04242 
04243         tmp_pkt.data += ret;
04244         tmp_pkt.size -= ret;
04245         firstloop = false;
04246     }
04247 
04248     return true;
04249 }
04250 
04251 int AvFormatDecoder::DecodeAudio(AVCodecContext *ctx,
04252                                  uint8_t *buffer, int &data_size,
04253                                  AVPacket *pkt)
04254 {
04255     AVFrame frame;
04256     int got_frame = 0;
04257 
04258     int ret = avcodec_decode_audio4(ctx, &frame, &got_frame, pkt);
04259     if (ret < 0 || !got_frame)
04260     {
04261         data_size = 0;
04262         return ret;
04263     }
04264 
04265     int plane_size;
04266     int planar = av_sample_fmt_is_planar(ctx->sample_fmt);
04267     data_size = av_samples_get_buffer_size(&plane_size, ctx->channels,
04268                                            frame.nb_samples,
04269                                            ctx->sample_fmt, 1);
04270     memcpy(buffer, frame.extended_data[0], plane_size);
04271 
04272     if (planar && ctx->channels > 1)
04273     {
04274         uint8_t *out = buffer + plane_size;
04275         for (int i = 1; i < ctx->channels; i++)
04276         {
04277             memcpy(out, frame.extended_data[i], plane_size);
04278             out += plane_size;
04279         }
04280     }
04281     return ret;
04282 }
04283 
04284 // documented in decoderbase.h
04285 bool AvFormatDecoder::GetFrame(DecodeType decodetype)
04286 {
04287     AVPacket *pkt = NULL;
04288     bool have_err = false;
04289 
04290     gotVideoFrame = false;
04291 
04292     frame_decoded = 0;
04293     decoded_video_frame = NULL;
04294 
04295     allowedquit = false;
04296     bool storevideoframes = false;
04297 
04298     avcodeclock->lock();
04299     AutoSelectTracks();
04300     avcodeclock->unlock();
04301 
04302     skipaudio = (lastvpts == 0);
04303 
04304     hasVideo = HasVideo(ic);
04305     needDummyVideoFrames = false;
04306 
04307     if (!hasVideo && (decodetype & kDecodeVideo))
04308     {
04309         // NB This could be an issue if the video stream is not
04310         // detected initially as the video buffers will be filled.
04311         needDummyVideoFrames = true;
04312         decodetype = (DecodeType)((int)decodetype & ~kDecodeVideo);
04313         skipaudio = false;
04314     }
04315 
04316     allowedquit = m_audio->IsBufferAlmostFull();
04317 
04318     if (private_dec && private_dec->HasBufferedFrames() &&
04319        (selectedTrack[kTrackTypeVideo].av_stream_index > -1))
04320     {
04321         AVStream *stream = ic->streams[selectedTrack[kTrackTypeVideo]
04322                                  .av_stream_index];
04323         AVFrame mpa_pic;
04324         avcodec_get_frame_defaults(&mpa_pic);
04325         int got_picture = 0;
04326         private_dec->GetFrame(stream, &mpa_pic, &got_picture, NULL);
04327         if (got_picture)
04328             ProcessVideoFrame(stream, &mpa_pic);
04329     }
04330 
04331     while (!allowedquit)
04332     {
04333         if ((decodetype & kDecodeAudio) &&
04334             ((currentTrack[kTrackTypeAudio] < 0) ||
04335              (selectedTrack[kTrackTypeAudio].av_stream_index < 0)))
04336         {
04337             // disable audio request if there are no audio streams anymore
04338             // and we have video, otherwise allow decoding to stop
04339             if (hasVideo)
04340                 decodetype = (DecodeType)((int)decodetype & ~kDecodeAudio);
04341             else
04342                 allowedquit = true;
04343         }
04344 
04345         StreamChangeCheck();
04346 
04347         if (gotVideoFrame)
04348         {
04349             if (decodetype == kDecodeNothing)
04350             {
04351                 // no need to buffer audio or video if we
04352                 // only care about building a keyframe map.
04353                 allowedquit = true;
04354                 continue;
04355             }
04356             else if (lowbuffers && ((decodetype & kDecodeAV) == kDecodeAV) &&
04357                      storedPackets.count() < max_video_queue_size &&
04358                      lastapts < lastvpts + 100 &&
04359                      !ringBuffer->IsInDiscMenuOrStillFrame())
04360             {
04361                 storevideoframes = true;
04362             }
04363             else if (decodetype & kDecodeVideo)
04364             {
04365                 if (storedPackets.count() >= max_video_queue_size)
04366                     LOG(VB_GENERAL, LOG_WARNING, LOC +
04367                         QString("Audio %1 ms behind video but already %2 "
04368                                 "video frames queued. AV-Sync might be broken.")
04369                             .arg(lastvpts-lastapts).arg(storedPackets.count()));
04370                 allowedquit = true;
04371                 continue;
04372             }
04373         }
04374 
04375         if (!storevideoframes && storedPackets.count() > 0)
04376         {
04377             if (pkt)
04378             {
04379                 av_free_packet(pkt);
04380                 delete pkt;
04381             }
04382             pkt = storedPackets.takeFirst();
04383         }
04384         else
04385         {
04386             if (!pkt)
04387             {
04388                 pkt = new AVPacket;
04389                 memset(pkt, 0, sizeof(AVPacket));
04390                 av_init_packet(pkt);
04391             }
04392 
04393             int retval = 0;
04394             if (!ic || ((retval = av_read_frame(ic, pkt)) < 0))
04395             {
04396                 if (retval == -EAGAIN)
04397                     continue;
04398 
04399                 SetEof(true);
04400                 delete pkt;
04401                 errno = -retval;
04402                 LOG(VB_GENERAL, LOG_ERR, QString("decoding error") + ENO);
04403                 return false;
04404             }
04405 
04406             if (waitingForChange && pkt->pos >= readAdjust)
04407                 FileChanged();
04408 
04409             if (pkt->pos > readAdjust)
04410                 pkt->pos -= readAdjust;
04411         }
04412 
04413         if (!ic)
04414         {
04415             LOG(VB_GENERAL, LOG_ERR, LOC + "No context");
04416             av_free_packet(pkt);
04417             continue;
04418         }
04419 
04420         if (pkt->stream_index >= (int)ic->nb_streams)
04421         {
04422             LOG(VB_GENERAL, LOG_ERR, LOC + "Bad stream");
04423             av_free_packet(pkt);
04424             continue;
04425         }
04426 
04427         AVStream *curstream = ic->streams[pkt->stream_index];
04428 
04429         if (!curstream)
04430         {
04431             LOG(VB_GENERAL, LOG_ERR, LOC + "Bad stream (NULL)");
04432             av_free_packet(pkt);
04433             continue;
04434         }
04435 
04436         enum AVMediaType codec_type = curstream->codec->codec_type;
04437 
04438         if (storevideoframes && codec_type == AVMEDIA_TYPE_VIDEO)
04439         {
04440             av_dup_packet(pkt);
04441             storedPackets.append(pkt);
04442             pkt = NULL;
04443             continue;
04444         }
04445 
04446         if (codec_type == AVMEDIA_TYPE_VIDEO &&
04447             pkt->stream_index == selectedTrack[kTrackTypeVideo].av_stream_index)
04448         {
04449             if (!PreProcessVideoPacket(curstream, pkt))
04450                 continue;
04451 
04452             // If the resolution changed in XXXPreProcessPkt, we may
04453             // have a fatal error, so check for this before continuing.
04454             if (m_parent->IsErrored())
04455             {
04456                 av_free_packet(pkt);
04457                 delete pkt;
04458                 return false;
04459             }
04460         }
04461 
04462         if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
04463             curstream->codec->codec_id == CODEC_ID_TEXT)
04464         {
04465             ProcessRawTextPacket(pkt);
04466             av_free_packet(pkt);
04467             continue;
04468         }
04469 
04470         if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
04471             curstream->codec->codec_id == CODEC_ID_DVB_TELETEXT)
04472         {
04473             ProcessDVBDataPacket(curstream, pkt);
04474             av_free_packet(pkt);
04475             continue;
04476         }
04477 
04478         if (codec_type == AVMEDIA_TYPE_DATA)
04479         {
04480             ProcessDataPacket(curstream, pkt, decodetype);
04481             av_free_packet(pkt);
04482             continue;
04483         }
04484 
04485         if (!curstream->codec->codec)
04486         {
04487             LOG(VB_PLAYBACK, LOG_ERR, LOC +
04488                 QString("No codec for stream index %1, type(%2) id(%3:%4)")
04489                     .arg(pkt->stream_index)
04490                     .arg(ff_codec_type_string(codec_type))
04491                     .arg(ff_codec_id_string(curstream->codec->codec_id))
04492                     .arg(curstream->codec->codec_id));
04493             av_free_packet(pkt);
04494             continue;
04495         }
04496 
04497         have_err = false;
04498 
04499         switch (codec_type)
04500         {
04501             case AVMEDIA_TYPE_AUDIO:
04502             {
04503                 if (!ProcessAudioPacket(curstream, pkt, decodetype))
04504                     have_err = true;
04505                 else
04506                     GenerateDummyVideoFrames();
04507                 break;
04508             }
04509 
04510             case AVMEDIA_TYPE_VIDEO:
04511             {
04512                 if (pkt->stream_index != selectedTrack[kTrackTypeVideo].av_stream_index)
04513                 {
04514                     break;
04515                 }
04516 
04517                 if (pkt->pts != (int64_t) AV_NOPTS_VALUE)
04518                 {
04519                     lastccptsu = (long long)
04520                                  (av_q2d(curstream->time_base)*pkt->pts*1000000);
04521                 }
04522 
04523                 if (!(decodetype & kDecodeVideo))
04524                 {
04525                     framesPlayed++;
04526                     gotVideoFrame = 1;
04527                     break;
04528                 }
04529 
04530                 if (!ProcessVideoPacket(curstream, pkt))
04531                     have_err = true;
04532                 break;
04533             }
04534 
04535             case AVMEDIA_TYPE_SUBTITLE:
04536             {
04537                 if (!ProcessSubtitlePacket(curstream, pkt))
04538                     have_err = true;
04539                 break;
04540             }
04541 
04542             default:
04543             {
04544                 AVCodecContext *enc = curstream->codec;
04545                 LOG(VB_GENERAL, LOG_ERR, LOC +
04546                     QString("Decoding - id(%1) type(%2)")
04547                         .arg(ff_codec_id_string(enc->codec_id))
04548                         .arg(ff_codec_type_string(enc->codec_type)));
04549                 have_err = true;
04550                 break;
04551             }
04552         }
04553 
04554         if (!have_err)
04555             frame_decoded = 1;
04556 
04557         av_free_packet(pkt);
04558     }
04559 
04560     if (pkt)
04561         delete pkt;
04562 
04563     return true;
04564 }
04565 
04566 bool AvFormatDecoder::HasVideo(const AVFormatContext *ic)
04567 {
04568     if (ic && ic->cur_pmt_sect)
04569     {
04570         const PESPacket pes = PESPacket::ViewData(ic->cur_pmt_sect);
04571         const PSIPTable psip(pes);
04572         const ProgramMapTable pmt(psip);
04573 
04574         for (uint i = 0; i < pmt.StreamCount(); i++)
04575         {
04576             // MythTV remaps OpenCable Video to normal video during recording
04577             // so "dvb" is the safest choice for system info type, since this
04578             // will ignore other uses of the same stream id in DVB countries.
04579             if (pmt.IsVideo(i, "dvb"))
04580                 return true;
04581 
04582             // MHEG may explicitly select a private stream as video
04583             if ((i == (uint)selectedTrack[kTrackTypeVideo].av_stream_index) &&
04584                 (pmt.StreamType(i) == StreamID::PrivData))
04585             {
04586                 return true;
04587             }
04588         }
04589     }
04590 
04591     return GetTrackCount(kTrackTypeVideo);
04592 }
04593 
04594 bool AvFormatDecoder::GenerateDummyVideoFrames(void)
04595 {
04596     while (needDummyVideoFrames && m_parent &&
04597            m_parent->GetFreeVideoFrames())
04598     {
04599         VideoFrame *frame = m_parent->GetNextVideoFrame();
04600         if (!frame)
04601             return false;
04602 
04603         m_parent->ClearDummyVideoFrame(frame);
04604         m_parent->ReleaseNextVideoFrame(frame, lastvpts);
04605         m_parent->DeLimboFrame(frame);
04606 
04607         frame->interlaced_frame = 0; // not interlaced
04608         frame->top_field_first  = 1; // top field first
04609         frame->repeat_pict      = 0; // not a repeated picture
04610         frame->frameNumber      = framesPlayed;
04611         frame->dummy            = 1;
04612 
04613         decoded_video_frame = frame;
04614         framesPlayed++;
04615         gotVideoFrame = true;
04616     }
04617     return true;
04618 }
04619 
04620 QString AvFormatDecoder::GetCodecDecoderName(void) const
04621 {
04622     if (private_dec)
04623         return private_dec->GetName();
04624     return get_decoder_name(video_codec_id);
04625 }
04626 
04627 QString AvFormatDecoder::GetRawEncodingType(void)
04628 {
04629     int stream = selectedTrack[kTrackTypeVideo].av_stream_index;
04630     if (stream < 0 || !ic)
04631         return QString();
04632     return ff_codec_id_string(ic->streams[stream]->codec->codec_id);
04633 }
04634 
04635 void *AvFormatDecoder::GetVideoCodecPrivate(void)
04636 {
04637     return NULL; // TODO is this still needed
04638 }
04639 
04640 void AvFormatDecoder::SetDisablePassThrough(bool disable)
04641 {
04642     // can only disable never re-enable as once
04643     // timestretch is on its on for the session
04644     if (disable_passthru)
04645         return;
04646 
04647     if (selectedTrack[kTrackTypeAudio].av_stream_index < 0)
04648     {
04649         disable_passthru = disable;
04650         return;
04651     }
04652 
04653     if (disable != disable_passthru)
04654     {
04655         disable_passthru = disable;
04656         QString msg = (disable) ? "Disabling" : "Allowing";
04657         LOG(VB_AUDIO, LOG_INFO, LOC + msg + " pass through");
04658 
04659         // Force pass through state to be reanalyzed
04660         QMutexLocker locker(avcodeclock);
04661         SetupAudioStream();
04662     }
04663 }
04664 
04665 inline bool AvFormatDecoder::DecoderWillDownmix(const AVCodecContext *ctx)
04666 {
04667     // Until ffmpeg properly implements dialnorm
04668     // use Myth internal downmixer if machines has FPU/SSE
04669     if (m_audio->CanDownmix() && AudioOutputUtil::has_hardware_fpu())
04670         return false;
04671     if (!m_audio->CanDownmix())
04672         return true;
04673     // use ffmpeg only for dolby codecs if we have to
04674     switch (ctx->codec_id)
04675     {
04676         case CODEC_ID_AC3:
04677         case CODEC_ID_TRUEHD:
04678         case CODEC_ID_EAC3:
04679             return true;
04680         default:
04681             return false;
04682     }
04683 }
04684 
04685 bool AvFormatDecoder::DoPassThrough(const AVCodecContext *ctx, bool withProfile)
04686 {
04687     bool passthru;
04688 
04689     // if withProfile == false, we will accept any DTS stream regardless
04690     // of its profile. We do so, so we can bitstream DTS-HD as DTS core
04691     if (!withProfile && ctx->codec_id == CODEC_ID_DTS && !m_audio->CanDTSHD())
04692         passthru = m_audio->CanPassthrough(ctx->sample_rate, ctx->channels,
04693                                            ctx->codec_id, FF_PROFILE_DTS);
04694     else
04695         passthru = m_audio->CanPassthrough(ctx->sample_rate, ctx->channels,
04696                                            ctx->codec_id, ctx->profile);
04697 
04698     passthru &= !disable_passthru;
04699 
04700     return passthru;
04701 }
04702 
04710 bool AvFormatDecoder::SetupAudioStream(void)
04711 {
04712     AudioInfo info; // no_audio
04713     AVStream *curstream = NULL;
04714     AVCodecContext *ctx = NULL;
04715     AudioInfo old_in    = audioIn;
04716     bool using_passthru = false;
04717     int  orig_channels  = 2;
04718 
04719     if ((currentTrack[kTrackTypeAudio] >= 0) && ic &&
04720         (selectedTrack[kTrackTypeAudio].av_stream_index <=
04721          (int) ic->nb_streams) &&
04722         (curstream = ic->streams[selectedTrack[kTrackTypeAudio]
04723                                  .av_stream_index]))
04724     {
04725         assert(curstream);
04726         assert(curstream->codec);
04727         ctx = curstream->codec;
04728         orig_channels = selectedTrack[kTrackTypeAudio].orig_num_channels;
04729         AudioFormat fmt;
04730 
04731         switch (ctx->sample_fmt)
04732         {
04733             case AV_SAMPLE_FMT_U8:     fmt = FORMAT_U8;    break;
04734             case AV_SAMPLE_FMT_S16:    fmt = FORMAT_S16;   break;
04735             case AV_SAMPLE_FMT_FLT:    fmt = FORMAT_FLT;   break;
04736             case AV_SAMPLE_FMT_DBL:    fmt = FORMAT_NONE;  break;
04737             case AV_SAMPLE_FMT_S32:
04738                 switch (ctx->bits_per_raw_sample)
04739                 {
04740                     case  0:    fmt = FORMAT_S32;   break;
04741                     case 24:    fmt = FORMAT_S24;   break;
04742                     case 32:    fmt = FORMAT_S32;   break;
04743                     default:    fmt = FORMAT_NONE;
04744                 }
04745                 break;
04746             default:                fmt = FORMAT_NONE;
04747         }
04748 
04749         if (fmt == FORMAT_NONE)
04750         {
04751             int bps = av_get_bytes_per_sample(ctx->sample_fmt) << 3;
04752             if (ctx->sample_fmt == AV_SAMPLE_FMT_S32 &&
04753                 ctx->bits_per_raw_sample)
04754                 bps = ctx->bits_per_raw_sample;
04755             LOG(VB_GENERAL, LOG_ERR, LOC +
04756                 QString("Unsupported sample format with %1 bits").arg(bps));
04757             return false;
04758         }
04759 
04760         using_passthru = DoPassThrough(ctx, false);
04761 
04762         ctx->request_channels = ctx->channels;
04763 
04764         if (!using_passthru &&
04765             ctx->channels > (int)m_audio->GetMaxChannels() &&
04766             DecoderWillDownmix(ctx))
04767         {
04768             ctx->request_channels = m_audio->GetMaxChannels();
04769         }
04770 
04771         info = AudioInfo(ctx->codec_id, fmt, ctx->sample_rate,
04772                          ctx->channels, using_passthru, orig_channels,
04773                          ctx->codec_id == CODEC_ID_DTS ? ctx->profile : 0);
04774     }
04775 
04776     if (!ctx)
04777     {
04778         if (GetTrackCount(kTrackTypeAudio))
04779             LOG(VB_PLAYBACK, LOG_INFO, LOC +
04780                 "No codec context. Returning false");
04781         return false;
04782     }
04783 
04784     if (info == audioIn)
04785         return false;
04786 
04787     LOG(VB_AUDIO, LOG_INFO, LOC + "Initializing audio parms from " +
04788         QString("audio track #%1").arg(currentTrack[kTrackTypeAudio]+1));
04789 
04790     audioOut = audioIn = info;
04791 
04792     LOG(VB_AUDIO, LOG_INFO, LOC + "Audio format changed " +
04793         QString("\n\t\t\tfrom %1 to %2")
04794             .arg(old_in.toString()).arg(audioOut.toString()));
04795 
04796     m_audio->SetAudioParams(audioOut.format, orig_channels,
04797                             ctx->request_channels,
04798                             audioOut.codec_id, audioOut.sample_rate,
04799                             audioOut.do_passthru, audioOut.codec_profile);
04800     m_audio->ReinitAudio();
04801 
04802     if (LCD *lcd = LCD::Get())
04803     {
04804         LCDAudioFormatSet audio_format;
04805 
04806         switch (ctx->codec_id)
04807         {
04808             case CODEC_ID_MP2:
04809                 audio_format = AUDIO_MPEG2;
04810                 break;
04811             case CODEC_ID_MP3:
04812                 audio_format = AUDIO_MP3;
04813                 break;
04814             case CODEC_ID_AC3:
04815                 audio_format = AUDIO_AC3;
04816                 break;
04817             case CODEC_ID_DTS:
04818                 audio_format = AUDIO_DTS;
04819                 break;
04820             case CODEC_ID_VORBIS:
04821                 audio_format = AUDIO_OGG;
04822                 break;
04823             case CODEC_ID_WMAV1:
04824                 audio_format = AUDIO_WMA;
04825                 break;
04826             case CODEC_ID_WMAV2:
04827                 audio_format = AUDIO_WMA2;
04828                 break;
04829             default:
04830                 audio_format = AUDIO_WAV;
04831                 break;
04832         }
04833 
04834         lcd->setAudioFormatLEDs(audio_format, true);
04835 
04836         if (audioOut.do_passthru)
04837             lcd->setVariousLEDs(VARIOUS_SPDIF, true);
04838         else
04839             lcd->setVariousLEDs(VARIOUS_SPDIF, false);
04840 
04841         switch (audioIn.channels)
04842         {
04843             case 0:
04844             /* nb: aac and mp3 seem to be coming up 0 here, may point to an
04845              * avformatdecoder audio channel handling bug, per janneg */
04846             case 1:
04847             case 2:
04848                 /* all audio codecs have at *least* one channel, but
04849                  * LR is the fewest LED we can light up */
04850                 lcd->setSpeakerLEDs(SPEAKER_LR, true);
04851                 break;
04852             case 3:
04853             case 4:
04854             case 5:
04855             case 6:
04856                 lcd->setSpeakerLEDs(SPEAKER_51, true);
04857                 break;
04858             default:
04859                 lcd->setSpeakerLEDs(SPEAKER_71, true);
04860                 break;
04861         }
04862 
04863     }
04864     return true;
04865 }
04866 
04867 void AvFormatDecoder::av_update_stream_timings_video(AVFormatContext *ic)
04868 {
04869     int64_t start_time, start_time1, end_time, end_time1;
04870     int64_t duration, duration1, filesize;
04871     AVStream *st = NULL;
04872 
04873     start_time = INT64_MAX;
04874     end_time = INT64_MIN;
04875 
04876     for (uint i = 0; i < ic->nb_streams; i++)
04877     {
04878         AVStream *st1 = ic->streams[i];
04879         if (st1 && st1->codec->codec_type == AVMEDIA_TYPE_VIDEO)
04880         {
04881             st = st1;
04882             break;
04883         }
04884     }
04885     if (!st)
04886         return;
04887 
04888    duration = INT64_MIN;
04889    if (st->start_time != (int64_t)AV_NOPTS_VALUE && st->time_base.den) {
04890        start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
04891        if (start_time1 < start_time)
04892            start_time = start_time1;
04893        if (st->duration != (int64_t)AV_NOPTS_VALUE) {
04894            end_time1 = start_time1 +
04895                       av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
04896            if (end_time1 > end_time)
04897                end_time = end_time1;
04898        }
04899    }
04900    if (st->duration != (int64_t)AV_NOPTS_VALUE) {
04901        duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
04902        if (duration1 > duration)
04903            duration = duration1;
04904    }
04905     if (start_time != INT64_MAX) {
04906         ic->start_time = start_time;
04907         if (end_time != INT64_MIN) {
04908             if (end_time - start_time > duration)
04909                 duration = end_time - start_time;
04910         }
04911     }
04912     if (duration != INT64_MIN) {
04913         ic->duration = duration;
04914         if (ic->pb && (filesize = avio_size(ic->pb)) > 0) {
04915             /* compute the bitrate */
04916             ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
04917                 (double)ic->duration;
04918         }
04919     }
04920 }
04921 
04922 /* vim: set expandtab tabstop=4 shiftwidth=4: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends