Valkka  1.6.1
OpenSource Video Management
decoder.h
Go to the documentation of this file.
1 #ifndef decoder_HEADER_GUARD
2 #define decoder_HEADER_GUARD
3 /*
4  * decoder.h : FFmpeg decoders
5  *
6  * (c) Copyright 2017-2024 Sampsa Riikonen
7  *
8  * Authors: Sampsa Riikonen <sampsa.riikonen@iki.fi>
9  *
10  * This file is part of the Valkka library.
11  *
12  * Valkka is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as
14  * published by the Free Software Foundation, either version 3 of the
15  * License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>
24  *
25  */
26 
37 #include "frame.h"
38 #include <random>
39 
40 // #define DECODE_VERBOSE
41 
42 // AVThread has std::vector<Decoder*> decoders
43 // decoders[1]->output() returns reference to Decoder::out_frame
44 // (VideoDecoder*)(..) returns reference to VideoDecoder::out_frame, right? (Decoder::out_frame) is hidden
45 //
46 // decoders[1] .. is a VideoDecoder.. but decoders has been declared as a vector Decoder
47 // Decoder* decoder = VideoDecoder()
48 // .. so, decoder->getWhatever, where getWhatever is virtual method, will always give the correct object
49 // but decoder->out_frame returns frame that depends on the cast
50 
51 // VideoDecoder->output() returns
52 
61 class Decoder
62 {
63 
64 public:
65  Decoder();
66  virtual ~Decoder();
67 
68 protected:
70  bool has_frame;
71 
72 public:
73  void input(Frame *f);
74  long int getMsTimestamp();
75  virtual Frame *output() = 0;
76  virtual void releaseOutput();
77  virtual void flush() = 0;
78  virtual bool pull() = 0;
79  virtual bool isOk();
80  bool hasFrame();
81 };
82 
89 class DummyDecoder : public Decoder
90 {
91 
92 private:
94 
95 public:
96  virtual Frame *output();
97  virtual void flush();
98  virtual bool pull();
99 };
100 
107 class AVDecoder : public Decoder
108 {
109 
110 public:
116  AVDecoder(AVCodecID av_codec_id, int n_threads = 1);
117  virtual ~AVDecoder();
118 
119 protected:
120  int n_threads;
121 
122 public:
123  AVCodecID av_codec_id;
124  AVPacket *av_packet;
125  AVCodecContext *av_codec_context;
126  AVCodec *av_codec;
127 
128 public:
129  // needs virtual void output, virtual void pull
130  virtual void flush();
131 };
132 
141 class VideoDecoder : public AVDecoder
142 {
143 
144 public:
145  VideoDecoder(AVCodecID av_codec_id, int n_threads = 1);
146  virtual ~VideoDecoder();
147 
148 protected:
149  AVBitmapFrame out_frame;
150  int width;
151  int height;
152  AVFrame *aux_av_frame;
153  AVPixelFormat current_pixel_format;
154  SwsContext *sws_ctx;
155  float secs_per_frame;
156 
157 public:
158  virtual Frame *output();
159  virtual bool pull();
160 
161  /*
162 private: // for simulating decoder slow down
163  std::random_device rd; //Will be used to obtain a seed for the random number engine
164  std::mt19937 gen; //Standard mersenne_twister_engine seeded with rd()
165  std::uniform_int_distribution<> dis;
166 */
167 };
168 
169 #endif
Decoded YUV/RGB frame in FFMpeg format.
Definition: frame.h:361
Decoder using FFmpeg/libav.
Definition: decoder.h:108
AVDecoder(AVCodecID av_codec_id, int n_threads=1)
Default constructor.
Definition: decoder.cpp:92
AVCodecContext * av_codec_context
FFmpeg internal data structure.
Definition: decoder.h:125
AVCodecID av_codec_id
FFmpeg AVCodecId, identifying the codec.
Definition: decoder.h:123
AVPacket * av_packet
FFmpeg internal data structure; encoded frame (say, H264)
Definition: decoder.h:124
AVCodec * av_codec
FFmpeg internal data structure.
Definition: decoder.h:126
virtual void flush()
Reset decoder state. How to flush depends on the decoder library.
Definition: decoder.cpp:160
Custom payload Frame.
Definition: frame.h:166
A Virtual class for decoders.
Definition: decoder.h:62
virtual bool pull()=0
Decode in_frame to out_frame. Return true if decoder returned a new frame (into out_frame),...
virtual Frame * output()=0
< Return in_frame timestamp
virtual void flush()=0
Reset decoder state. How to flush depends on the decoder library.
Decoder()
Default constructor.
Definition: decoder.cpp:39
BasicFrame in_frame
Payload data to be decoded.
Definition: decoder.h:69
virtual ~Decoder()
Default destructor.
Definition: decoder.cpp:42
virtual bool isOk()
The thread that uses this decoder can check if it has gone sour.
Definition: decoder.cpp:71
void input(Frame *f)
Create a copy of the frame into the internal storage of the decoder (i.e. to Decoder::in_frame)
Definition: decoder.cpp:49
virtual void releaseOutput()
Decoder might want to know that it's ok to overwrite the frame.
Definition: decoder.cpp:66
A Dummy decoder.
Definition: decoder.h:90
virtual bool pull()
Decode in_frame to out_frame. Return true if decoder returned a new frame (into out_frame),...
Definition: decoder.cpp:85
virtual Frame * output()
Return a reference to the internal storage of the decoder where the decoded frame is.
Definition: decoder.cpp:78
virtual void flush()
Reset decoder state. How to flush depends on the decoder library.
Definition: decoder.cpp:83
BasicFrame out_frame
Output frame: no decoding, just copy input here.
Definition: decoder.h:93
Frame: An abstract queueable class.
Definition: frame.h:112
Video decoder using FFmpeg/libav.
Definition: decoder.h:142
VideoDecoder(AVCodecID av_codec_id, int n_threads=1)
Default constructor.
Definition: decoder.cpp:167
virtual Frame * output()
Return a reference to the internal storage of the decoder where the decoded frame is.
Definition: decoder.cpp:202
virtual ~VideoDecoder()
Default destructor.
Definition: decoder.cpp:191
virtual bool pull()
Decode in_frame to out_frame. Return true if decoder returned a new frame (into out_frame),...
Definition: decoder.cpp:208
Frame classes.