FFmpeg  4.4.9
rtpenc_vc2hq.c
Go to the documentation of this file.
1 /*
2  * RTP packetizer for VC-2 HQ payload format (draft version 1) - experimental
3  * Copyright (c) 2016 Thomas Volkert <thomas@netzeal.de>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/dirac.h"
24 #include "libavcodec/get_bits.h"
25 #include "libavcodec/golomb.h"
26 
27 #include "avformat.h"
28 #include "rtpenc.h"
29 
30 #define RTP_VC2HQ_PL_HEADER_SIZE 4
31 
32 #define DIRAC_DATA_UNIT_HEADER_SIZE 13
33 #define DIRAC_PIC_NR_SIZE 4
34 #define DIRAC_RTP_PCODE_HQ_PIC_FRAGMENT 0xEC
35 
36 static int send_packet(AVFormatContext *ctx, uint8_t parse_code, int info_hdr_size, const uint8_t *buf, int size, int i, int f, int rtp_m)
37 {
38  RTPMuxContext *rtp_ctx = ctx->priv_data;
39 
40  if (size < 0 ||
41  size > rtp_ctx->max_payload_size - RTP_VC2HQ_PL_HEADER_SIZE - info_hdr_size) {
42  av_log(ctx, AV_LOG_ERROR, "VC-2 data unit too large for RTP payload buffer\n");
43  return AVERROR_INVALIDDATA;
44  }
45 
46  AV_WB16(&rtp_ctx->buf[0], rtp_ctx->seq_high); /* extended sequence number */
47  AV_WB8 (&rtp_ctx->buf[2], i ? (f ? (0x03) : (0x02)) : 0x00); /* flags: interlaced, second field */
48  AV_WB8 (&rtp_ctx->buf[3], parse_code);
49  if (size > 0)
50  memcpy(&rtp_ctx->buf[4 + info_hdr_size], buf, size);
51  ff_rtp_send_data(ctx, rtp_ctx->buf, RTP_VC2HQ_PL_HEADER_SIZE + info_hdr_size + size, rtp_m);
52  return 0;
53 }
54 
55 static int send_picture(AVFormatContext *ctx, const uint8_t *buf, int size, int interlaced)
56 {
57  RTPMuxContext *rtp_ctx = ctx->priv_data;
58  GetBitContext gc;
59  int lvl, second_field;
60  uint32_t pic_nr, wavelet_depth, prefix_bytes, size_scaler;
61  int frag_len;
62  char *info_hdr = &rtp_ctx->buf[4];
63 
64  if (size < DIRAC_PIC_NR_SIZE)
65  return AVERROR(EINVAL);
66 
67  pic_nr = AV_RB32(&buf[0]);
68  buf += DIRAC_PIC_NR_SIZE;
70  second_field = interlaced && (pic_nr & 0x01);
71 
72  init_get_bits8(&gc, buf, FFMIN(size, UINT16_MAX));
73  get_interleaved_ue_golomb(&gc); /* wavelet_idx */
74  wavelet_depth = get_interleaved_ue_golomb(&gc);
75  get_interleaved_ue_golomb(&gc); /* num_x */
76  get_interleaved_ue_golomb(&gc); /* num_y */
77  prefix_bytes = get_interleaved_ue_golomb(&gc);
78  size_scaler = get_interleaved_ue_golomb(&gc);
79  if (prefix_bytes > UINT16_MAX || size_scaler > UINT16_MAX)
80  return AVERROR_INVALIDDATA;
81  /* pass the quantization matrices */
82  if (get_bits1(&gc)) {
84  for(lvl = 0; lvl < wavelet_depth; lvl++)
85  {
86  if (get_bits_left(&gc) < 0)
87  return AVERROR_INVALIDDATA;
91  }
92  }
93 
94  frag_len = (get_bits_count(&gc) + 7) / 8; /* length of transform parameters */
95  if (get_bits_left(&gc) < 0 || frag_len > UINT16_MAX)
96  return AVERROR_INVALIDDATA;
97 
98  AV_WB32(&info_hdr[ 0], pic_nr);
99  AV_WB16(&info_hdr[ 4], prefix_bytes);
100  AV_WB16(&info_hdr[ 6], size_scaler);
101  AV_WB16(&info_hdr[ 8], frag_len);
102  AV_WB16(&info_hdr[10], 0 /* nr. of slices */);
103  if (send_packet(ctx, DIRAC_RTP_PCODE_HQ_PIC_FRAGMENT, 12, buf, frag_len, interlaced, second_field, 0) < 0)
104  return AVERROR_INVALIDDATA;
105  buf += frag_len;
106  size -= frag_len;
107 
108  while (size > 0) {
109  frag_len = FFMIN3(rtp_ctx->max_payload_size - 20 /* pl header */, size, UINT16_MAX);
110  AV_WB16(&info_hdr[ 8], frag_len);
111  AV_WB16(&info_hdr[10], 1 /* nr. of slices */);
112  AV_WB16(&info_hdr[12], 0 /* slice x */);
113  AV_WB16(&info_hdr[14], 0 /* slice y */);
114 
115  size -= frag_len;
116  if (send_packet(ctx, DIRAC_RTP_PCODE_HQ_PIC_FRAGMENT, 16, buf, frag_len, interlaced, second_field, size > 0 ? 0 : 1) < 0)
117  return AVERROR_INVALIDDATA;
118  buf += frag_len;
119  }
120  return 0;
121 }
122 
124 {
125  const uint8_t *end = frame_buf + frame_size;
126  const uint8_t *unit = frame_buf;
127  uint8_t parse_code;
128  uint32_t unit_size;
129 
130  while (end - unit >= DIRAC_DATA_UNIT_HEADER_SIZE) {
131  parse_code = unit[4];
132  unit_size = AV_RB32(&unit[5]);
133  if (!unit_size)
134  unit_size = end - unit;
135 
136  if (unit_size < DIRAC_DATA_UNIT_HEADER_SIZE || unit_size > end - unit)
137  break;
138 
139  switch (parse_code) {
140  /* sequence header */
141  /* end of sequence */
143  case DIRAC_PCODE_END_SEQ:
144  send_packet(ctx, parse_code, 0, unit + DIRAC_DATA_UNIT_HEADER_SIZE, unit_size - DIRAC_DATA_UNIT_HEADER_SIZE, 0, 0, 0);
145  break;
146  /* HQ picture */
149  break;
150  /* parse codes without specification */
151  case DIRAC_PCODE_AUX:
152  case DIRAC_PCODE_PAD:
153  break;
154  default:
155  avpriv_report_missing_feature(ctx, "VC-2 parse code %d", parse_code);
156  break;
157  }
158  unit += unit_size;
159  }
160 }
uint8_t
Main libavformat public API header.
#define AV_RB32
Definition: intreadwrite.h:130
#define f(width, name)
Definition: cbs_vp9.c:255
#define FFMIN(a, b)
Definition: common.h:105
#define FFMIN3(a, b, c)
Definition: common.h:106
Interface to Dirac Decoder/Encoder.
@ DIRAC_PCODE_AUX
Definition: dirac.h:60
@ DIRAC_PCODE_END_SEQ
Definition: dirac.h:59
@ DIRAC_PCODE_SEQ_HEADER
Definition: dirac.h:58
@ DIRAC_PCODE_PAD
Definition: dirac.h:61
@ DIRAC_PCODE_PICTURE_HQ
Definition: dirac.h:65
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
exp golomb vlc stuff
static unsigned get_interleaved_ue_golomb(GetBitContext *gb)
Definition: golomb.h:145
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int i
Definition: input.c:407
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
#define AV_WB8(p, d)
Definition: intreadwrite.h:396
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t interlaced
Definition: mxfenc.c:2208
int frame_size
Definition: mxfenc.c:2206
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
Definition: rtpenc.c:371
#define RTP_VC2HQ_PL_HEADER_SIZE
Definition: rtpenc_vc2hq.c:30
static int send_picture(AVFormatContext *ctx, const uint8_t *buf, int size, int interlaced)
Definition: rtpenc_vc2hq.c:55
#define DIRAC_DATA_UNIT_HEADER_SIZE
Definition: rtpenc_vc2hq.c:32
void ff_rtp_send_vc2hq(AVFormatContext *ctx, const uint8_t *frame_buf, int frame_size, int interlaced)
Definition: rtpenc_vc2hq.c:123
static int send_packet(AVFormatContext *ctx, uint8_t parse_code, int info_hdr_size, const uint8_t *buf, int size, int i, int f, int rtp_m)
Definition: rtpenc_vc2hq.c:36
#define DIRAC_PIC_NR_SIZE
Definition: rtpenc_vc2hq.c:33
#define DIRAC_RTP_PCODE_HQ_PIC_FRAGMENT
Definition: rtpenc_vc2hq.c:34
Format I/O context.
Definition: avformat.h:1232
void * priv_data
Format private data.
Definition: avformat.h:1260
uint16_t seq_high
Definition: rtpenc.h:35
int max_payload_size
Definition: rtpenc.h:39
uint8_t * buf
Definition: rtpenc.h:50
#define av_log(a,...)
AVFormatContext * ctx
Definition: movenc.c:48
int size