File indexing completed on 2025-01-19 03:57:05

0001 /*********************************************************
0002  * Copyright (C) 2020, Val Doroshchuk <valbok@gmail.com> *
0003  *                                                       *
0004  * This file is part of QtAVPlayer.                      *
0005  * Free Qt Media Player based on FFmpeg.                 *
0006  *********************************************************/
0007 
0008 #include "qavpacket_p.h"
0009 #include "qavcodec_p.h"
0010 #include "qavstream.h"
0011 #include <QSharedPointer>
0012 #include <QDebug>
0013 
0014 extern "C" {
0015 #include <libavcodec/avcodec.h>
0016 #include <libavformat/avformat.h>
0017 #include <libavutil/opt.h>
0018 }
0019 
0020 QT_BEGIN_NAMESPACE
0021 
0022 class QAVPacketPrivate
0023 {
0024 public:
0025     AVPacket *pkt = nullptr;
0026     QAVStream stream;
0027 };
0028 
0029 QAVPacket::QAVPacket()
0030     : d_ptr(new QAVPacketPrivate)
0031 {
0032     d_ptr->pkt = av_packet_alloc();
0033     d_ptr->pkt->size = 0;
0034     d_ptr->pkt->stream_index = -1;
0035     d_ptr->pkt->pts = AV_NOPTS_VALUE;
0036 }
0037 
0038 QAVPacket::QAVPacket(const QAVPacket &other)
0039     : QAVPacket()
0040 {
0041     *this = other;
0042 }
0043 
0044 QAVPacket &QAVPacket::operator=(const QAVPacket &other)
0045 {
0046     av_packet_unref(d_ptr->pkt);
0047     av_packet_ref(d_ptr->pkt, other.d_ptr->pkt);
0048 
0049     d_ptr->stream = other.d_ptr->stream;
0050 
0051     return *this;
0052 }
0053 
0054 QAVPacket::operator bool() const
0055 {
0056     Q_D(const QAVPacket);
0057     return d->pkt->size;
0058 }
0059 
0060 QAVPacket::~QAVPacket()
0061 {
0062     Q_D(QAVPacket);
0063     av_packet_free(&d->pkt);
0064 }
0065 
0066 AVPacket *QAVPacket::packet() const
0067 {
0068     return d_func()->pkt;
0069 }
0070 
0071 double QAVPacket::duration() const
0072 {
0073     Q_D(const QAVPacket);
0074     if (!d->stream)
0075         return 0.0;
0076     auto tb = d->stream.stream()->time_base;
0077     return tb.num && tb.den ? d->pkt->duration * av_q2d(tb) : 0.0;
0078 }
0079 
0080 double QAVPacket::pts() const
0081 {
0082     Q_D(const QAVPacket);
0083     if (!d->stream)
0084         return 0.0;
0085     auto tb = d->stream.stream()->time_base;
0086     return tb.num && tb.den ? d->pkt->pts * av_q2d(tb) : 0.0;
0087 }
0088 
0089 QAVStream QAVPacket::stream() const
0090 {
0091     Q_D(const QAVPacket);
0092     return d->stream;
0093 }
0094 
0095 void QAVPacket::setStream(const QAVStream &stream)
0096 {
0097     Q_D(QAVPacket);
0098     d->stream = stream;
0099 }
0100 
0101 int QAVPacket::send() const
0102 {
0103     Q_D(const QAVPacket);
0104     return d->stream ? d->stream.codec()->write(*this) : 0;
0105 }
0106 
0107 QT_END_NAMESPACE