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.

148 lines
4.6KB

  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 "libavutil/intreadwrite.h"
  24. #include "rtpenc.h"
  25. void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buf, int size)
  26. {
  27. RTPMuxContext *s = s1->priv_data;
  28. const uint8_t *qtables = NULL;
  29. int nb_qtables = 0;
  30. uint8_t type;
  31. uint8_t w, h;
  32. uint8_t *p;
  33. int off = 0; /* fragment offset of the current JPEG frame */
  34. int len;
  35. int i;
  36. s->buf_ptr = s->buf;
  37. s->timestamp = s->cur_timestamp;
  38. /* convert video pixel dimensions from pixels to blocks */
  39. w = FF_CEIL_RSHIFT(s1->streams[0]->codec->width, 3);
  40. h = FF_CEIL_RSHIFT(s1->streams[0]->codec->height, 3);
  41. /* get the pixel format type or fail */
  42. if (s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUVJ422P ||
  43. (s1->streams[0]->codec->color_range == AVCOL_RANGE_JPEG &&
  44. s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV422P)) {
  45. type = 0;
  46. } else if (s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUVJ420P ||
  47. (s1->streams[0]->codec->color_range == AVCOL_RANGE_JPEG &&
  48. s1->streams[0]->codec->pix_fmt == AV_PIX_FMT_YUV420P)) {
  49. type = 1;
  50. } else {
  51. av_log(s1, AV_LOG_ERROR, "Unsupported pixel format\n");
  52. return;
  53. }
  54. /* preparse the header for getting some infos */
  55. for (i = 0; i < size; i++) {
  56. if (buf[i] != 0xff)
  57. continue;
  58. if (buf[i + 1] == DQT) {
  59. if (buf[i + 4])
  60. av_log(s1, AV_LOG_WARNING,
  61. "Only 8-bit precision is supported.\n");
  62. /* a quantization table is 64 bytes long */
  63. nb_qtables = AV_RB16(&buf[i + 2]) / 65;
  64. if (i + 4 + nb_qtables * 65 > size) {
  65. av_log(s1, AV_LOG_ERROR, "Too short JPEG header. Aborted!\n");
  66. return;
  67. }
  68. qtables = &buf[i + 4];
  69. } else if (buf[i + 1] == SOF0) {
  70. if (buf[i + 14] != 17 || buf[i + 17] != 17) {
  71. av_log(s1, AV_LOG_ERROR,
  72. "Only 1x1 chroma blocks are supported. Aborted!\n");
  73. return;
  74. }
  75. } else if (buf[i + 1] == SOS) {
  76. /* SOS is last marker in the header */
  77. i += AV_RB16(&buf[i + 2]) + 2;
  78. if (i > size) {
  79. av_log(s1, AV_LOG_ERROR,
  80. "Insufficient data. Aborted!\n");
  81. return;
  82. }
  83. break;
  84. }
  85. }
  86. /* skip JPEG header */
  87. buf += i;
  88. size -= i;
  89. for (i = size - 2; i >= 0; i--) {
  90. if (buf[i] == 0xff && buf[i + 1] == EOI) {
  91. /* Remove the EOI marker */
  92. size = i;
  93. break;
  94. }
  95. }
  96. p = s->buf_ptr;
  97. while (size > 0) {
  98. int hdr_size = 8;
  99. if (off == 0 && nb_qtables)
  100. hdr_size += 4 + 64 * nb_qtables;
  101. /* payload max in one packet */
  102. len = FFMIN(size, s->max_payload_size - hdr_size);
  103. /* set main header */
  104. bytestream_put_byte(&p, 0);
  105. bytestream_put_be24(&p, off);
  106. bytestream_put_byte(&p, type);
  107. bytestream_put_byte(&p, 255);
  108. bytestream_put_byte(&p, w);
  109. bytestream_put_byte(&p, h);
  110. if (off == 0 && nb_qtables) {
  111. /* set quantization tables header */
  112. bytestream_put_byte(&p, 0);
  113. bytestream_put_byte(&p, 0);
  114. bytestream_put_be16(&p, 64 * nb_qtables);
  115. for (i = 0; i < nb_qtables; i++)
  116. bytestream_put_buffer(&p, &qtables[65 * i + 1], 64);
  117. }
  118. /* copy payload data */
  119. memcpy(p, buf, len);
  120. /* marker bit is last packet in frame */
  121. ff_rtp_send_data(s1, s->buf, len + hdr_size, size == len);
  122. buf += len;
  123. size -= len;
  124. off += len;
  125. p = s->buf;
  126. }
  127. }