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
47namespace ojph
48{
49 namespace thds
50 { class thread_pool; }
51
52namespace stex // stream expand
53{
54
55// defined here
56class packets_handler;
57class frames_handler;
58
59// defined elsewhere
60struct j2k_frame_storer;
61
63//
64//
65//
66//
67//
69
70/*****************************************************************************/
79{
85 {
86 PT_BODY = 0, // this is body packet
89 PT_MAIN = 3, // frame has only one main packet
90 };
91public:
95 rtp_packet() { num_bytes = 0; next = NULL; }
96
102 void init(rtp_packet* next) { this->next = next; }
103
104public:
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; }
113 ui32 result = ntohs(*(ui16*)(data + 2));
114 result |= ((ui32)data[15]) << 16; // extended sequence (ESEQ)
115 return result;
116 }
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
124 { return ((ui32)data[12]) >> 6; }
126 { return (((ui32)data[12]) >> 3) & 0x7; }
128 if (get_packet_type() != PT_BODY) return ((ui32)data[12]) & 0x7;
129 else return (((ui32)data[13]) >> 7) & 0x1;
130 }
132 ui32 result = (((ui32)data[13]) & 0xF) << 8;
133 result |= (ui32)data[14];
134 return result;
135 }
137 { return data + 20; }
139 { return (ui32)num_bytes - 20; }
140
141 // only in main payload header
143 assert(get_packet_type() != PT_BODY);
144 return (((ui32)data[13]) & 0x80) != 0;
145 }
147 assert(get_packet_type() != PT_BODY);
148 return (((ui32)data[13]) >> 4) & 0x7;
149 }
151 assert(get_packet_type() != PT_BODY);
152 return (((ui32)data[16]) & 0x80) != 0;
153 }
155 assert(get_packet_type() != PT_BODY);
156 return (((ui32)data[16]) & 0x40) != 0;
157 }
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 }
167 assert(get_packet_type() != PT_BODY);
168 return (ui32)data[17];
169 }
171 assert(get_packet_type() != PT_BODY);
172 return (ui32)data[18];
173 }
175 assert(get_packet_type() != PT_BODY);
176 return (ui32)data[19];
177 }
178
179 // only in body payload header
181 assert(get_packet_type() == PT_BODY);
182 return ((ui32)data[12]) & 0x7;
183 }
185 assert(get_packet_type() == PT_BODY);
186 return (((ui32)data[13]) >> 4) & 0x7;
187 }
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 }
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
205public:
206 static constexpr int max_size = 2048;
207 // ethernet packet are only 1500
211};
212
214//
215//
216//
217//
218//
220
221/*****************************************************************************/
248{
249public:
254 {
255 quiet = false;
256 avail = in_use = NULL;
258 frames = NULL;
259 num_packets = 0;
260 packet_store = NULL;
261 }
267
268public:
281
295
303
308 void flush();
309
310private:
316 void consume_packet();
317
318private:
319 bool quiet;
325
328};
329
331//
332//
333//
334//
335//
337
338/*****************************************************************************/
353struct stex_file {
354public:
359 {
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
369public:
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
399
400public:
404 std::atomic_int done;
407
408 const char *name_template;
410
412};
413
415//
416//
417//
418//
419//
421
422/*****************************************************************************/
429{
430public:
435 {
436 quiet = false;
437 num_threads = 0;
438 target_name = NULL;
439 num_files = 0;
442 files_store = in_use = avail = processing = NULL;
443 num_complete_files.store(0);
444 thread_pool = NULL;
445 storers_store = NULL;
446 }
451
452public:
465 void init(bool quiet, const char *target_name,
467
477 void push(rtp_packet* p);
478
491
496 bool flush();
497
507 { num_complete_files.fetch_add(1, std::memory_order_release); }
508
509private:
519
527 void send_to_processing();
528
529private:
530 bool quiet;
532 const char *target_name;
543 std::atomic_int32_t
544 num_complete_files; //<!num. of files for which processing is complete
549};
550
551} // !stex namespace
552} // !ojph namespace
553
554#endif
mem_outfile stores encoded j2k codestreams in memory
Definition ojph_file.h:127
Assumes packets arrive in order.
ui32 num_threads
number of threads used for saving
j2k_frame_storer * storers_store
address for allocated frame storers
ui32 total_frames
total number of frames that were observed
stex_file * in_use
the frame that is being filled with data
void push(rtp_packet *p)
call this function to push rtp_packets to this object
frames_handler()
default construction
bool flush()
This function is not used, and therefore it is not clear how to use it.
void get_stats(ui32 &total_frames, ui32 &trunc_frames, ui32 &lost_frames)
call this function to collect statistics about frames
stex_file * processing
frames that are being saved
stex_file * avail
available frames structures
ui32 last_time_stamp
last observed time stamp
ui32 last_seq_number
last observed sequence number
ui32 trunc_frames
truncated frames (because of a packet lostt)
void check_files_in_processing()
call this function to process stex_file for which processing is complete
void increment_num_complete_files()
other threads call this function to let frames_handler know that processing is done.
void send_to_processing()
Handles complete/truncated files and send them for storing.
ui32 lost_frames
frames for which main header was not received
void init(bool quiet, const char *target_name, thds::thread_pool *thread_pool)
call this function to initialize this object
const char * target_name
target file name template
thds::thread_pool * thread_pool
thread pool for processing frames
bool quiet
no informational info is printed when true
std::atomic_int32_t num_complete_files
ui32 num_files
maximum number of in-flight files.
stex_file * files_store
address for allocated files
Interprets new packets, buffers them if needed.
rtp_packet * packet_store
address of packet memory allocation
rtp_packet * exchange(rtp_packet *p)
Call this function to get a packet from the packet chain.
frames_handler * frames
frames object
void consume_packet()
This function sends the packet in in_use (oldest) to frames handler object.
ui32 get_num_lost_packets() const
This function provides information about the observed number of lost packets.
bool quiet
no informational info is printed when true
ui32 num_packets
maximum number of packets in packet_store
rtp_packet * in_use
start of used packet chain
ui32 last_seq_num
the last observed sequence number
ui32 lost_packets
number of lost packets – just statistics
void flush()
This function is not used, and therefore it is not clear how to use it.
rtp_packet * avail
start of available packets chain
void init(bool quiet, ui32 num_packets, frames_handler *frames)
call this to initialize packets_handler
Implements a pool of threads, and can queue tasks.
uint16_t ui16
Definition ojph_defs.h:52
uint32_t ui32
Definition ojph_defs.h:54
uint8_t ui8
Definition ojph_defs.h:50
Saves a j2k frame to disk without decoding.
inteprets RTP header and payload, and holds received packets.
ui32 num_bytes
number of bytes
rtp_packet()
default constructor
ui8 data[max_size]
data in the packet
static constexpr int max_size
maximum packet size
rtp_packet * next
used for linking packets
void init(rtp_packet *next)
Call this to link packets.
packet_type
packet types based on the main header of draft-ietf-avtcore-rtp-j2k-scl-00
holds in memory j2k codestream together with other info
frames_handler * parent
the object holding this frame
const char * name_template
name template for saved files
ojph::mem_outfile f
holds in-memory j2k codestream
ui32 frame_idx
frame number in the sequence
void init(frames_handler *parent, stex_file *next, j2k_frame_storer *storer, const char *name_template)
call this function to initialize stex_file
void notify_file_completion()
other threads can call this function to signal completion of processing.
ui32 last_seen_seq
the last seen RTP sequence number
stex_file * next
used to create files chain
stex_file()
default constructor
ui32 time_stamp
time stamp at which this file must be displayed
std::atomic_int done
saving is completed when 0 is reached
j2k_frame_storer * storer
stores a j2k frame using another thread