FFmpeg  4.4.9
rtpenc_jpeg.c
Go to the documentation of this file.
1 /*
2  * RTP JPEG-compressed video Packetizer, RFC 2435
3  * Copyright (c) 2012 Samuel Pitoiset
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 "libavcodec/bytestream.h"
23 #include "libavcodec/mjpeg.h"
24 #include "libavcodec/jpegtables.h"
25 #include "libavutil/intreadwrite.h"
26 #include "rtpenc.h"
27 
29 {
30  RTPMuxContext *s = s1->priv_data;
31  const uint8_t *qtables[4] = { NULL };
32  int nb_qtables = 0;
33  uint8_t type;
34  uint8_t w, h;
35  uint8_t *p;
36  int off = 0; /* fragment offset of the current JPEG frame */
37  int len;
38  int i;
39  int default_huffman_tables = 0;
40 
41  s->buf_ptr = s->buf;
42  s->timestamp = s->cur_timestamp;
43 
44  /* convert video pixel dimensions from pixels to blocks */
45  w = AV_CEIL_RSHIFT(s1->streams[0]->codecpar->width, 3);
46  h = AV_CEIL_RSHIFT(s1->streams[0]->codecpar->height, 3);
47 
48  /* get the pixel format type or fail */
49  if (s1->streams[0]->codecpar->format == AV_PIX_FMT_YUVJ422P ||
50  (s1->streams[0]->codecpar->color_range == AVCOL_RANGE_JPEG &&
51  s1->streams[0]->codecpar->format == AV_PIX_FMT_YUV422P)) {
52  type = 0;
53  } else if (s1->streams[0]->codecpar->format == AV_PIX_FMT_YUVJ420P ||
54  (s1->streams[0]->codecpar->color_range == AVCOL_RANGE_JPEG &&
55  s1->streams[0]->codecpar->format == AV_PIX_FMT_YUV420P)) {
56  type = 1;
57  } else {
58  av_log(s1, AV_LOG_ERROR, "Unsupported pixel format\n");
59  return;
60  }
61 
62  /* preparse the header for getting some info */
63  for (i = 0; i < size; i++) {
64  if (buf[i] != 0xff)
65  continue;
66 
67  if (size - i < 5 || (buf[i + 1] == SOF0 && size - i < 18)) {
68  av_log(s1, AV_LOG_ERROR, "Too short JPEG header. Aborted!\n");
69  return;
70  }
71 
72  if (buf[i + 1] == DQT) {
73  int tables, j;
74  if (buf[i + 4] & 0xF0)
76  "Only 8-bit precision is supported.\n");
77 
78  /* a quantization table is 64 bytes long */
79  tables = AV_RB16(&buf[i + 2]) / 65;
80  if (i + 5 + tables * 65 > size) {
81  av_log(s1, AV_LOG_ERROR, "Too short JPEG header. Aborted!\n");
82  return;
83  }
84  if (nb_qtables + tables > 4) {
85  av_log(s1, AV_LOG_ERROR, "Invalid number of quantisation tables\n");
86  return;
87  }
88 
89  for (j = 0; j < tables; j++)
90  qtables[nb_qtables + j] = buf + i + 5 + j * 65;
91  nb_qtables += tables;
92  } else if (buf[i + 1] == SOF0) {
93  if (buf[i + 14] != 17 || buf[i + 17] != 17) {
95  "Only 1x1 chroma blocks are supported. Aborted!\n");
96  return;
97  }
98  } else if (buf[i + 1] == DHT) {
99  int dht_size = AV_RB16(&buf[i + 2]);
100  default_huffman_tables |= 1 << 4;
101  i += 3;
102  dht_size -= 2;
103  if (i + dht_size >= size)
104  continue;
105  while (dht_size > 0)
106  switch (buf[i + 1]) {
107  case 0x00:
108  if ( dht_size >= 29
109  && !memcmp(buf + i + 2, avpriv_mjpeg_bits_dc_luminance + 1, 16)
110  && !memcmp(buf + i + 18, avpriv_mjpeg_val_dc, 12)) {
111  default_huffman_tables |= 1;
112  i += 29;
113  dht_size -= 29;
114  } else {
115  i += dht_size;
116  dht_size = 0;
117  }
118  break;
119  case 0x01:
120  if ( dht_size >= 29
121  && !memcmp(buf + i + 2, avpriv_mjpeg_bits_dc_chrominance + 1, 16)
122  && !memcmp(buf + i + 18, avpriv_mjpeg_val_dc, 12)) {
123  default_huffman_tables |= 1 << 1;
124  i += 29;
125  dht_size -= 29;
126  } else {
127  i += dht_size;
128  dht_size = 0;
129  }
130  break;
131  case 0x10:
132  if ( dht_size >= 179
133  && !memcmp(buf + i + 2, avpriv_mjpeg_bits_ac_luminance + 1, 16)
134  && !memcmp(buf + i + 18, avpriv_mjpeg_val_ac_luminance, 162)) {
135  default_huffman_tables |= 1 << 2;
136  i += 179;
137  dht_size -= 179;
138  } else {
139  i += dht_size;
140  dht_size = 0;
141  }
142  break;
143  case 0x11:
144  if ( dht_size >= 179
145  && !memcmp(buf + i + 2, avpriv_mjpeg_bits_ac_chrominance + 1, 16)
146  && !memcmp(buf + i + 18, avpriv_mjpeg_val_ac_chrominance, 162)) {
147  default_huffman_tables |= 1 << 3;
148  i += 179;
149  dht_size -= 179;
150  } else {
151  i += dht_size;
152  dht_size = 0;
153  }
154  break;
155  default:
156  i += dht_size;
157  dht_size = 0;
158  continue;
159  }
160  } else if (buf[i + 1] == SOS) {
161  /* SOS is last marker in the header */
162  i += AV_RB16(&buf[i + 2]) + 2;
163  if (i > size) {
165  "Insufficient data. Aborted!\n");
166  return;
167  }
168  break;
169  }
170  }
171  if (default_huffman_tables && default_huffman_tables != 31) {
173  "RFC 2435 requires standard Huffman tables for jpeg\n");
174  return;
175  }
176  if (nb_qtables && nb_qtables != 2)
178  "RFC 2435 suggests two quantization tables, %d provided\n",
179  nb_qtables);
180 
181  /* skip JPEG header */
182  buf += i;
183  size -= i;
184 
185  for (i = size - 2; i >= 0; i--) {
186  if (buf[i] == 0xff && buf[i + 1] == EOI) {
187  /* Remove the EOI marker */
188  size = i;
189  break;
190  }
191  }
192 
193  p = s->buf_ptr;
194  while (size > 0) {
195  int hdr_size = 8;
196 
197  if (off == 0 && nb_qtables)
198  hdr_size += 4 + 64 * nb_qtables;
199 
200  if (hdr_size >= s->max_payload_size) {
201  av_log(s1, AV_LOG_ERROR, "RTP max payload size %d too small for the %d byte JPEG header\n",
202  s->max_payload_size, hdr_size);
203  return;
204  }
205 
206  /* payload max in one packet */
207  len = FFMIN(size, s->max_payload_size - hdr_size);
208 
209  /* set main header */
210  bytestream_put_byte(&p, 0);
211  bytestream_put_be24(&p, off);
212  bytestream_put_byte(&p, type);
213  bytestream_put_byte(&p, 255);
214  bytestream_put_byte(&p, w);
215  bytestream_put_byte(&p, h);
216 
217  if (off == 0 && nb_qtables) {
218  /* set quantization tables header */
219  bytestream_put_byte(&p, 0);
220  bytestream_put_byte(&p, 0);
221  bytestream_put_be16(&p, 64 * nb_qtables);
222 
223  for (i = 0; i < nb_qtables; i++)
224  bytestream_put_buffer(&p, qtables[i], 64);
225  }
226 
227  /* copy payload data */
228  memcpy(p, buf, len);
229 
230  /* marker bit is last packet in frame */
231  ff_rtp_send_data(s1, s->buf, len + hdr_size, size == len);
232 
233  buf += len;
234  size -= len;
235  off += len;
236  p = s->buf;
237  }
238 }
uint8_t
#define AV_RB16
Definition: intreadwrite.h:53
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:372
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFMIN(a, b)
Definition: common.h:105
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define NULL
Definition: coverity.c:32
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
cl_device_type type
int i
Definition: input.c:407
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: jpegtables.c:99
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: jpegtables.c:102
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
uint8_t w
Definition: llviddspenc.c:39
MJPEG encoder and decoder.
@ DQT
Definition: mjpeg.h:73
@ DHT
Definition: mjpeg.h:56
@ SOS
Definition: mjpeg.h:72
@ EOI
Definition: mjpeg.h:71
@ SOF0
Definition: mjpeg.h:39
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:586
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
#define s1
Definition: regdef.h:38
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
Definition: rtpenc.c:371
void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buf, int size)
Definition: rtpenc_jpeg.c:28
Format I/O context.
Definition: avformat.h:1232
#define av_log(a,...)
static const uint8_t *const tables[]
int size
int len