FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
40  s->buf_ptr = s->buf;
41  s->timestamp = s->cur_timestamp;
42 
43  /* convert video pixel dimensions from pixels to blocks */
44  w = FF_CEIL_RSHIFT(s1->streams[0]->codec->width, 3);
45  h = FF_CEIL_RSHIFT(s1->streams[0]->codec->height, 3);
46 
47  /* get the pixel format type or fail */
48  if (s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUVJ422P ||
50  s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV422P)) {
51  type = 0;
52  } else if (s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUVJ420P ||
54  s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV420P)) {
55  type = 1;
56  } else {
57  av_log(s1, AV_LOG_ERROR, "Unsupported pixel format\n");
58  return;
59  }
60 
61  /* preparse the header for getting some infos */
62  for (i = 0; i < size; i++) {
63  if (buf[i] != 0xff)
64  continue;
65 
66  if (buf[i + 1] == DQT) {
67  int tables, j;
68  if (buf[i + 4] & 0xF0)
70  "Only 8-bit precision is supported.\n");
71 
72  /* a quantization table is 64 bytes long */
73  tables = AV_RB16(&buf[i + 2]) / 65;
74  if (i + 5 + tables * 65 > size) {
75  av_log(s1, AV_LOG_ERROR, "Too short JPEG header. Aborted!\n");
76  return;
77  }
78  if (nb_qtables + tables > 4) {
79  av_log(s1, AV_LOG_ERROR, "Invalid number of quantisation tables\n");
80  return;
81  }
82 
83  for (j = 0; j < tables; j++)
84  qtables[nb_qtables + j] = buf + i + 5 + j * 65;
85  nb_qtables += tables;
86  } else if (buf[i + 1] == SOF0) {
87  if (buf[i + 14] != 17 || buf[i + 17] != 17) {
88  av_log(s1, AV_LOG_ERROR,
89  "Only 1x1 chroma blocks are supported. Aborted!\n");
90  return;
91  }
92  } else if (buf[i + 1] == DHT) {
93  if ( AV_RB16(&buf[i + 2]) < 418
94  || i + 420 >= size
95  || buf[i + 4] != 0x00
96  || buf[i + 33] != 0x01
97  || buf[i + 62] != 0x10
98  || buf[i + 241] != 0x11
99  || memcmp(buf + i + 5, avpriv_mjpeg_bits_dc_luminance + 1, 16)
100  || memcmp(buf + i + 21, avpriv_mjpeg_val_dc, 12)
101  || memcmp(buf + i + 34, avpriv_mjpeg_bits_dc_chrominance + 1, 16)
102  || memcmp(buf + i + 50, avpriv_mjpeg_val_dc, 12)
103  || memcmp(buf + i + 63, avpriv_mjpeg_bits_ac_luminance + 1, 16)
104  || memcmp(buf + i + 79, avpriv_mjpeg_val_ac_luminance, 162)
105  || memcmp(buf + i + 242, avpriv_mjpeg_bits_ac_chrominance + 1, 16)
106  || memcmp(buf + i + 258, avpriv_mjpeg_val_ac_chrominance, 162)) {
107  av_log(s1, AV_LOG_ERROR,
108  "RFC 2435 requires standard Huffman tables for jpeg\n");
109  return;
110  }
111  } else if (buf[i + 1] == SOS) {
112  /* SOS is last marker in the header */
113  i += AV_RB16(&buf[i + 2]) + 2;
114  if (i > size) {
115  av_log(s1, AV_LOG_ERROR,
116  "Insufficient data. Aborted!\n");
117  return;
118  }
119  break;
120  }
121  }
122  if (nb_qtables && nb_qtables != 2)
124  "RFC 2435 suggests two quantization tables, %d provided\n",
125  nb_qtables);
126 
127  /* skip JPEG header */
128  buf += i;
129  size -= i;
130 
131  for (i = size - 2; i >= 0; i--) {
132  if (buf[i] == 0xff && buf[i + 1] == EOI) {
133  /* Remove the EOI marker */
134  size = i;
135  break;
136  }
137  }
138 
139  p = s->buf_ptr;
140  while (size > 0) {
141  int hdr_size = 8;
142 
143  if (off == 0 && nb_qtables)
144  hdr_size += 4 + 64 * nb_qtables;
145 
146  /* payload max in one packet */
147  len = FFMIN(size, s->max_payload_size - hdr_size);
148 
149  /* set main header */
150  bytestream_put_byte(&p, 0);
151  bytestream_put_be24(&p, off);
152  bytestream_put_byte(&p, type);
153  bytestream_put_byte(&p, 255);
154  bytestream_put_byte(&p, w);
155  bytestream_put_byte(&p, h);
156 
157  if (off == 0 && nb_qtables) {
158  /* set quantization tables header */
159  bytestream_put_byte(&p, 0);
160  bytestream_put_byte(&p, 0);
161  bytestream_put_be16(&p, 64 * nb_qtables);
162 
163  for (i = 0; i < nb_qtables; i++)
164  bytestream_put_buffer(&p, qtables[i], 64);
165  }
166 
167  /* copy payload data */
168  memcpy(p, buf, len);
169 
170  /* marker bit is last packet in frame */
171  ff_rtp_send_data(s1, s->buf, len + hdr_size, size == len);
172 
173  buf += len;
174  size -= len;
175  off += len;
176  p = s->buf;
177  }
178 }
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Definition: mjpeg.h:71
Definition: mjpeg.h:73
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2237
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
int max_payload_size
Definition: rtpenc.h:38
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
MJPEG encoder and decoder.
Format I/O context.
Definition: avformat.h:1273
Definition: mjpeg.h:72
uint8_t
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
const uint8_t avpriv_mjpeg_bits_dc_luminance[17]
Definition: jpegtables.c:65
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:76
uint8_t * buf
Definition: rtpenc.h:49
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
const uint8_t avpriv_mjpeg_bits_dc_chrominance[17]
Definition: jpegtables.c:70
Definition: mjpeg.h:39
static const uint8_t *const tables[]
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
Definition: mjpeg.h:56
#define FFMIN(a, b)
Definition: common.h:81
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
Definition: rtpenc.c:306
int width
picture width / height.
Definition: avcodec.h:1681
#define FF_CEIL_RSHIFT(a, b)
Definition: common.h:57
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:540
uint32_t cur_timestamp
Definition: rtpenc.h:37
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
const uint8_t avpriv_mjpeg_val_dc[12]
Definition: jpegtables.c:67
#define s1
Definition: regdef.h:38
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
uint8_t * buf_ptr
Definition: rtpenc.h:50
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buf, int size)
Definition: rtpenc_jpeg.c:28
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:368
const uint8_t avpriv_mjpeg_bits_ac_luminance[17]
Definition: jpegtables.c:73
uint32_t timestamp
Definition: rtpenc.h:35
int len
const uint8_t avpriv_mjpeg_val_ac_luminance[]
Definition: jpegtables.c:75
void * priv_data
Format private data.
Definition: avformat.h:1301