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.

234 lines
7.7KB

  1. /*
  2. * CPiA video decoder.
  3. * Copyright (c) 2010 Hans de Goede <hdegoede@redhat.com>
  4. *
  5. * This decoder is based on the LGPL code available at
  6. * https://v4l4j.googlecode.com/svn/v4l4j/trunk/libvideo/libv4lconvert/cpia1.c
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "avcodec.h"
  25. #include "get_bits.h"
  26. #include "internal.h"
  27. #define FRAME_HEADER_SIZE 64
  28. #define MAGIC_0 0x19 /**< First header byte */
  29. #define MAGIC_1 0x68 /**< Second header byte */
  30. #define SUBSAMPLE_420 0
  31. #define SUBSAMPLE_422 1
  32. #define YUVORDER_YUYV 0
  33. #define YUVORDER_UYVY 1
  34. #define NOT_COMPRESSED 0
  35. #define COMPRESSED 1
  36. #define NO_DECIMATION 0
  37. #define DECIMATION_ENAB 1
  38. #define EOL 0xfd /**< End Of Line marker */
  39. #define EOI 0xff /**< End Of Image marker */
  40. typedef struct {
  41. AVFrame *frame;
  42. } CpiaContext;
  43. static int cpia_decode_frame(AVCodecContext *avctx,
  44. void *data, int *got_frame, AVPacket* avpkt)
  45. {
  46. CpiaContext* const cpia = avctx->priv_data;
  47. int i,j,ret;
  48. uint8_t* const header = avpkt->data;
  49. uint8_t* src;
  50. int src_size;
  51. uint16_t linelength;
  52. uint8_t skip;
  53. AVFrame *frame = cpia->frame;
  54. uint8_t *y, *u, *v, *y_end, *u_end, *v_end;
  55. // Check header
  56. if ( avpkt->size < FRAME_HEADER_SIZE
  57. || header[0] != MAGIC_0 || header[1] != MAGIC_1
  58. || (header[17] != SUBSAMPLE_420 && header[17] != SUBSAMPLE_422)
  59. || (header[18] != YUVORDER_YUYV && header[18] != YUVORDER_UYVY)
  60. || (header[28] != NOT_COMPRESSED && header[28] != COMPRESSED)
  61. || (header[29] != NO_DECIMATION && header[29] != DECIMATION_ENAB)
  62. ) {
  63. av_log(avctx, AV_LOG_ERROR, "Invalid header!\n");
  64. return AVERROR_INVALIDDATA;
  65. }
  66. // currently unsupported properties
  67. if (header[17] == SUBSAMPLE_422) {
  68. avpriv_report_missing_feature(avctx, "4:2:2 subsampling");
  69. return AVERROR_PATCHWELCOME;
  70. }
  71. if (header[18] == YUVORDER_UYVY) {
  72. avpriv_report_missing_feature(avctx, "YUV byte order UYVY");
  73. return AVERROR_PATCHWELCOME;
  74. }
  75. if (header[29] == DECIMATION_ENAB) {
  76. avpriv_report_missing_feature(avctx, "Decimation");
  77. return AVERROR_PATCHWELCOME;
  78. }
  79. src = header + FRAME_HEADER_SIZE;
  80. src_size = avpkt->size - FRAME_HEADER_SIZE;
  81. if (header[28] == NOT_COMPRESSED) {
  82. frame->pict_type = AV_PICTURE_TYPE_I;
  83. frame->key_frame = 1;
  84. } else {
  85. frame->pict_type = AV_PICTURE_TYPE_P;
  86. frame->key_frame = 0;
  87. }
  88. // Get buffer filled with previous frame
  89. if ((ret = ff_reget_buffer(avctx, frame)) < 0)
  90. return ret;
  91. for ( i = 0;
  92. i < frame->height;
  93. i++, src += linelength, src_size -= linelength
  94. ) {
  95. // Read line length, two byte little endian
  96. linelength = AV_RL16(src);
  97. src += 2;
  98. if (src_size < linelength) {
  99. av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
  100. av_log(avctx, AV_LOG_WARNING, "Frame ended unexpectedly!\n");
  101. break;
  102. }
  103. if (src[linelength - 1] != EOL) {
  104. av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
  105. av_log(avctx, AV_LOG_WARNING, "Wrong line length %d or line not terminated properly (found 0x%02x)!\n", linelength, src[linelength - 1]);
  106. break;
  107. }
  108. /* Update the data pointers. Y data is on every line.
  109. * U and V data on every second line
  110. */
  111. y = &frame->data[0][i * frame->linesize[0]];
  112. u = &frame->data[1][(i >> 1) * frame->linesize[1]];
  113. v = &frame->data[2][(i >> 1) * frame->linesize[2]];
  114. y_end = y + frame->linesize[0] - 1;
  115. u_end = u + frame->linesize[1] - 1;
  116. v_end = v + frame->linesize[2] - 1;
  117. if ((i & 1) && header[17] == SUBSAMPLE_420) {
  118. /* We are on a odd line and 420 subsample is used.
  119. * On this line only Y values are specified, one per pixel.
  120. */
  121. for (j = 0; j < linelength - 1; j++) {
  122. if (y > y_end) {
  123. av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
  124. av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
  125. break;
  126. }
  127. if ((src[j] & 1) && header[28] == COMPRESSED) {
  128. /* It seems that odd lines are always uncompressed, but
  129. * we do it according to specification anyways.
  130. */
  131. skip = src[j] >> 1;
  132. y += skip;
  133. } else {
  134. *(y++) = src[j];
  135. }
  136. }
  137. } else if (header[17] == SUBSAMPLE_420) {
  138. /* We are on an even line and 420 subsample is used.
  139. * On this line each pair of pixels is described by four bytes.
  140. */
  141. for (j = 0; j < linelength - 4; ) {
  142. if (y + 1 > y_end || u > u_end || v > v_end) {
  143. av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
  144. av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
  145. break;
  146. }
  147. if ((src[j] & 1) && header[28] == COMPRESSED) {
  148. // Skip amount of pixels and move forward one byte
  149. skip = src[j] >> 1;
  150. y += skip;
  151. u += skip >> 1;
  152. v += skip >> 1;
  153. j++;
  154. } else {
  155. // Set image data as specified and move forward 4 bytes
  156. *(y++) = src[j];
  157. *(u++) = src[j+1];
  158. *(y++) = src[j+2];
  159. *(v++) = src[j+3];
  160. j += 4;
  161. }
  162. }
  163. }
  164. }
  165. *got_frame = 1;
  166. if ((ret = av_frame_ref(data, cpia->frame)) < 0)
  167. return ret;
  168. return avpkt->size;
  169. }
  170. static av_cold int cpia_decode_init(AVCodecContext *avctx)
  171. {
  172. CpiaContext *s = avctx->priv_data;
  173. // output pixel format
  174. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  175. /* The default timebase set by the v4l2 demuxer leads to probing which is buggy.
  176. * Set some reasonable time_base to skip this.
  177. */
  178. if (avctx->time_base.num == 1 && avctx->time_base.den == 1000000) {
  179. avctx->time_base.num = 1;
  180. avctx->time_base.den = 60;
  181. }
  182. s->frame = av_frame_alloc();
  183. if (!s->frame)
  184. return AVERROR(ENOMEM);
  185. return 0;
  186. }
  187. static av_cold int cpia_decode_end(AVCodecContext *avctx)
  188. {
  189. CpiaContext *s = avctx->priv_data;
  190. av_frame_free(&s->frame);
  191. return 0;
  192. }
  193. AVCodec ff_cpia_decoder = {
  194. .name = "cpia",
  195. .long_name = NULL_IF_CONFIG_SMALL("CPiA video format"),
  196. .type = AVMEDIA_TYPE_VIDEO,
  197. .id = AV_CODEC_ID_CPIA,
  198. .priv_data_size = sizeof(CpiaContext),
  199. .init = cpia_decode_init,
  200. .close = cpia_decode_end,
  201. .decode = cpia_decode_frame,
  202. .capabilities = CODEC_CAP_DR1,
  203. };