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.

117 lines
4.1KB

  1. /*
  2. * Copyright (c) 2016 Michael Niedermayer
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/intreadwrite.h"
  21. #include "avcodec.h"
  22. #include "internal.h"
  23. static av_cold int m101_decode_init(AVCodecContext *avctx)
  24. {
  25. if (avctx->extradata_size < 6*4) {
  26. avpriv_request_sample(avctx, "Missing or too small extradata (size %d)", avctx->extradata_size);
  27. return AVERROR_INVALIDDATA;
  28. }
  29. if (avctx->extradata[2*4] == 10)
  30. avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
  31. else if (avctx->extradata[2*4] == 8) {
  32. avctx->pix_fmt = AV_PIX_FMT_YUYV422;
  33. } else {
  34. avpriv_request_sample(avctx, "BPS %d", avctx->extradata[2*4]);
  35. return AVERROR_INVALIDDATA;
  36. }
  37. return 0;
  38. }
  39. static int m101_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  40. AVPacket *avpkt)
  41. {
  42. const uint8_t *buf = avpkt->data;
  43. int stride, ret;
  44. int x, y;
  45. int min_stride = 2 * avctx->width;
  46. int bits = avctx->extradata[2*4];
  47. AVFrame *frame = data;
  48. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  49. return ret;
  50. frame->pict_type = AV_PICTURE_TYPE_I;
  51. frame->key_frame = 1;
  52. stride = AV_RL32(avctx->extradata + 5*4);
  53. if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10)
  54. min_stride = (avctx->width + 15) / 16 * 40;
  55. if (stride < min_stride || avpkt->size < stride * (uint64_t)avctx->height) {
  56. av_log(avctx, AV_LOG_ERROR, "stride (%d) is invalid for packet sized %d\n",
  57. stride, avpkt->size);
  58. return AVERROR_INVALIDDATA;
  59. }
  60. frame->interlaced_frame = ((avctx->extradata[3*4] & 3) != 3);
  61. if (frame->interlaced_frame)
  62. frame->top_field_first = avctx->extradata[3*4] & 1;
  63. for (y = 0; y < avctx->height; y++) {
  64. int src_y = y;
  65. if (frame->interlaced_frame)
  66. src_y = ((y&1)^frame->top_field_first) ? y/2 : (y/2 + avctx->height/2);
  67. if (bits == 8) {
  68. uint8_t *line = frame->data[0] + y*frame->linesize[0];
  69. memcpy(line, buf + src_y*stride, 2*avctx->width);
  70. } else {
  71. int block;
  72. uint16_t *luma = (uint16_t*)&frame->data[0][y*frame->linesize[0]];
  73. uint16_t *cb = (uint16_t*)&frame->data[1][y*frame->linesize[1]];
  74. uint16_t *cr = (uint16_t*)&frame->data[2][y*frame->linesize[2]];
  75. for (block = 0; 16*block < avctx->width; block ++) {
  76. const uint8_t *buf_src = buf + src_y*stride + 40*block;
  77. for (x = 0; x < 16 && x + 16*block < avctx->width; x++) {
  78. int xd = x + 16*block;
  79. if (x&1) {
  80. luma [xd] = (4*buf_src[2*x + 0]) + ((buf_src[32 + (x>>1)]>>4)&3);
  81. } else {
  82. luma [xd] = (4*buf_src[2*x + 0]) + (buf_src[32 + (x>>1)] &3);
  83. cb[xd>>1] = (4*buf_src[2*x + 1]) + ((buf_src[32 + (x>>1)]>>2)&3);
  84. cr[xd>>1] = (4*buf_src[2*x + 3]) + (buf_src[32 + (x>>1)]>>6);
  85. }
  86. }
  87. }
  88. }
  89. }
  90. *got_frame = 1;
  91. return avpkt->size;
  92. }
  93. AVCodec ff_m101_decoder = {
  94. .name = "m101",
  95. .long_name = NULL_IF_CONFIG_SMALL("Matrox Uncompressed SD"),
  96. .type = AVMEDIA_TYPE_VIDEO,
  97. .id = AV_CODEC_ID_M101,
  98. .init = m101_decode_init,
  99. .decode = m101_decode_frame,
  100. .capabilities = AV_CODEC_CAP_DR1,
  101. };