media100.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Media 100 decoder
  3. * Copyright (c) 2022 Paul B Mahol
  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. /**
  22. * @file
  23. * Media 100 decoder.
  24. */
  25. #include <inttypes.h>
  26. #include "libavutil/intreadwrite.h"
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "codec_internal.h"
  30. typedef struct Media100Context {
  31. AVCodecContext *avctx; // wrapper context for mjpegb
  32. AVPacket *pkt;
  33. } Media100Context;
  34. static av_cold int media100_decode_init(AVCodecContext *avctx)
  35. {
  36. Media100Context *ctx = avctx->priv_data;
  37. const AVCodec *codec;
  38. int ret;
  39. codec = avcodec_find_decoder(AV_CODEC_ID_MJPEGB);
  40. if (!codec)
  41. return AVERROR_BUG;
  42. ctx->avctx = avcodec_alloc_context3(codec);
  43. if (!ctx->avctx)
  44. return AVERROR(ENOMEM);
  45. ctx->avctx->thread_count = 1;
  46. ctx->avctx->flags = avctx->flags;
  47. ctx->avctx->flags2 = avctx->flags2;
  48. ctx->avctx->width = ctx->avctx->coded_width = avctx->width;
  49. ctx->avctx->height = ctx->avctx->coded_height = avctx->height;
  50. ret = avcodec_open2(ctx->avctx, codec, NULL);
  51. if (ret < 0)
  52. return ret;
  53. ctx->pkt = av_packet_alloc();
  54. if (!ctx->pkt)
  55. return AVERROR(ENOMEM);
  56. return 0;
  57. }
  58. static int media100_decode_frame(AVCodecContext *avctx,
  59. AVFrame *frame, int *got_frame,
  60. AVPacket *avpkt)
  61. {
  62. Media100Context *ctx = avctx->priv_data;
  63. unsigned second_field_offset = 0;
  64. unsigned next_field = 0;
  65. unsigned dht_offset[2];
  66. unsigned dqt_offset[2];
  67. unsigned sod_offset[2];
  68. unsigned sof_offset[2];
  69. unsigned sos_offset[2];
  70. unsigned field = 0;
  71. GetByteContext gb;
  72. PutByteContext pb;
  73. AVPacket *pkt;
  74. int ret;
  75. if (avpkt->size + 1024 > ctx->pkt->size) {
  76. ret = av_grow_packet(ctx->pkt, avpkt->size + 1024 - ctx->pkt->size);
  77. if (ret < 0)
  78. return ret;
  79. }
  80. ret = av_packet_make_writable(ctx->pkt);
  81. if (ret < 0)
  82. return ret;
  83. bytestream2_init(&gb, avpkt->data, avpkt->size);
  84. bytestream2_init_writer(&pb, ctx->pkt->data, ctx->pkt->size);
  85. second_field:
  86. bytestream2_put_be32(&pb, 0);
  87. bytestream2_put_be32(&pb, AV_RB32("mjpg"));
  88. bytestream2_put_be32(&pb, 0);
  89. bytestream2_put_be32(&pb, 0);
  90. for (int i = 0; i < 6; i++)
  91. bytestream2_put_be32(&pb, 0);
  92. sof_offset[field] = bytestream2_tell_p(&pb);
  93. bytestream2_put_be16(&pb, 17);
  94. bytestream2_put_byte(&pb, 8);
  95. bytestream2_put_be16(&pb, avctx->height / 2);
  96. bytestream2_put_be16(&pb, avctx->width);
  97. bytestream2_put_byte(&pb, 3);
  98. bytestream2_put_byte(&pb, 1);
  99. bytestream2_put_byte(&pb, 0x21);
  100. bytestream2_put_byte(&pb, 0);
  101. bytestream2_put_byte(&pb, 2);
  102. bytestream2_put_byte(&pb, 0x11);
  103. bytestream2_put_byte(&pb, 1);
  104. bytestream2_put_byte(&pb, 3);
  105. bytestream2_put_byte(&pb, 0x11);
  106. bytestream2_put_byte(&pb, 1);
  107. sos_offset[field] = bytestream2_tell_p(&pb);
  108. bytestream2_put_be16(&pb, 12);
  109. bytestream2_put_byte(&pb, 3);
  110. bytestream2_put_byte(&pb, 1);
  111. bytestream2_put_byte(&pb, 0);
  112. bytestream2_put_byte(&pb, 2);
  113. bytestream2_put_byte(&pb, 0x11);
  114. bytestream2_put_byte(&pb, 3);
  115. bytestream2_put_byte(&pb, 0x11);
  116. bytestream2_put_byte(&pb, 0);
  117. bytestream2_put_byte(&pb, 0);
  118. bytestream2_put_byte(&pb, 0);
  119. dqt_offset[field] = bytestream2_tell_p(&pb);
  120. bytestream2_put_be16(&pb, 132);
  121. bytestream2_put_byte(&pb, 0);
  122. bytestream2_skip(&gb, 4);
  123. for (int i = 0; i < 64; i++)
  124. bytestream2_put_byte(&pb, bytestream2_get_be32(&gb));
  125. bytestream2_put_byte(&pb, 1);
  126. for (int i = 0; i < 64; i++)
  127. bytestream2_put_byte(&pb, bytestream2_get_be32(&gb));
  128. dht_offset[field] = 0;
  129. sod_offset[field] = bytestream2_tell_p(&pb);
  130. for (int i = bytestream2_tell(&gb) + 8; next_field == 0 && i < avpkt->size - 4; i++) {
  131. if (AV_RB32(avpkt->data + i) == 0x00000001) {
  132. next_field = i;
  133. break;
  134. }
  135. }
  136. bytestream2_skip(&gb, 8);
  137. bytestream2_copy_buffer(&pb, &gb, next_field - bytestream2_tell(&gb));
  138. bytestream2_put_be64(&pb, 0);
  139. if (field == 0) {
  140. field = 1;
  141. second_field_offset = bytestream2_tell_p(&pb);
  142. next_field = avpkt->size;
  143. goto second_field;
  144. }
  145. pkt = ctx->pkt;
  146. AV_WB32(pkt->data + 8, second_field_offset);
  147. AV_WB32(pkt->data + 12, second_field_offset);
  148. AV_WB32(pkt->data + 16, second_field_offset);
  149. AV_WB32(pkt->data + 20, dqt_offset[0]);
  150. AV_WB32(pkt->data + 24, dht_offset[0]);
  151. AV_WB32(pkt->data + 28, sof_offset[0]);
  152. AV_WB32(pkt->data + 32, sos_offset[0]);
  153. AV_WB32(pkt->data + 36, sod_offset[0]);
  154. AV_WB32(pkt->data + second_field_offset + 8, bytestream2_tell_p(&pb) - second_field_offset);
  155. AV_WB32(pkt->data + second_field_offset + 12, bytestream2_tell_p(&pb) - second_field_offset);
  156. AV_WB32(pkt->data + second_field_offset + 16, 0);
  157. AV_WB32(pkt->data + second_field_offset + 20, dqt_offset[1] - second_field_offset);
  158. AV_WB32(pkt->data + second_field_offset + 24, dht_offset[1]);
  159. AV_WB32(pkt->data + second_field_offset + 28, sof_offset[1] - second_field_offset);
  160. AV_WB32(pkt->data + second_field_offset + 32, sos_offset[1] - second_field_offset);
  161. AV_WB32(pkt->data + second_field_offset + 36, sod_offset[1] - second_field_offset);
  162. pkt->size = bytestream2_tell_p(&pb);
  163. ret = avcodec_send_packet(ctx->avctx, pkt);
  164. if (ret < 0) {
  165. av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
  166. return ret;
  167. }
  168. ret = avcodec_receive_frame(ctx->avctx, frame);
  169. if (ret < 0)
  170. return ret;
  171. *got_frame = 1;
  172. return avpkt->size;
  173. }
  174. static av_cold int media100_decode_end(AVCodecContext *avctx)
  175. {
  176. Media100Context *ctx = avctx->priv_data;
  177. avcodec_free_context(&ctx->avctx);
  178. av_packet_free(&ctx->pkt);
  179. return 0;
  180. }
  181. const FFCodec ff_media100_decoder = {
  182. .p.name = "media100",
  183. CODEC_LONG_NAME("Media 100"),
  184. .p.type = AVMEDIA_TYPE_VIDEO,
  185. .p.id = AV_CODEC_ID_MEDIA100,
  186. .priv_data_size = sizeof(Media100Context),
  187. .init = media100_decode_init,
  188. .close = media100_decode_end,
  189. FF_CODEC_DECODE_CB(media100_decode_frame),
  190. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  191. };