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 "libavutil/intreadwrite.h"
25 #include "rtpenc.h"
26 
28 {
29  RTPMuxContext *s = s1->priv_data;
30  const uint8_t *qtables = NULL;
31  int nb_qtables = 0;
32  uint8_t type;
33  uint8_t w, h;
34  uint8_t *p;
35  int off = 0; /* fragment offset of the current JPEG frame */
36  int len;
37  int i;
38 
39  s->buf_ptr = s->buf;
40  s->timestamp = s->cur_timestamp;
41 
42  /* convert video pixel dimensions from pixels to blocks */
43  w = FF_CEIL_RSHIFT(s1->streams[0]->codec->width, 3);
44  h = FF_CEIL_RSHIFT(s1->streams[0]->codec->height, 3);
45 
46  /* get the pixel format type or fail */
47  if (s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUVJ422P ||
49  s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV422P)) {
50  type = 0;
51  } else if (s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUVJ420P ||
53  s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV420P)) {
54  type = 1;
55  } else {
56  av_log(s1, AV_LOG_ERROR, "Unsupported pixel format\n");
57  return;
58  }
59 
60  /* preparse the header for getting some infos */
61  for (i = 0; i < size; i++) {
62  if (buf[i] != 0xff)
63  continue;
64 
65  if (buf[i + 1] == DQT) {
66  if (buf[i + 4])
68  "Only 8-bit precision is supported.\n");
69 
70  /* a quantization table is 64 bytes long */
71  nb_qtables = AV_RB16(&buf[i + 2]) / 65;
72  if (i + 4 + nb_qtables * 65 > size) {
73  av_log(s1, AV_LOG_ERROR, "Too short JPEG header. Aborted!\n");
74  return;
75  }
76 
77  qtables = &buf[i + 4];
78  } else if (buf[i + 1] == SOF0) {
79  if (buf[i + 14] != 17 || buf[i + 17] != 17) {
80  av_log(s1, AV_LOG_ERROR,
81  "Only 1x1 chroma blocks are supported. Aborted!\n");
82  return;
83  }
84  } else if (buf[i + 1] == SOS) {
85  /* SOS is last marker in the header */
86  i += AV_RB16(&buf[i + 2]) + 2;
87  if (i > size) {
88  av_log(s1, AV_LOG_ERROR,
89  "Insufficient data. Aborted!\n");
90  return;
91  }
92  break;
93  }
94  }
95 
96  /* skip JPEG header */
97  buf += i;
98  size -= i;
99 
100  for (i = size - 2; i >= 0; i--) {
101  if (buf[i] == 0xff && buf[i + 1] == EOI) {
102  /* Remove the EOI marker */
103  size = i;
104  break;
105  }
106  }
107 
108  p = s->buf_ptr;
109  while (size > 0) {
110  int hdr_size = 8;
111 
112  if (off == 0 && nb_qtables)
113  hdr_size += 4 + 64 * nb_qtables;
114 
115  /* payload max in one packet */
116  len = FFMIN(size, s->max_payload_size - hdr_size);
117 
118  /* set main header */
119  bytestream_put_byte(&p, 0);
120  bytestream_put_be24(&p, off);
121  bytestream_put_byte(&p, type);
122  bytestream_put_byte(&p, 255);
123  bytestream_put_byte(&p, w);
124  bytestream_put_byte(&p, h);
125 
126  if (off == 0 && nb_qtables) {
127  /* set quantization tables header */
128  bytestream_put_byte(&p, 0);
129  bytestream_put_byte(&p, 0);
130  bytestream_put_be16(&p, 64 * nb_qtables);
131 
132  for (i = 0; i < nb_qtables; i++)
133  bytestream_put_buffer(&p, &qtables[65 * i + 1], 64);
134  }
135 
136  /* copy payload data */
137  memcpy(p, buf, len);
138 
139  /* marker bit is last packet in frame */
140  ff_rtp_send_data(s1, s->buf, len + hdr_size, size == len);
141 
142  buf += len;
143  size -= len;
144  off += len;
145  p = s->buf;
146  }
147 }
#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:1960
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
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:85
MJPEG encoder and decoder.
Format I/O context.
Definition: avformat.h:1272
Definition: mjpeg.h:72
uint8_t
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1340
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
Definition: mjpeg.h:39
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
#define FFMIN(a, b)
Definition: common.h:66
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:1414
#define FF_CEIL_RSHIFT(a, b)
Definition: common.h:57
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:523
uint32_t cur_timestamp
Definition: rtpenc.h:37
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
#define s1
Definition: regdef.h:38
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:27
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:366
uint32_t timestamp
Definition: rtpenc.h:35
int len
void * priv_data
Format private data.
Definition: avformat.h:1300