Valkka  0.17.0
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  * Copyright 2017, 2018 Valkka Security Ltd. and 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 Affero 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 Affero General Public License for more details.
21  *
22  * You should have received a copy of the GNU Affero 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 // AVThread has std::vector<Decoder*> decoders
41 // decoders[1]->output() returns reference to Decoder::out_frame
42 // (VideoDecoder*)(..) returns reference to VideoDecoder::out_frame, right? (Decoder::out_frame) is hidden
43 //
44 // decoders[1] .. is a VideoDecoder.. but decoders has been declared as a vector Decoder
45 // Decoder* decoder = VideoDecoder()
46 // .. so, decoder->getWhatever, where getWhatever is virtual method, will always give the correct object
47 // but decoder->out_frame returns frame that depends on the cast
48 
49 // VideoDecoder->output() returns
50 
58 class Decoder
59 {
60 
61 public:
62  Decoder();
63  virtual ~Decoder();
64 
65 protected:
67  bool has_frame;
68 
69 public:
70  void input(Frame *f);
71  long int getMsTimestamp();
72  virtual Frame *output() = 0;
73  virtual void flush() = 0;
74  virtual bool pull() = 0;
75  bool hasFrame();
76 };
77 
84 class DummyDecoder : public Decoder
85 {
86 
87 private:
89 
90 public:
91  virtual Frame *output();
92  virtual void flush();
93  virtual bool pull();
94 };
95 
102 class AVDecoder : public Decoder
103 {
104 
105 public:
111  AVDecoder(AVCodecID av_codec_id, int n_threads = 1);
112  virtual ~AVDecoder();
113 
114 protected:
115  int n_threads;
116 
117 public:
118  AVCodecID av_codec_id;
119  AVPacket *av_packet;
120  AVCodecContext *av_codec_context;
121  AVCodec *av_codec;
122 
123 public:
124  // needs virtual void output, virtual void pull
125  virtual void flush();
126 };
127 
136 class VideoDecoder : public AVDecoder
137 {
138 
139 public:
140  VideoDecoder(AVCodecID av_codec_id, int n_threads = 1);
141  virtual ~VideoDecoder();
142 
143 protected:
144  AVBitmapFrame out_frame;
145  int width;
146  int height;
147  AVFrame *aux_av_frame;
148  AVPixelFormat current_pixel_format;
149  SwsContext *sws_ctx;
150  float secs_per_frame;
151 
152 public:
153  virtual Frame *output();
154  virtual bool pull();
155 
156  /*
157 private: // for simulating decoder slow down
158  std::random_device rd; //Will be used to obtain a seed for the random number engine
159  std::mt19937 gen; //Standard mersenne_twister_engine seeded with rd()
160  std::uniform_int_distribution<> dis;
161 */
162 };
163 
164 #endif
A Virtual class for decoders.
Definition: decoder.h:58
Custom payload Frame.
Definition: frame.h:160
virtual ~Decoder()
Default destructor.
Definition: decoder.cpp:43
Decoder using FFmpeg/libav.
Definition: decoder.h:102
AVCodecContext * av_codec_context
FFmpeg internal data structure.
Definition: decoder.h:120
AVCodecID av_codec_id
FFmpeg AVCodecId, identifying the codec.
Definition: decoder.h:118
AVCodec * av_codec
FFmpeg internal data structure.
Definition: decoder.h:121
Frame classes.
AVPacket * av_packet
FFmpeg internal data structure; encoded frame (say, H264)
Definition: decoder.h:119
virtual Frame * output()=0
< Return in_frame timestamp
Decoder()
Default constructor.
Definition: decoder.cpp:41
Video decoder using FFmpeg/libav.
Definition: decoder.h:136
Decoded YUV/RGB frame in FFMpeg format.
Definition: frame.h:354
BasicFrame in_frame
Payload data to be decoded.
Definition: decoder.h:66
Frame: An abstract queueable class.
Definition: frame.h:108
virtual void flush()=0
Reset decoder state. How to flush depends on the decoder library.
BasicFrame out_frame
Output frame: no decoding, just copy input here.
Definition: decoder.h:88
A Dummy decoder.
Definition: decoder.h:84
virtual bool pull()=0
Decode in_frame to out_frame. Return true if decoder returned a new frame (into out_frame), otherwise false. Implementation depends on the decoder library.
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:50