• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_VIDEO_CODING_CODECS_MULTIPLEX_MULTIPLEX_ENCODED_IMAGE_PACKER_H_
12 #define MODULES_VIDEO_CODING_CODECS_MULTIPLEX_MULTIPLEX_ENCODED_IMAGE_PACKER_H_
13 
14 #include <cstdint>
15 #include <memory>
16 #include <vector>
17 
18 #include "api/video/encoded_image.h"
19 #include "api/video_codecs/video_codec.h"
20 
21 namespace webrtc {
22 
23 // Struct describing the whole bundle of multiple frames of an image.
24 // This struct is expected to be the set in the beginning of a picture's
25 // bitstream data.
26 struct MultiplexImageHeader {
27   // The number of frame components making up the complete picture data.
28   // For example, |frame_count| = 2 for the case of YUV frame with Alpha frame.
29   uint8_t component_count;
30 
31   // The increasing image ID given by the encoder. For different components
32   // of a single picture, they have the same |picture_index|.
33   uint16_t image_index;
34 
35   // The location of the first MultiplexImageComponentHeader in the bitstream,
36   // in terms of byte from the beginning of the bitstream.
37   uint32_t first_component_header_offset;
38 
39   // The location of the augmenting data in the bitstream, in terms of bytes
40   // from the beginning of the bitstream
41   uint32_t augmenting_data_offset;
42 
43   // The size of the augmenting data in the bitstream it terms of byte
44   uint16_t augmenting_data_size;
45 };
46 const int kMultiplexImageHeaderSize =
47     sizeof(uint8_t) + 2 * sizeof(uint16_t) + 2 * sizeof(uint32_t);
48 
49 // Struct describing the individual image component's content.
50 struct MultiplexImageComponentHeader {
51   // The location of the next MultiplexImageComponentHeader in the bitstream,
52   // in terms of the byte from the beginning of the bitstream;
53   uint32_t next_component_header_offset;
54 
55   // Identifies which component this frame represent, i.e. YUV frame vs Alpha
56   // frame.
57   uint8_t component_index;
58 
59   // The location of the real encoded image data of the frame in the bitstream,
60   // in terms of byte from the beginning of the bitstream.
61   uint32_t bitstream_offset;
62 
63   // Indicates the number of bytes of the encoded image data.
64   uint32_t bitstream_length;
65 
66   // Indicated the underlying VideoCodecType of the frame, i.e. VP9 or VP8 etc.
67   VideoCodecType codec_type;
68 
69   // Indicated the underlying frame is a key frame or delta frame.
70   VideoFrameType frame_type;
71 };
72 const int kMultiplexImageComponentHeaderSize =
73     sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint32_t) +
74     sizeof(uint8_t) + sizeof(uint8_t);
75 
76 // Struct holding the encoded image for one component.
77 struct MultiplexImageComponent {
78   // Indicated the underlying VideoCodecType of the frame, i.e. VP9 or VP8 etc.
79   VideoCodecType codec_type;
80 
81   // Identifies which component this frame represent, i.e. YUV frame vs Alpha
82   // frame.
83   uint8_t component_index;
84 
85   // Stores the actual frame data of the encoded image.
86   EncodedImage encoded_image;
87 };
88 
89 // Struct holding the whole frame bundle of components of an image.
90 struct MultiplexImage {
91   uint16_t image_index;
92   uint8_t component_count;
93   uint16_t augmenting_data_size;
94   std::unique_ptr<uint8_t[]> augmenting_data;
95   std::vector<MultiplexImageComponent> image_components;
96 
97   MultiplexImage(uint16_t picture_index,
98                  uint8_t component_count,
99                  std::unique_ptr<uint8_t[]> augmenting_data,
100                  uint16_t augmenting_data_size);
101 };
102 
103 // A utility class providing conversion between two representations of a
104 // multiplex image frame:
105 // 1. Packed version is just one encoded image, we pack all necessary metadata
106 //    in the bitstream as headers.
107 // 2. Unpacked version is essentially a list of encoded images, one for one
108 //    component.
109 class MultiplexEncodedImagePacker {
110  public:
111   // Note: It is caller responsibility to release the buffer of the result.
112   static EncodedImage PackAndRelease(const MultiplexImage& image);
113 
114   // Note: The image components just share the memory with |combined_image|.
115   static MultiplexImage Unpack(const EncodedImage& combined_image);
116 };
117 
118 }  // namespace webrtc
119 
120 #endif  // MODULES_VIDEO_CODING_CODECS_MULTIPLEX_MULTIPLEX_ENCODED_IMAGE_PACKER_H_
121