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.

179 lines
6.1KB

  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. #include "libavcodec/bytestream.h"
  22. #include "libavcodec/mjpeg.h"
  23. #include "libavcodec/jpegtables.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "rtpenc.h"
  26. void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buf, int size)
  27. {
  28. RTPMuxContext *s = s1->priv_data;
  29. const uint8_t *qtables[4] = { NULL };
  30. int nb_qtables = 0;
  31. uint8_t type;
  32. uint8_t w, h;
  33. uint8_t *p;
  34. int off = 0; /* fragment offset of the current JPEG frame */
  35. int len;
  36. int i;
  37. s->buf_ptr = s->buf;
  38. s->timestamp = s->cur_timestamp;
  39. /* convert video pixel dimensions from pixels to blocks */
  40. w = FF_CEIL_RSHIFT(s1->streams[0]->codec->width, 3);
  41. h = FF_CEIL_RSHIFT(s1->streams[0]->codec->height, 3);
  42. /* get the pixel format type or fail */
  43. if (s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUVJ422P ||
  44. (s1->streams[0]->codec->color_range == AVCOL_RANGE_JPEG &&
  45. s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV422P)) {
  46. type = 0;
  47. } else if (s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUVJ420P ||
  48. (s1->streams[0]->codec->color_range == AVCOL_RANGE_JPEG &&
  49. s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV420P)) {
  50. type = 1;
  51. } else {
  52. av_log(s1, AV_LOG_ERROR, "Unsupported pixel format\n");
  53. return;
  54. }
  55. /* preparse the header for getting some infos */
  56. for (i = 0; i < size; i++) {
  57. if (buf[i] != 0xff)
  58. continue;
  59. if (buf[i + 1] == DQT) {
  60. int tables, j;
  61. if (buf[i + 4] & 0xF0)
  62. av_log(s1, AV_LOG_WARNING,
  63. "Only 8-bit precision is supported.\n");
  64. /* a quantization table is 64 bytes long */
  65. tables = AV_RB16(&buf[i + 2]) / 65;
  66. if (i + 5 + tables * 65 > size) {
  67. av_log(s1, AV_LOG_ERROR, "Too short JPEG header. Aborted!\n");
  68. return;
  69. }
  70. if (nb_qtables + tables > 4) {
  71. av_log(s1, AV_LOG_ERROR, "Invalid number of quantisation tables\n");
  72. return;
  73. }
  74. for (j = 0; j < tables; j++)
  75. qtables[nb_qtables + j] = buf + i + 5 + j * 65;
  76. nb_qtables += tables;
  77. } else if (buf[i + 1] == SOF0) {
  78. if (buf[i + 14] != 17 || buf[i + 17] != 17) {
  79. av_log(s1, AV_LOG_ERROR,
  80. "Only 1x1 chroma blocks are supported. Aborted!\n");
  81. return;
  82. }
  83. } else if (buf[i + 1] == DHT) {
  84. if ( AV_RB16(&buf[i + 2]) < 418
  85. || i + 420 >= size
  86. || buf[i + 4] != 0x00
  87. || buf[i + 33] != 0x01
  88. || buf[i + 62] != 0x10
  89. || buf[i + 241] != 0x11
  90. || memcmp(buf + i + 5, avpriv_mjpeg_bits_dc_luminance + 1, 16)
  91. || memcmp(buf + i + 21, avpriv_mjpeg_val_dc, 12)
  92. || memcmp(buf + i + 34, avpriv_mjpeg_bits_dc_chrominance + 1, 16)
  93. || memcmp(buf + i + 50, avpriv_mjpeg_val_dc, 12)
  94. || memcmp(buf + i + 63, avpriv_mjpeg_bits_ac_luminance + 1, 16)
  95. || memcmp(buf + i + 79, avpriv_mjpeg_val_ac_luminance, 162)
  96. || memcmp(buf + i + 242, avpriv_mjpeg_bits_ac_chrominance + 1, 16)
  97. || memcmp(buf + i + 258, avpriv_mjpeg_val_ac_chrominance, 162)) {
  98. av_log(s1, AV_LOG_ERROR,
  99. "RFC 2435 requires standard Huffman tables for jpeg\n");
  100. return;
  101. }
  102. } else if (buf[i + 1] == SOS) {
  103. /* SOS is last marker in the header */
  104. i += AV_RB16(&buf[i + 2]) + 2;
  105. if (i > size) {
  106. av_log(s1, AV_LOG_ERROR,
  107. "Insufficient data. Aborted!\n");
  108. return;
  109. }
  110. break;
  111. }
  112. }
  113. if (nb_qtables && nb_qtables != 2)
  114. av_log(s1, AV_LOG_WARNING,
  115. "RFC 2435 suggests two quantization tables, %d provided\n",
  116. nb_qtables);
  117. /* skip JPEG header */
  118. buf += i;
  119. size -= i;
  120. for (i = size - 2; i >= 0; i--) {
  121. if (buf[i] == 0xff && buf[i + 1] == EOI) {
  122. /* Remove the EOI marker */
  123. size = i;
  124. break;
  125. }
  126. }
  127. p = s->buf_ptr;
  128. while (size > 0) {
  129. int hdr_size = 8;
  130. if (off == 0 && nb_qtables)
  131. hdr_size += 4 + 64 * nb_qtables;
  132. /* payload max in one packet */
  133. len = FFMIN(size, s->max_payload_size - hdr_size);
  134. /* set main header */
  135. bytestream_put_byte(&p, 0);
  136. bytestream_put_be24(&p, off);
  137. bytestream_put_byte(&p, type);
  138. bytestream_put_byte(&p, 255);
  139. bytestream_put_byte(&p, w);
  140. bytestream_put_byte(&p, h);
  141. if (off == 0 && nb_qtables) {
  142. /* set quantization tables header */
  143. bytestream_put_byte(&p, 0);
  144. bytestream_put_byte(&p, 0);
  145. bytestream_put_be16(&p, 64 * nb_qtables);
  146. for (i = 0; i < nb_qtables; i++)
  147. bytestream_put_buffer(&p, qtables[i], 64);
  148. }
  149. /* copy payload data */
  150. memcpy(p, buf, len);
  151. /* marker bit is last packet in frame */
  152. ff_rtp_send_data(s1, s->buf, len + hdr_size, size == len);
  153. buf += len;
  154. size -= len;
  155. off += len;
  156. p = s->buf;
  157. }
  158. }