OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
stream_expand_support.h
Go to the documentation of this file.
1//***************************************************************************/
2// This software is released under the 2-Clause BSD license, included
3// below.
4//
5// Copyright (c) 2024, Aous Naman
6// Copyright (c) 2024, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2024, The University of New South Wales, Australia
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// 1. Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//
16// 2. Redistributions in binary form must reproduce the above copyright
17// notice, this list of conditions and the following disclaimer in the
18// documentation and/or other materials provided with the distribution.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//***************************************************************************/
32// This file is part of the OpenJPH software implementation.
33// File: stream_expand_support.cpp
34// Author: Aous Naman
35// Date: 18 April 2024
36//***************************************************************************/
37
38#ifndef OJPH_STR_EX_SUPPORT_H
39#define OJPH_STR_EX_SUPPORT_H
40
41#include <atomic>
42#include <cassert>
43#include "ojph_base.h"
44#include "ojph_file.h"
45#include "ojph_sockets.h"
46
47 namespace ojph
48{
49 namespace thds
50 { class thread_pool; }
51
52 namespace stex // stream expand
53{
54
55// defined here
56 class packets_handler;
57 class frames_handler;
58
59// defined elsewhere
60 struct j2k_frame_storer;
61
63//
64//
65//
66//
67//
69
70/*****************************************************************************/
78 struct rtp_packet
79{
84 enum packet_type : ui32
85 {
86 PT_BODY = 0, // this is body packet
87 PT_MAIN_FOLLOWED_BY_MAIN = 1,
88 PT_MAIN_FOLLOWED_BY_BODY = 2,
89 PT_MAIN = 3, // frame has only one main packet
90 };
91 public:
95 rtp_packet() { num_bytes = 0; next = NULL; }
96
102 void init(rtp_packet* next) { this->next = next; }
103
104 public:
105 // RTP header
106 ui32 get_rtp_version() { return ((ui32)data[0]) >> 6; }
107 bool is_padded() { return (data[0] & 0x20) != 0; }
108 bool is_extended() { return (data[0] & 0x10) != 0; }
109 ui32 get_csrc_count() { return (ui32)(data[0]) & 0xF; }
110 bool is_marked() { return (data[1] & 0x80) != 0; }
111 ui32 get_payload_type() { return (ui32)(data[1]) & 0x7F; }
112 ui32 get_seq_num() {
113 ui32 result = ntohs(*(ui16*)(data + 2));
114 result |= ((ui32)data[15]) << 16; // extended sequence (ESEQ)
115 return result;
116 }
117 ui32 get_time_stamp()
118 { return ntohl(*(ui32*)(data + 4)); }
119 ui32 get_ssrc() // not used for the time being
120 { return ntohl(*(ui32*)(data + 8)); }
121
122 // common in main and body payload headers
123 ui32 get_packet_type()
124 { return ((ui32)data[12]) >> 6; }
125 ui32 get_TP()
126 { return (((ui32)data[12]) >> 3) & 0x7; }
127 ui32 get_ORDH() {
128 if (get_packet_type() != PT_BODY) return ((ui32)data[12]) & 0x7;
129 else return (((ui32)data[13]) >> 7) & 0x1;
130 }
131 ui32 get_PTSTAMP() {
132 ui32 result = (((ui32)data[13]) & 0xF) << 8;
133 result |= (ui32)data[14];
134 return result;
135 }
136 ui8* get_data()
137 { return data + 20; }
138 ui32 get_data_size()
139 { return (ui32)num_bytes - 20; }
140
141 // only in main payload header
142 bool is_PTSTAMP_used() {
143 assert(get_packet_type() != PT_BODY);
144 return (((ui32)data[13]) & 0x80) != 0;
145 }
146 ui32 get_XTRAC() {
147 assert(get_packet_type() != PT_BODY);
148 return (((ui32)data[13]) >> 4) & 0x7;
149 }
150 bool is_codestream_header_reusable() {
151 assert(get_packet_type() != PT_BODY);
152 return (((ui32)data[16]) & 0x80) != 0;
153 }
154 bool is_component_colorimetry_used() {
155 assert(get_packet_type() != PT_BODY);
156 return (((ui32)data[16]) & 0x40) != 0;
157 }
158 bool is_codeblock_caching_used() {
159 assert(get_packet_type() != PT_BODY);
160 return (((ui32)data[16]) & 0x20) != 0;
161 }
162 bool is_RANGE() {
163 assert(get_packet_type() != PT_BODY);
164 return ((ui32)data[16] & 1) != 0;
165 }
166 ui32 get_PRIMS(){
167 assert(get_packet_type() != PT_BODY);
168 return (ui32)data[17];
169 }
170 ui32 get_TRANS() {
171 assert(get_packet_type() != PT_BODY);
172 return (ui32)data[18];
173 }
174 ui32 get_MAT() {
175 assert(get_packet_type() != PT_BODY);
176 return (ui32)data[19];
177 }
178
179 // only in body payload header
180 ui32 get_RES() {
181 assert(get_packet_type() == PT_BODY);
182 return ((ui32)data[12]) & 0x7;
183 }
184 ui32 get_QUAL() {
185 assert(get_packet_type() == PT_BODY);
186 return (((ui32)data[13]) >> 4) & 0x7;
187 }
188 ui32 get_data_pos() {
189 ui32 result = 0;
190 if (get_packet_type() == PT_BODY) {
191 result = ((ui32)data[16]) << 4;
192 result |= (((ui32)data[17]) >> 4) & 0xF;
193 }
194 return result;
195 }
196 ui32 get_PID() {
197 assert(get_packet_type() == PT_BODY);
198 ui32 result = (((ui32)data[17]) & 0xF) << 16;
199 result |= ((ui32)data[18]) << 8;
200 result |= ((ui32)data[19]);
201 return result;
202 }
203
204
205 public:
206 static constexpr int max_size = 2048;
207 // ethernet packet are only 1500
208 ui8 data[max_size];
209 ui32 num_bytes;
210 rtp_packet* next;
211};
212
214//
215//
216//
217//
218//
220
221/*****************************************************************************/
247 class packets_handler
248{
249 public:
253 packets_handler()
254 {
255 quiet = false;
256 avail = in_use = NULL;
257 last_seq_num = lost_packets = 0;
258 frames = NULL;
259 num_packets = 0;
260 packet_store = NULL;
261 }
262
265 ~packets_handler()
266 { if (packet_store) delete[] packet_store; }
267
268 public:
280 void init(bool quiet, ui32 num_packets, frames_handler* frames);
281
294 rtp_packet* exchange(rtp_packet* p);
295
302 ui32 get_num_lost_packets() const { return lost_packets; }
303
308 void flush();
309
310 private:
316 void consume_packet();
317
318 private:
319 bool quiet;
320 rtp_packet* avail;
321 rtp_packet* in_use;
322 ui32 last_seq_num;
323 ui32 lost_packets;
324 frames_handler* frames;
325
326 ui32 num_packets;
327 rtp_packet* packet_store;
328};
329
331//
332//
333//
334//
335//
337
338/*****************************************************************************/
353 struct stex_file {
354 public:
358 stex_file()
359 {
360 time_stamp = last_seen_seq = 0;
361 done.store(0, std::memory_order_relaxed);
362 frame_idx = 0;
363 parent = NULL;
364 name_template = NULL;
365 storer = NULL;
366 next = NULL;
367 }
368
369 public:
381 void init(frames_handler* parent, stex_file* next, j2k_frame_storer *storer,
382 const char *name_template)
383 {
384 this->parent = parent;
385 this->name_template = name_template;
386 this->next = next;
387 this->storer = storer;
388 }
389
398 void notify_file_completion();
399
400 public:
401 ojph::mem_outfile f;
402 ui32 time_stamp;
403 ui32 last_seen_seq;
404 std::atomic_int done;
405 ui32 frame_idx;
406 frames_handler* parent;
407
408 const char *name_template;
409 j2k_frame_storer* storer;
410
411 stex_file* next;
412};
413
415//
416//
417//
418//
419//
421
422/*****************************************************************************/
428 class frames_handler
429{
430 public:
434 frames_handler()
435 {
436 quiet = false;
437 num_threads = 0;
438 target_name = NULL;
439 num_files = 0;
440 last_seq_number = last_time_stamp = 0;
441 total_frames = trunc_frames = lost_frames = 0;
442 files_store = in_use = avail = processing = NULL;
443 num_complete_files.store(0);
444 thread_pool = NULL;
445 storers_store = NULL;
446 }
447
450 ~frames_handler();
451
452 public:
465 void init(bool quiet, const char *target_name,
466 thds::thread_pool* thread_pool);
467
477 void push(rtp_packet* p);
478
490 void get_stats(ui32& total_frames, ui32& trunc_frames, ui32& lost_frames);
491
496 bool flush();
497
506 void increment_num_complete_files()
507 { num_complete_files.fetch_add(1, std::memory_order_release); }
508
509 private:
518 void check_files_in_processing();
519
527 void send_to_processing();
528
529 private:
530 bool quiet;
531 ui32 num_threads;
532 const char *target_name;
533 ui32 num_files;
534 ui32 last_seq_number;
535 ui32 last_time_stamp;
536 ui32 total_frames;
537 ui32 trunc_frames;
538 ui32 lost_frames;
539 stex_file* files_store;
540 stex_file* in_use;
541 stex_file* avail;
542 stex_file* processing;
543 std::atomic_int32_t
544 num_complete_files; //<!num. of files for which processing is complete
545 thds::thread_pool*
546 thread_pool;
547 j2k_frame_storer*
548 storers_store;
549};
550
551} // !stex namespace
552} // !ojph namespace
553
554#endif
ojph::mem_outfile
mem_outfile stores encoded j2k codestreams in memory
Definition ojph_file.h:127
ojph::stex::frames_handler
Assumes packets arrive in order.
Definition stream_expand_support.h:429
ojph::stex::frames_handler::num_threads
ui32 num_threads
number of threads used for saving
Definition stream_expand_support.h:531
ojph::stex::frames_handler::storers_store
j2k_frame_storer * storers_store
address for allocated frame storers
Definition stream_expand_support.h:548
ojph::stex::frames_handler::total_frames
ui32 total_frames
total number of frames that were observed
Definition stream_expand_support.h:536
ojph::stex::frames_handler::in_use
stex_file * in_use
the frame that is being filled with data
Definition stream_expand_support.h:540
ojph::stex::frames_handler::push
void push(rtp_packet *p)
call this function to push rtp_packets to this object
Definition stream_expand_support.cpp:291
ojph::stex::frames_handler::frames_handler
frames_handler()
default construction
Definition stream_expand_support.h:434
ojph::stex::frames_handler::flush
bool flush()
This function is not used, and therefore it is not clear how to use it.
Definition stream_expand_support.cpp:386
ojph::stex::frames_handler::get_stats
void get_stats(ui32 &total_frames, ui32 &trunc_frames, ui32 &lost_frames)
call this function to collect statistics about frames
Definition stream_expand_support.cpp:377
ojph::stex::frames_handler::processing
stex_file * processing
frames that are being saved
Definition stream_expand_support.h:542
ojph::stex::frames_handler::avail
stex_file * avail
available frames structures
Definition stream_expand_support.h:541
ojph::stex::frames_handler::last_time_stamp
ui32 last_time_stamp
last observed time stamp
Definition stream_expand_support.h:535
ojph::stex::frames_handler::last_seq_number
ui32 last_seq_number
last observed sequence number
Definition stream_expand_support.h:534
ojph::stex::frames_handler::trunc_frames
ui32 trunc_frames
truncated frames (because of a packet lostt)
Definition stream_expand_support.h:537
ojph::stex::frames_handler::check_files_in_processing
void check_files_in_processing()
call this function to process stex_file for which processing is complete
Definition stream_expand_support.cpp:405
ojph::stex::frames_handler::increment_num_complete_files
void increment_num_complete_files()
other threads call this function to let frames_handler know that processing is done.
Definition stream_expand_support.h:506
ojph::stex::frames_handler::~frames_handler
~frames_handler()
default destructor
Definition stream_expand_support.cpp:257
ojph::stex::frames_handler::send_to_processing
void send_to_processing()
Handles complete/truncated files and send them for storing.
Definition stream_expand_support.cpp:447
ojph::stex::frames_handler::lost_frames
ui32 lost_frames
frames for which main header was not received
Definition stream_expand_support.h:538
ojph::stex::frames_handler::init
void init(bool quiet, const char *target_name, thds::thread_pool *thread_pool)
call this function to initialize this object
Definition stream_expand_support.cpp:266
ojph::stex::frames_handler::target_name
const char * target_name
target file name template
Definition stream_expand_support.h:532
ojph::stex::frames_handler::thread_pool
thds::thread_pool * thread_pool
thread pool for processing frames
Definition stream_expand_support.h:546
ojph::stex::frames_handler::quiet
bool quiet
no informational info is printed when true
Definition stream_expand_support.h:530
ojph::stex::frames_handler::num_complete_files
std::atomic_int32_t num_complete_files
Definition stream_expand_support.h:544
ojph::stex::frames_handler::num_files
ui32 num_files
maximum number of in-flight files.
Definition stream_expand_support.h:533
ojph::stex::frames_handler::files_store
stex_file * files_store
address for allocated files
Definition stream_expand_support.h:539
ojph::stex::packets_handler
Interprets new packets, buffers them if needed.
Definition stream_expand_support.h:248
ojph::stex::packets_handler::packet_store
rtp_packet * packet_store
address of packet memory allocation
Definition stream_expand_support.h:327
ojph::stex::packets_handler::~packets_handler
~packets_handler()
default destructor
Definition stream_expand_support.h:265
ojph::stex::packets_handler::exchange
rtp_packet * exchange(rtp_packet *p)
Call this function to get a packet from the packet chain.
Definition stream_expand_support.cpp:120
ojph::stex::packets_handler::frames
frames_handler * frames
frames object
Definition stream_expand_support.h:324
ojph::stex::packets_handler::consume_packet
void consume_packet()
This function sends the packet in in_use (oldest) to frames handler object.
Definition stream_expand_support.cpp:221
ojph::stex::packets_handler::get_num_lost_packets
ui32 get_num_lost_packets() const
This function provides information about the observed number of lost packets.
Definition stream_expand_support.h:302
ojph::stex::packets_handler::quiet
bool quiet
no informational info is printed when true
Definition stream_expand_support.h:319
ojph::stex::packets_handler::num_packets
ui32 num_packets
maximum number of packets in packet_store
Definition stream_expand_support.h:326
ojph::stex::packets_handler::in_use
rtp_packet * in_use
start of used packet chain
Definition stream_expand_support.h:321
ojph::stex::packets_handler::last_seq_num
ui32 last_seq_num
the last observed sequence number
Definition stream_expand_support.h:322
ojph::stex::packets_handler::lost_packets
ui32 lost_packets
number of lost packets – just statistics
Definition stream_expand_support.h:323
ojph::stex::packets_handler::packets_handler
packets_handler()
default constructor
Definition stream_expand_support.h:253
ojph::stex::packets_handler::flush
void flush()
This function is not used, and therefore it is not clear how to use it.
Definition stream_expand_support.cpp:208
ojph::stex::packets_handler::avail
rtp_packet * avail
start of available packets chain
Definition stream_expand_support.h:320
ojph::stex::packets_handler::init
void init(bool quiet, ui32 num_packets, frames_handler *frames)
call this to initialize packets_handler
Definition stream_expand_support.cpp:105
ojph::thds::thread_pool
Implements a pool of threads, and can queue tasks.
Definition ojph_threads.h:98
ojph::stex
Definition stream_expand_support.cpp:47
ojph::thds
Definition ojph_threads.h:51
ojph
Definition ojph_img_io.h:52
ojph::ui16
uint16_t ui16
Definition ojph_defs.h:52
ojph::ui32
uint32_t ui32
Definition ojph_defs.h:54
ojph::ui8
uint8_t ui8
Definition ojph_defs.h:50
ojph_base.h
ojph_file.h
ojph_sockets.h
ojph::stex::j2k_frame_storer
Saves a j2k frame to disk without decoding.
Definition threaded_frame_processors.h:65
ojph::stex::rtp_packet
interprets RTP header and payload, and holds received packets.
Definition stream_expand_support.h:79
ojph::stex::rtp_packet::num_bytes
ui32 num_bytes
number of bytes
Definition stream_expand_support.h:209
ojph::stex::rtp_packet::get_XTRAC
ui32 get_XTRAC()
Definition stream_expand_support.h:146
ojph::stex::rtp_packet::get_data_pos
ui32 get_data_pos()
Definition stream_expand_support.h:188
ojph::stex::rtp_packet::get_payload_type
ui32 get_payload_type()
Definition stream_expand_support.h:111
ojph::stex::rtp_packet::get_time_stamp
ui32 get_time_stamp()
Definition stream_expand_support.h:117
ojph::stex::rtp_packet::get_TP
ui32 get_TP()
Definition stream_expand_support.h:125
ojph::stex::rtp_packet::is_codestream_header_reusable
bool is_codestream_header_reusable()
Definition stream_expand_support.h:150
ojph::stex::rtp_packet::rtp_packet
rtp_packet()
default constructor
Definition stream_expand_support.h:95
ojph::stex::rtp_packet::get_PRIMS
ui32 get_PRIMS()
Definition stream_expand_support.h:166
ojph::stex::rtp_packet::get_seq_num
ui32 get_seq_num()
Definition stream_expand_support.h:112
ojph::stex::rtp_packet::get_PID
ui32 get_PID()
Definition stream_expand_support.h:196
ojph::stex::rtp_packet::get_TRANS
ui32 get_TRANS()
Definition stream_expand_support.h:170
ojph::stex::rtp_packet::get_QUAL
ui32 get_QUAL()
Definition stream_expand_support.h:184
ojph::stex::rtp_packet::get_MAT
ui32 get_MAT()
Definition stream_expand_support.h:174
ojph::stex::rtp_packet::get_ssrc
ui32 get_ssrc()
Definition stream_expand_support.h:119
ojph::stex::rtp_packet::get_RES
ui32 get_RES()
Definition stream_expand_support.h:180
ojph::stex::rtp_packet::is_extended
bool is_extended()
Definition stream_expand_support.h:108
ojph::stex::rtp_packet::get_data_size
ui32 get_data_size()
Definition stream_expand_support.h:138
ojph::stex::rtp_packet::get_csrc_count
ui32 get_csrc_count()
Definition stream_expand_support.h:109
ojph::stex::rtp_packet::get_data
ui8 * get_data()
Definition stream_expand_support.h:136
ojph::stex::rtp_packet::data
ui8 data[max_size]
data in the packet
Definition stream_expand_support.h:208
ojph::stex::rtp_packet::max_size
static constexpr int max_size
maximum packet size
Definition stream_expand_support.h:206
ojph::stex::rtp_packet::is_PTSTAMP_used
bool is_PTSTAMP_used()
Definition stream_expand_support.h:142
ojph::stex::rtp_packet::next
rtp_packet * next
used for linking packets
Definition stream_expand_support.h:210
ojph::stex::rtp_packet::is_padded
bool is_padded()
Definition stream_expand_support.h:107
ojph::stex::rtp_packet::init
void init(rtp_packet *next)
Call this to link packets.
Definition stream_expand_support.h:102
ojph::stex::rtp_packet::is_component_colorimetry_used
bool is_component_colorimetry_used()
Definition stream_expand_support.h:154
ojph::stex::rtp_packet::get_packet_type
ui32 get_packet_type()
Definition stream_expand_support.h:123
ojph::stex::rtp_packet::get_PTSTAMP
ui32 get_PTSTAMP()
Definition stream_expand_support.h:131
ojph::stex::rtp_packet::get_rtp_version
ui32 get_rtp_version()
Definition stream_expand_support.h:106
ojph::stex::rtp_packet::is_codeblock_caching_used
bool is_codeblock_caching_used()
Definition stream_expand_support.h:158
ojph::stex::rtp_packet::packet_type
packet_type
packet types based on the main header of draft-ietf-avtcore-rtp-j2k-scl-00
Definition stream_expand_support.h:85
ojph::stex::rtp_packet::PT_MAIN_FOLLOWED_BY_MAIN
@ PT_MAIN_FOLLOWED_BY_MAIN
Definition stream_expand_support.h:87
ojph::stex::rtp_packet::PT_MAIN
@ PT_MAIN
Definition stream_expand_support.h:89
ojph::stex::rtp_packet::PT_BODY
@ PT_BODY
Definition stream_expand_support.h:86
ojph::stex::rtp_packet::PT_MAIN_FOLLOWED_BY_BODY
@ PT_MAIN_FOLLOWED_BY_BODY
Definition stream_expand_support.h:88
ojph::stex::rtp_packet::is_RANGE
bool is_RANGE()
Definition stream_expand_support.h:162
ojph::stex::rtp_packet::get_ORDH
ui32 get_ORDH()
Definition stream_expand_support.h:127
ojph::stex::rtp_packet::is_marked
bool is_marked()
Definition stream_expand_support.h:110
ojph::stex::stex_file
holds in memory j2k codestream together with other info
Definition stream_expand_support.h:353
ojph::stex::stex_file::parent
frames_handler * parent
the object holding this frame
Definition stream_expand_support.h:406
ojph::stex::stex_file::name_template
const char * name_template
name template for saved files
Definition stream_expand_support.h:408
ojph::stex::stex_file::f
ojph::mem_outfile f
holds in-memory j2k codestream
Definition stream_expand_support.h:401
ojph::stex::stex_file::frame_idx
ui32 frame_idx
frame number in the sequence
Definition stream_expand_support.h:405
ojph::stex::stex_file::init
void init(frames_handler *parent, stex_file *next, j2k_frame_storer *storer, const char *name_template)
call this function to initialize stex_file
Definition stream_expand_support.h:381
ojph::stex::stex_file::notify_file_completion
void notify_file_completion()
other threads can call this function to signal completion of processing.
Definition stream_expand_support.cpp:241
ojph::stex::stex_file::last_seen_seq
ui32 last_seen_seq
the last seen RTP sequence number
Definition stream_expand_support.h:403
ojph::stex::stex_file::next
stex_file * next
used to create files chain
Definition stream_expand_support.h:411
ojph::stex::stex_file::stex_file
stex_file()
default constructor
Definition stream_expand_support.h:358
ojph::stex::stex_file::time_stamp
ui32 time_stamp
time stamp at which this file must be displayed
Definition stream_expand_support.h:402
ojph::stex::stex_file::done
std::atomic_int done
saving is completed when 0 is reached
Definition stream_expand_support.h:404
ojph::stex::stex_file::storer
j2k_frame_storer * storer
stores a j2k frame using another thread
Definition stream_expand_support.h:409