2 * RTP JPEG-compressed video Packetizer, RFC 2435
3 * Copyright (c) 2012 Samuel Pitoiset
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavcodec/bytestream.h"
23 #include "libavcodec/mjpeg.h"
24 #include "libavutil/intreadwrite.h"
27 void ff_rtp_send_jpeg(AVFormatContext
*s1
, const uint8_t *buf
, int size
)
29 RTPMuxContext
*s
= s1
->priv_data
;
30 const uint8_t *qtables
= NULL
;
35 int off
= 0; /* fragment offset of the current JPEG frame */
40 s
->timestamp
= s
->cur_timestamp
;
42 /* convert video pixel dimensions from pixels to blocks */
43 w
= s1
->streams
[0]->codec
->width
>> 3;
44 h
= s1
->streams
[0]->codec
->height
>> 3;
46 /* get the pixel format type or fail */
47 if (s1
->streams
[0]->codec
->pix_fmt
== AV_PIX_FMT_YUVJ422P
||
48 (s1
->streams
[0]->codec
->color_range
== AVCOL_RANGE_JPEG
&&
49 s1
->streams
[0]->codec
->pix_fmt
== AV_PIX_FMT_YUV422P
)) {
51 } else if (s1
->streams
[0]->codec
->pix_fmt
== AV_PIX_FMT_YUVJ420P
||
52 (s1
->streams
[0]->codec
->color_range
== AVCOL_RANGE_JPEG
&&
53 s1
->streams
[0]->codec
->pix_fmt
== AV_PIX_FMT_YUV420P
)) {
56 av_log(s1
, AV_LOG_ERROR
, "Unsupported pixel format\n");
60 /* preparse the header for getting some infos */
61 for (i
= 0; i
< size
; i
++) {
65 if (buf
[i
+ 1] == DQT
) {
67 av_log(s1
, AV_LOG_WARNING
,
68 "Only 8-bit precision is supported.\n");
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");
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");
84 } else if (buf
[i
+ 1] == SOS
) {
85 /* SOS is last marker in the header */
86 i
+= AV_RB16(&buf
[i
+ 2]) + 2;
91 /* skip JPEG header */
95 for (i
= size
- 2; i
>= 0; i
--) {
96 if (buf
[i
] == 0xff && buf
[i
+ 1] == EOI
) {
97 /* Remove the EOI marker */
107 if (off
== 0 && nb_qtables
)
108 hdr_size
+= 4 + 64 * nb_qtables
;
110 /* payload max in one packet */
111 len
= FFMIN(size
, s
->max_payload_size
- hdr_size
);
113 /* set main header */
114 bytestream_put_byte(&p
, 0);
115 bytestream_put_be24(&p
, off
);
116 bytestream_put_byte(&p
, type
);
117 bytestream_put_byte(&p
, 255);
118 bytestream_put_byte(&p
, w
);
119 bytestream_put_byte(&p
, h
);
121 if (off
== 0 && nb_qtables
) {
122 /* set quantization tables header */
123 bytestream_put_byte(&p
, 0);
124 bytestream_put_byte(&p
, 0);
125 bytestream_put_be16(&p
, 64 * nb_qtables
);
127 for (i
= 0; i
< nb_qtables
; i
++)
128 bytestream_put_buffer(&p
, &qtables
[65 * i
+ 1], 64);
131 /* copy payload data */
134 /* marker bit is last packet in frame */
135 ff_rtp_send_data(s1
, s
->buf
, len
+ hdr_size
, size
== len
);