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.

332 lines
9.8KB

  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. /**
  26. * RTP/JPEG specific private data.
  27. */
  28. struct PayloadContext {
  29. AVIOContext *frame; ///< current frame buffer
  30. uint32_t timestamp; ///< current frame timestamp
  31. int hdr_size; ///< size of the current frame header
  32. };
  33. static PayloadContext *jpeg_new_context(void)
  34. {
  35. return av_mallocz(sizeof(PayloadContext));
  36. }
  37. static inline void free_frame_if_needed(PayloadContext *jpeg)
  38. {
  39. if (jpeg->frame) {
  40. uint8_t *p;
  41. avio_close_dyn_buf(jpeg->frame, &p);
  42. av_free(p);
  43. jpeg->frame = NULL;
  44. }
  45. }
  46. static void jpeg_free_context(PayloadContext *jpeg)
  47. {
  48. free_frame_if_needed(jpeg);
  49. av_free(jpeg);
  50. }
  51. static void jpeg_create_huffman_table(PutBitContext *p, int table_class,
  52. int table_id, const uint8_t *bits_table,
  53. const uint8_t *value_table)
  54. {
  55. int i, n = 0;
  56. put_bits(p, 8, 0);
  57. put_bits(p, 4, table_class);
  58. put_bits(p, 4, table_id);
  59. for (i = 1; i <= 16; i++) {
  60. n += bits_table[i];
  61. put_bits(p, 8, bits_table[i]);
  62. }
  63. for (i = 0; i < n; i++) {
  64. put_bits(p, 8, value_table[i]);
  65. }
  66. }
  67. static int jpeg_create_header(uint8_t *buf, int size, uint32_t type, uint32_t w,
  68. uint32_t h, const uint8_t *qtable, int nb_qtable)
  69. {
  70. PutBitContext pbc;
  71. init_put_bits(&pbc, buf, size);
  72. /* Convert from blocks to pixels. */
  73. w <<= 3;
  74. h <<= 3;
  75. /* SOI */
  76. put_marker(&pbc, SOI);
  77. /* JFIF header */
  78. put_marker(&pbc, APP0);
  79. put_bits(&pbc, 16, 16);
  80. avpriv_put_string(&pbc, "JFIF", 1);
  81. put_bits(&pbc, 16, 0x0201);
  82. put_bits(&pbc, 8, 0);
  83. put_bits(&pbc, 16, 1);
  84. put_bits(&pbc, 16, 1);
  85. put_bits(&pbc, 8, 0);
  86. put_bits(&pbc, 8, 0);
  87. /* DQT */
  88. put_marker(&pbc, DQT);
  89. if (nb_qtable == 2) {
  90. put_bits(&pbc, 16, 2 + 2 * (1 + 64));
  91. } else {
  92. put_bits(&pbc, 16, 2 + 1 * (1 + 64));
  93. }
  94. put_bits(&pbc, 8, 0);
  95. /* Each table is an array of 64 values given in zig-zag
  96. * order, identical to the format used in a JFIF DQT
  97. * marker segment. */
  98. avpriv_copy_bits(&pbc, qtable, 64 * 8);
  99. if (nb_qtable == 2) {
  100. put_bits(&pbc, 8, 1);
  101. avpriv_copy_bits(&pbc, qtable + 64, 64 * 8);
  102. }
  103. /* DHT */
  104. put_marker(&pbc, DHT);
  105. jpeg_create_huffman_table(&pbc, 0, 0, avpriv_mjpeg_bits_dc_luminance,
  106. avpriv_mjpeg_val_dc);
  107. jpeg_create_huffman_table(&pbc, 0, 1, avpriv_mjpeg_bits_dc_chrominance,
  108. avpriv_mjpeg_val_dc);
  109. jpeg_create_huffman_table(&pbc, 1, 0, avpriv_mjpeg_bits_ac_luminance,
  110. avpriv_mjpeg_val_ac_luminance);
  111. jpeg_create_huffman_table(&pbc, 1, 1, avpriv_mjpeg_bits_ac_chrominance,
  112. avpriv_mjpeg_val_ac_chrominance);
  113. /* SOF0 */
  114. put_marker(&pbc, SOF0);
  115. put_bits(&pbc, 16, 17);
  116. put_bits(&pbc, 8, 8);
  117. put_bits(&pbc, 8, h >> 8);
  118. put_bits(&pbc, 8, h);
  119. put_bits(&pbc, 8, w >> 8);
  120. put_bits(&pbc, 8, w);
  121. put_bits(&pbc, 8, 3);
  122. put_bits(&pbc, 8, 1);
  123. put_bits(&pbc, 8, type ? 34 : 33);
  124. put_bits(&pbc, 8, 0);
  125. put_bits(&pbc, 8, 2);
  126. put_bits(&pbc, 8, 17);
  127. put_bits(&pbc, 8, nb_qtable == 2 ? 1 : 0);
  128. put_bits(&pbc, 8, 3);
  129. put_bits(&pbc, 8, 17);
  130. put_bits(&pbc, 8, nb_qtable == 2 ? 1 : 0);
  131. /* SOS */
  132. put_marker(&pbc, SOS);
  133. put_bits(&pbc, 16, 12);
  134. put_bits(&pbc, 8, 3);
  135. put_bits(&pbc, 8, 1);
  136. put_bits(&pbc, 8, 0);
  137. put_bits(&pbc, 8, 2);
  138. put_bits(&pbc, 8, 17);
  139. put_bits(&pbc, 8, 3);
  140. put_bits(&pbc, 8, 17);
  141. put_bits(&pbc, 8, 0);
  142. put_bits(&pbc, 8, 63);
  143. put_bits(&pbc, 8, 0);
  144. /* Fill the buffer. */
  145. flush_put_bits(&pbc);
  146. /* Return the length in bytes of the JPEG header. */
  147. return put_bits_count(&pbc) / 8;
  148. }
  149. static int jpeg_parse_packet(AVFormatContext *ctx, PayloadContext *jpeg,
  150. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  151. const uint8_t *buf, int len, int flags)
  152. {
  153. uint8_t type, q, width, height;
  154. const uint8_t *qtables = NULL;
  155. uint16_t qtable_len;
  156. uint32_t off;
  157. int ret;
  158. if (len < 8) {
  159. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  160. return AVERROR_INVALIDDATA;
  161. }
  162. /* Parse the main JPEG header. */
  163. off = AV_RB24(buf + 1); /* fragment byte offset */
  164. type = AV_RB8(buf + 4); /* id of jpeg decoder params */
  165. q = AV_RB8(buf + 5); /* quantization factor (or table id) */
  166. width = AV_RB8(buf + 6); /* frame width in 8 pixel blocks */
  167. height = AV_RB8(buf + 7); /* frame height in 8 pixel blocks */
  168. buf += 8;
  169. len -= 8;
  170. /* Parse the restart marker header. */
  171. if (type > 63) {
  172. av_log(ctx, AV_LOG_ERROR,
  173. "Unimplemented RTP/JPEG restart marker header.\n");
  174. return AVERROR_PATCHWELCOME;
  175. }
  176. /* Parse the quantization table header. */
  177. if (q > 127 && off == 0) {
  178. uint8_t precision;
  179. if (len < 4) {
  180. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  181. return AVERROR_INVALIDDATA;
  182. }
  183. /* The first byte is reserved for future use. */
  184. precision = AV_RB8(buf + 1); /* size of coefficients */
  185. qtable_len = AV_RB16(buf + 2); /* length in bytes */
  186. buf += 4;
  187. len -= 4;
  188. if (precision)
  189. av_log(ctx, AV_LOG_WARNING, "Only 8-bit precision is supported.\n");
  190. if (q == 255 && qtable_len == 0) {
  191. av_log(ctx, AV_LOG_ERROR,
  192. "Invalid RTP/JPEG packet. Quantization tables not found.\n");
  193. return AVERROR_INVALIDDATA;
  194. }
  195. if (qtable_len > 0) {
  196. if (len < qtable_len) {
  197. av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
  198. return AVERROR_INVALIDDATA;
  199. }
  200. qtables = buf;
  201. buf += qtable_len;
  202. len -= qtable_len;
  203. }
  204. }
  205. if (off == 0) {
  206. /* Start of JPEG data packet. */
  207. uint8_t hdr[1024];
  208. /* Skip the current frame in case of the end packet
  209. * has been lost somewhere. */
  210. free_frame_if_needed(jpeg);
  211. if ((ret = avio_open_dyn_buf(&jpeg->frame)) < 0)
  212. return ret;
  213. jpeg->timestamp = *timestamp;
  214. if (!qtables) {
  215. av_log(ctx, AV_LOG_ERROR,
  216. "Unimplemented default quantization tables.\n");
  217. return AVERROR_PATCHWELCOME;
  218. }
  219. /* Generate a frame and scan headers that can be prepended to the
  220. * RTP/JPEG data payload to produce a JPEG compressed image in
  221. * interchange format. */
  222. jpeg->hdr_size = jpeg_create_header(hdr, sizeof(hdr), type, width,
  223. height, qtables,
  224. qtable_len > 64 ? 2 : 1);
  225. /* Copy JPEG header to frame buffer. */
  226. avio_write(jpeg->frame, hdr, jpeg->hdr_size);
  227. }
  228. if (!jpeg->frame) {
  229. av_log(ctx, AV_LOG_ERROR,
  230. "Received packet without a start chunk; dropping frame.\n");
  231. return AVERROR(EAGAIN);
  232. }
  233. if (jpeg->timestamp != *timestamp) {
  234. /* Skip the current frame if timestamp is incorrect.
  235. * A start packet has been lost somewhere. */
  236. free_frame_if_needed(jpeg);
  237. av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match.\n");
  238. return AVERROR_INVALIDDATA;
  239. }
  240. if (off != avio_tell(jpeg->frame) - jpeg->hdr_size) {
  241. av_log(ctx, AV_LOG_ERROR,
  242. "Missing packets; dropping frame.\n");
  243. return AVERROR(EAGAIN);
  244. }
  245. /* Copy data to frame buffer. */
  246. avio_write(jpeg->frame, buf, len);
  247. if (flags & RTP_FLAG_MARKER) {
  248. /* End of JPEG data packet. */
  249. PutBitContext pbc;
  250. uint8_t buf[2];
  251. /* Put EOI marker. */
  252. init_put_bits(&pbc, buf, sizeof(buf));
  253. put_marker(&pbc, EOI);
  254. flush_put_bits(&pbc);
  255. avio_write(jpeg->frame, buf, sizeof(buf));
  256. /* Prepare the JPEG packet. */
  257. av_init_packet(pkt);
  258. pkt->size = avio_close_dyn_buf(jpeg->frame, &pkt->data);
  259. if (pkt->size < 0) {
  260. av_log(ctx, AV_LOG_ERROR,
  261. "Error occured when getting frame buffer.\n");
  262. jpeg->frame = NULL;
  263. return pkt->size;
  264. }
  265. pkt->stream_index = st->index;
  266. pkt->destruct = av_destruct_packet;
  267. /* Re-init the frame buffer. */
  268. jpeg->frame = NULL;
  269. return 0;
  270. }
  271. return AVERROR(EAGAIN);
  272. }
  273. RTPDynamicProtocolHandler ff_jpeg_dynamic_handler = {
  274. .enc_name = "JPEG",
  275. .codec_type = AVMEDIA_TYPE_VIDEO,
  276. .codec_id = AV_CODEC_ID_MJPEG,
  277. .alloc = jpeg_new_context,
  278. .free = jpeg_free_context,
  279. .parse_packet = jpeg_parse_packet,
  280. .static_payload_id = 26,
  281. };