FFmpeg  4.3
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 (buf[i + 1] == DQT) {
68  int tables, j;
69  if (buf[i + 4] & 0xF0)
71  "Only 8-bit precision is supported.\n");
72 
73  /* a quantization table is 64 bytes long */
74  tables = AV_RB16(&buf[i + 2]) / 65;
75  if (i + 5 + tables * 65 > size) {
76  av_log(s1, AV_LOG_ERROR, "Too short JPEG header. Aborted!\n");
77  return;
78  }
79  if (nb_qtables + tables > 4) {
80  av_log(s1, AV_LOG_ERROR, "Invalid number of quantisation tables\n");
81  return;
82  }
83 
84  for (j = 0; j < tables; j++)
85  qtables[nb_qtables + j] = buf + i + 5 + j * 65;
86  nb_qtables += tables;
87  } else if (buf[i + 1] == SOF0) {
88  if (buf[i + 14] != 17 || buf[i + 17] != 17) {
90  "Only 1x1 chroma blocks are supported. Aborted!\n");
91  return;
92  }
93  } else if (buf[i + 1] == DHT) {
94  int dht_size = AV_RB16(&buf[i + 2]);
95  default_huffman_tables |= 1 << 4;
96  i += 3;
97  dht_size -= 2;
98  if (i + dht_size >= size)
99  continue;
100  while (dht_size > 0)
101  switch (buf[i + 1]) {
102  case 0x00:
103  if ( dht_size >= 29
104  && !memcmp(buf + i + 2, avpriv_mjpeg_bits_dc_luminance + 1, 16)
105  && !memcmp(buf + i + 18, avpriv_mjpeg_val_dc, 12)) {
106  default_huffman_tables |= 1;
107  i += 29;
108  dht_size -= 29;
109  } else {
110  i += dht_size;
111  dht_size = 0;
112  }
113  break;
114  case 0x01:
115  if ( dht_size >= 29
116  && !memcmp(buf + i + 2, avpriv_mjpeg_bits_dc_chrominance + 1, 16)
117  && !memcmp(buf + i + 18, avpriv_mjpeg_val_dc, 12)) {
118  default_huffman_tables |= 1 << 1;
119  i += 29;
120  dht_size -= 29;
121  } else {
122  i += dht_size;
123  dht_size = 0;
124  }
125  break;
126  case 0x10:
127  if ( dht_size >= 179
128  && !memcmp(buf + i + 2, avpriv_mjpeg_bits_ac_luminance + 1, 16)
129  && !memcmp(buf + i + 18, avpriv_mjpeg_val_ac_luminance, 162)) {
130  default_huffman_tables |= 1 << 2;
131  i += 179;
132  dht_size -= 179;
133  } else {
134  i += dht_size;
135  dht_size = 0;
136  }
137  break;
138  case 0x11:
139  if ( dht_size >= 179
140  && !memcmp(buf + i + 2, avpriv_mjpeg_bits_ac_chrominance + 1, 16)
141  && !memcmp(buf + i + 18, avpriv_mjpeg_val_ac_chrominance, 162)) {
142  default_huffman_tables |= 1 << 3;
143  i += 179;
144  dht_size -= 179;
145  } else {
146  i += dht_size;
147  dht_size = 0;
148  }
149  break;
150  default:
151  i += dht_size;
152  dht_size = 0;
153  continue;
154  }
155  } else if (buf[i + 1] == SOS) {
156  /* SOS is last marker in the header */
157  i += AV_RB16(&buf[i + 2]) + 2;
158  if (i > size) {
160  "Insufficient data. Aborted!\n");
161  return;
162  }
163  break;
164  }
165  }
166  if (default_huffman_tables && default_huffman_tables != 31) {
168  "RFC 2435 requires standard Huffman tables for jpeg\n");
169  return;
170  }
171  if (nb_qtables && nb_qtables != 2)
173  "RFC 2435 suggests two quantization tables, %d provided\n",
174  nb_qtables);
175 
176  /* skip JPEG header */
177  buf += i;
178  size -= i;
179 
180  for (i = size - 2; i >= 0; i--) {
181  if (buf[i] == 0xff && buf[i + 1] == EOI) {
182  /* Remove the EOI marker */
183  size = i;
184  break;
185  }
186  }
187 
188  p = s->buf_ptr;
189  while (size > 0) {
190  int hdr_size = 8;
191 
192  if (off == 0 && nb_qtables)
193  hdr_size += 4 + 64 * nb_qtables;
194 
195  /* payload max in one packet */
196  len = FFMIN(size, s->max_payload_size - hdr_size);
197 
198  /* set main header */
199  bytestream_put_byte(&p, 0);
200  bytestream_put_be24(&p, off);
201  bytestream_put_byte(&p, type);
202  bytestream_put_byte(&p, 255);
203  bytestream_put_byte(&p, w);
204  bytestream_put_byte(&p, h);
205 
206  if (off == 0 && nb_qtables) {
207  /* set quantization tables header */
208  bytestream_put_byte(&p, 0);
209  bytestream_put_byte(&p, 0);
210  bytestream_put_be16(&p, 64 * nb_qtables);
211 
212  for (i = 0; i < nb_qtables; i++)
213  bytestream_put_buffer(&p, qtables[i], 64);
214  }
215 
216  /* copy payload data */
217  memcpy(p, buf, len);
218 
219  /* marker bit is last packet in frame */
220  ff_rtp_send_data(s1, s->buf, len + hdr_size, size == len);
221 
222  buf += len;
223  size -= len;
224  off += len;
225  p = s->buf;
226  }
227 }
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
jpegtables.h
mjpeg.h
SOS
@ SOS
Definition: mjpeg.h:72
SOF0
@ SOF0
Definition: mjpeg.h:39
avpriv_mjpeg_bits_ac_luminance
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
avpriv_mjpeg_val_ac_luminance
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:535
AV_RB16
#define AV_RB16
Definition: intreadwrite.h:53
ff_rtp_send_data
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
Definition: rtpenc.c:332
avpriv_mjpeg_bits_dc_luminance
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
tables
Writing a table generator This documentation is preliminary Parts of the API are not good and should be changed Basic concepts A table generator consists of two *_tablegen c and *_tablegen h The h file will provide the variable declarations and initialization code for the tables
Definition: tablegen.txt:10
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
avpriv_mjpeg_bits_dc_chrominance
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AV_PIX_FMT_YUVJ422P
@ 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
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
avpriv_mjpeg_val_dc
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
s1
#define s1
Definition: regdef.h:38
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
AVFormatContext
Format I/O context.
Definition: avformat.h:1335
NULL
#define NULL
Definition: coverity.c:32
ff_rtp_send_jpeg
void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buf, int size)
Definition: rtpenc_jpeg.c:28
AV_PIX_FMT_YUVJ420P
@ 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
RTPMuxContext
Definition: rtpenc.h:27
avpriv_mjpeg_val_ac_chrominance
const uint8_t avpriv_mjpeg_val_ac_chrominance[]
Definition: jpegtables.c:102
size
int size
Definition: twinvq_data.h:11134
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
DQT
@ DQT
Definition: mjpeg.h:73
bytestream_put_buffer
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
EOI
@ EOI
Definition: mjpeg.h:71
uint8_t
uint8_t
Definition: audio_convert.c:194
len
int len
Definition: vorbis_enc_data.h:452
rtpenc.h
DHT
@ DHT
Definition: mjpeg.h:56
w
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new please make sure they are as small as space on each network bandwidth and so on benefit from smaller test cases Also keep in mind older checkouts use existing sample that means in practice generally do not remove or overwrite files as it likely would break older checkouts or releases Also all needed samples for a commit should be ideally before the push If you need an account for frequently uploading samples or you wish to help others by doing that send a mail to ffmpeg devel rsync vauL Duo ug o o w
Definition: fate.txt:150
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
h
h
Definition: vp9dsp_template.c:2038
avpriv_mjpeg_bits_ac_chrominance
const uint8_t avpriv_mjpeg_bits_ac_chrominance[17]
Definition: jpegtables.c:99