rtpdec_jpeg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * RTP JPEG-compressed Video Depacketizer, 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. #include "avformat.h"
  22. #include "rtpdec_formats.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavcodec/mjpeg.h"
  25. #include "libavcodec/bytestream.h"
  26. /**
  27. * RTP/JPEG specific private data.
  28. */
  29. struct PayloadContext {
  30. AVIOContext *frame; ///< current frame buffer
  31. uint32_t timestamp; ///< current frame timestamp
  32. int hdr_size; ///< size of the current frame header
  33. uint8_t qtables[128][128];
  34. uint8_t qtables_len[128];
  35. };
  36. static const uint8_t default_quantizers[128] = {
  37. /* luma table */
  38. 16, 11, 12, 14, 12, 10, 16, 14,
  39. 13, 14, 18, 17, 16, 19, 24, 40,
  40. 26, 24, 22, 22, 24, 49, 35, 37,
  41. 29, 40, 58, 51, 61, 60, 57, 51,
  42. 56, 55, 64, 72, 92, 78, 64, 68,
  43. 87, 69, 55, 56, 80, 109, 81, 87,
  44. 95, 98, 103, 104, 103, 62, 77, 113,
  45. 121, 112, 100, 120, 92, 101, 103, 99,
  46. /* chroma table */
  47. 17, 18, 18, 24, 21, 24, 47, 26,
  48. 26, 47, 99, 66, 56, 66, 99, 99,
  49. 99, 99, 99, 99, 99, 99, 99, 99,
  50. 99, 99, 99, 99, 99, 99, 99, 99,
  51. 99, 99, 99, 99, 99, 99, 99, 99,
  52. 99, 99, 99, 99, 99, 99, 99, 99,
  53. 99, 99, 99, 99, 99, 99, 99, 99,
  54. 99, 99, 99, 99, 99, 99, 99, 99
  55. };
  56. static PayloadContext *jpeg_new_context(void)
  57. {
  58. return av_mallocz(sizeof(PayloadContext));
  59. }
  60. static inline void free_frame_if_needed(PayloadContext *jpeg)
  61. {
  62. if (jpeg->frame) {
  63. uint8_t *p;
  64. avio_close_dyn_buf(jpeg->frame, &p);
  65. av_free(p);
  66. jpeg->frame = NULL;
  67. }
  68. }
  69. static void jpeg_free_context(PayloadContext *jpeg)
  70. {
  71. free_frame_if_needed(jpeg);
  72. av_free(jpeg);
  73. }
  74. static int jpeg_create_huffman_table(PutByteContext *p, int table_class,
  75. int table_id, const uint8_t *bits_table,
  76. const uint8_t *value_table)
  77. {
  78. int i, n = 0;
  79. bytestream2_put_byte(p, table_class << 4 | table_id);
  80. for (i = 1; i <= 16; i++) {
  81. n += bits_table[i];
  82. bytestream2_put_byte(p, bits_table[i]);
  83. }
  84. for (i = 0; i < n; i++) {
  85. bytestream2_put_byte(p, value_table[i]);
  86. }
  87. return n + 17;
  88. }
  89. static void jpeg_put_marker(PutByteContext *pbc, int code)
  90. {
  91. bytestream2_put_byte(pbc, 0xff);
  92. bytestream2_put_byte(pbc, code);
  93. }
  94. static int jpeg_create_header(uint8_t *buf, int size, uint32_t type, uint32_t w,
  95. uint32_t h, const uint8_t *qtable, int nb_qtable)
  96. {
  97. PutByteContext pbc;
  98. uint8_t *dht_size_ptr;
  99. int dht_size, i;
  100. bytestream2_init_writer(&pbc, buf, size);
  101. /* Convert from blocks to pixels. */
  102. w <<= 3;
  103. h <<= 3;
  104. /* SOI */
  105. jpeg_put_marker(&pbc, SOI);
  106. /* JFIF header */
  107. jpeg_put_marker(&pbc, APP0);
  108. bytestream2_put_be16(&pbc, 16);
  109. bytestream2_put_buffer(&pbc, "JFIF", 5);
  110. bytestream2_put_be16(&pbc, 0x0201);
  111. bytestream2_put_byte(&pbc, 0);
  112. bytestream2_put_be16(&pbc, 1);
  113. bytestream2_put_be16(&pbc, 1);
  114. bytestream2_put_byte(&pbc, 0);
  115. bytestream2_put_byte(&pbc, 0);
  116. /* DQT */
  117. jpeg_put_marker(&pbc, DQT);
  118. bytestream2_put_be16(&pbc, 2 + nb_qtable * (1 + 64));
  119. for (i = 0; i < nb_qtable; i++) {
  120. bytestream2_put_byte(&pbc, i);
  121. /* Each table is an array of 64 values given in zig-zag
  122. * order, identical to the format used in a JFIF DQT
  123. * marker segment. */
  124. bytestream2_put_buffer(&pbc, qtable + 64 * i, 64);
  125. }
  126. /* DHT */
  127. jpeg_put_marker(&pbc, DHT);
  128. dht_size_ptr = pbc.buffer;
  129. bytestream2_put_be16(&pbc, 0);
  130. dht_size = 2;
  131. dht_size += jpeg_create_huffman_table(&pbc, 0, 0,avpriv_mjpeg_bits_dc_luminance,
  132. avpriv_mjpeg_val_dc);
  133. dht_size += jpeg_create_huffman_table(&pbc, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  134. avpriv_mjpeg_val_dc);
  135. dht_size += jpeg_create_huffman_table(&pbc, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  136. avpriv_mjpeg_val_ac_luminance);
  137. dht_size += jpeg_create_huffman_table(&pbc, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  138. avpriv_mjpeg_val_ac_chrominance);
  139. AV_WB16(dht_size_ptr, dht_size);
  140. /* SOF0 */
  141. jpeg_put_marker(&pbc, SOF0);
  142. bytestream2_put_be16(&pbc, 17); /* size */
  143. bytestream2_put_byte(&pbc, 8); /* bits per component */
  144. bytestream2_put_be16(&pbc, h);
  145. bytestream2_put_be16(&pbc, w);
  146. bytestream2_put_byte(&pbc, 3); /* number of components */
  147. bytestream2_put_byte(&pbc, 1); /* component number */
  148. bytestream2_put_byte(&pbc, (2 << 4) | (type ? 2 : 1)); /* hsample/vsample */
  149. bytestream2_put_byte(&pbc, 0); /* matrix number */
  150. bytestream2_put_byte(&pbc, 2); /* component number */
  151. bytestream2_put_byte(&pbc, 1 << 4 | 1); /* hsample/vsample */
  152. bytestream2_put_byte(&pbc, nb_qtable == 2 ? 1 : 0); /* matrix number */
  153. bytestream2_put_byte(&pbc, 3); /* component number */
  154. bytestream2_put_byte(&pbc, 1 << 4 | 1); /* hsample/vsample */
  155. bytestream2_put_byte(&pbc, nb_qtable == 2 ? 1 : 0); /* matrix number */
  156. /* SOS */
  157. jpeg_put_marker(&pbc, SOS);
  158. bytestream2_put_be16(&pbc, 12);
  159. bytestream2_put_byte(&pbc, 3);
  160. bytestream2_put_byte(&pbc, 1);
  161. bytestream2_put_byte(&pbc, 0);
  162. bytestream2_put_byte(&pbc, 2);
  163. bytestream2_put_byte(&pbc, 17);
  164. bytestream2_put_byte(&pbc, 3);
  165. bytestream2_put_byte(&pbc, 17);
  166. bytestream2_put_byte(&pbc, 0);
  167. bytestream2_put_byte(&pbc, 63);
  168. bytestream2_put_byte(&pbc, 0);
  169. /* Return the length in bytes of the JPEG header. */
  170. return bytestream2_tell_p(&pbc);
  171. }
  172. static void create_default_qtables(uint8_t *qtables, uint8_t q)
  173. {
  174. int factor = q;
  175. int i;
  176. factor = av_clip(q, 1, 99);
  177. if (q < 50)
  178. q = 5000 / factor;
  179. else
  180. q = 200 - factor * 2;
  181. for (i = 0; i < 128; i++) {
  182. int val = (default_quantizers[i] * q + 50) / 100;
  183. /* Limit the quantizers to 1 <= q <= 255. */
  184. val = av_clip(val, 1, 255);
  185. qtables[i] = val;
  186. }
  187. }
  188. static int jpeg_parse_packet(AVFormatContext *ctx, PayloadContext *jpeg,
  189. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  190. const uint8_t *buf, int len, int flags)
  191. {
  192. uint8_t type, q, width, height;
  193. const uint8_t *qtables = NULL;
  194. uint16_t qtable_len;
  195. uint32_t off;
  196. int ret;
  197. if (len < 8) {
  198. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  199. return AVERROR_INVALIDDATA;
  200. }
  201. /* Parse the main JPEG header. */
  202. off = AV_RB24(buf + 1); /* fragment byte offset */
  203. type = AV_RB8(buf + 4); /* id of jpeg decoder params */
  204. q = AV_RB8(buf + 5); /* quantization factor (or table id) */
  205. width = AV_RB8(buf + 6); /* frame width in 8 pixel blocks */
  206. height = AV_RB8(buf + 7); /* frame height in 8 pixel blocks */
  207. buf += 8;
  208. len -= 8;
  209. /* Parse the restart marker header. */
  210. if (type > 63) {
  211. av_log(ctx, AV_LOG_ERROR,
  212. "Unimplemented RTP/JPEG restart marker header.\n");
  213. return AVERROR_PATCHWELCOME;
  214. }
  215. if (type > 1) {
  216. av_log(ctx, AV_LOG_ERROR, "Unimplemented RTP/JPEG type %d\n", type);
  217. return AVERROR_PATCHWELCOME;
  218. }
  219. /* Parse the quantization table header. */
  220. if (off == 0) {
  221. /* Start of JPEG data packet. */
  222. uint8_t new_qtables[128];
  223. uint8_t hdr[1024];
  224. if (q > 127) {
  225. uint8_t precision;
  226. if (len < 4) {
  227. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  228. return AVERROR_INVALIDDATA;
  229. }
  230. /* The first byte is reserved for future use. */
  231. precision = AV_RB8(buf + 1); /* size of coefficients */
  232. qtable_len = AV_RB16(buf + 2); /* length in bytes */
  233. buf += 4;
  234. len -= 4;
  235. if (precision)
  236. av_log(ctx, AV_LOG_WARNING, "Only 8-bit precision is supported.\n");
  237. if (qtable_len > 0) {
  238. if (len < qtable_len) {
  239. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  240. return AVERROR_INVALIDDATA;
  241. }
  242. qtables = buf;
  243. buf += qtable_len;
  244. len -= qtable_len;
  245. if (q < 255) {
  246. if (jpeg->qtables_len[q - 128] &&
  247. (jpeg->qtables_len[q - 128] != qtable_len ||
  248. memcmp(qtables, &jpeg->qtables[q - 128][0], qtable_len))) {
  249. av_log(ctx, AV_LOG_WARNING,
  250. "Quantization tables for q=%d changed\n", q);
  251. } else if (!jpeg->qtables_len[q - 128] && qtable_len <= 128) {
  252. memcpy(&jpeg->qtables[q - 128][0], qtables,
  253. qtable_len);
  254. jpeg->qtables_len[q - 128] = qtable_len;
  255. }
  256. }
  257. } else {
  258. if (q == 255) {
  259. av_log(ctx, AV_LOG_ERROR,
  260. "Invalid RTP/JPEG packet. Quantization tables not found.\n");
  261. return AVERROR_INVALIDDATA;
  262. }
  263. if (!jpeg->qtables_len[q - 128]) {
  264. av_log(ctx, AV_LOG_ERROR,
  265. "No quantization tables known for q=%d yet.\n", q);
  266. return AVERROR_INVALIDDATA;
  267. }
  268. qtables = &jpeg->qtables[q - 128][0];
  269. qtable_len = jpeg->qtables_len[q - 128];
  270. }
  271. } else { /* q <= 127 */
  272. if (q == 0 || q > 99) {
  273. av_log(ctx, AV_LOG_ERROR, "Reserved q value %d\n", q);
  274. return AVERROR_INVALIDDATA;
  275. }
  276. create_default_qtables(new_qtables, q);
  277. qtables = new_qtables;
  278. qtable_len = sizeof(new_qtables);
  279. }
  280. /* Skip the current frame in case of the end packet
  281. * has been lost somewhere. */
  282. free_frame_if_needed(jpeg);
  283. if ((ret = avio_open_dyn_buf(&jpeg->frame)) < 0)
  284. return ret;
  285. jpeg->timestamp = *timestamp;
  286. /* Generate a frame and scan headers that can be prepended to the
  287. * RTP/JPEG data payload to produce a JPEG compressed image in
  288. * interchange format. */
  289. jpeg->hdr_size = jpeg_create_header(hdr, sizeof(hdr), type, width,
  290. height, qtables,
  291. qtable_len / 64);
  292. /* Copy JPEG header to frame buffer. */
  293. avio_write(jpeg->frame, hdr, jpeg->hdr_size);
  294. }
  295. if (!jpeg->frame) {
  296. av_log(ctx, AV_LOG_ERROR,
  297. "Received packet without a start chunk; dropping frame.\n");
  298. return AVERROR(EAGAIN);
  299. }
  300. if (jpeg->timestamp != *timestamp) {
  301. /* Skip the current frame if timestamp is incorrect.
  302. * A start packet has been lost somewhere. */
  303. free_frame_if_needed(jpeg);
  304. av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match.\n");
  305. return AVERROR_INVALIDDATA;
  306. }
  307. if (off != avio_tell(jpeg->frame) - jpeg->hdr_size) {
  308. av_log(ctx, AV_LOG_ERROR,
  309. "Missing packets; dropping frame.\n");
  310. return AVERROR(EAGAIN);
  311. }
  312. /* Copy data to frame buffer. */
  313. avio_write(jpeg->frame, buf, len);
  314. if (flags & RTP_FLAG_MARKER) {
  315. /* End of JPEG data packet. */
  316. uint8_t buf[2] = { 0xff, EOI };
  317. /* Put EOI marker. */
  318. avio_write(jpeg->frame, buf, sizeof(buf));
  319. /* Prepare the JPEG packet. */
  320. av_init_packet(pkt);
  321. pkt->size = avio_close_dyn_buf(jpeg->frame, &pkt->data);
  322. if (pkt->size < 0) {
  323. av_log(ctx, AV_LOG_ERROR,
  324. "Error occured when getting frame buffer.\n");
  325. jpeg->frame = NULL;
  326. return pkt->size;
  327. }
  328. pkt->stream_index = st->index;
  329. pkt->destruct = av_destruct_packet;
  330. /* Re-init the frame buffer. */
  331. jpeg->frame = NULL;
  332. return 0;
  333. }
  334. return AVERROR(EAGAIN);
  335. }
  336. RTPDynamicProtocolHandler ff_jpeg_dynamic_handler = {
  337. .enc_name = "JPEG",
  338. .codec_type = AVMEDIA_TYPE_VIDEO,
  339. .codec_id = AV_CODEC_ID_MJPEG,
  340. .alloc = jpeg_new_context,
  341. .free = jpeg_free_context,
  342. .parse_packet = jpeg_parse_packet,
  343. .static_payload_id = 26,
  344. };