You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
13KB

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