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.

236 lines
7.8KB

  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. av_log(avctx, AV_LOG_ERROR, "Unsupported subsample!\n");
  69. return AVERROR_PATCHWELCOME;
  70. }
  71. if (header[18] == YUVORDER_UYVY) {
  72. av_log(avctx, AV_LOG_ERROR, "Unsupported YUV byte order!\n");
  73. return AVERROR_PATCHWELCOME;
  74. }
  75. if (header[29] == DECIMATION_ENAB) {
  76. av_log(avctx, AV_LOG_ERROR, "Decimation unsupported!\n");
  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. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed!\n");
  91. return ret;
  92. }
  93. for ( i = 0;
  94. i < frame->height;
  95. i++, src += linelength, src_size -= linelength
  96. ) {
  97. // Read line length, two byte little endian
  98. linelength = AV_RL16(src);
  99. src += 2;
  100. if (src_size < linelength) {
  101. av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
  102. av_log(avctx, AV_LOG_WARNING, "Frame ended enexpectedly!\n");
  103. break;
  104. }
  105. if (src[linelength - 1] != EOL) {
  106. av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
  107. av_log(avctx, AV_LOG_WARNING, "Wrong line length %d or line not terminated properly (found 0x%02x)!\n", linelength, src[linelength - 1]);
  108. break;
  109. }
  110. /* Update the data pointers. Y data is on every line.
  111. * U and V data on every second line
  112. */
  113. y = &frame->data[0][i * frame->linesize[0]];
  114. u = &frame->data[1][(i >> 1) * frame->linesize[1]];
  115. v = &frame->data[2][(i >> 1) * frame->linesize[2]];
  116. y_end = y + frame->linesize[0] - 1;
  117. u_end = u + frame->linesize[1] - 1;
  118. v_end = v + frame->linesize[2] - 1;
  119. if ((i & 1) && header[17] == SUBSAMPLE_420) {
  120. /* We are on a odd line and 420 subsample is used.
  121. * On this line only Y values are specified, one per pixel.
  122. */
  123. for (j = 0; j < linelength - 1; j++) {
  124. if (y > y_end) {
  125. av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
  126. av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
  127. break;
  128. }
  129. if ((src[j] & 1) && header[28] == COMPRESSED) {
  130. /* It seems that odd lines are always uncompressed, but
  131. * we do it according to specification anyways.
  132. */
  133. skip = src[j] >> 1;
  134. y += skip;
  135. } else {
  136. *(y++) = src[j];
  137. }
  138. }
  139. } else if (header[17] == SUBSAMPLE_420) {
  140. /* We are on an even line and 420 subsample is used.
  141. * On this line each pair of pixels is described by four bytes.
  142. */
  143. for (j = 0; j < linelength - 4; ) {
  144. if (y + 1 > y_end || u > u_end || v > v_end) {
  145. av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
  146. av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
  147. break;
  148. }
  149. if ((src[j] & 1) && header[28] == COMPRESSED) {
  150. // Skip amount of pixels and move forward one byte
  151. skip = src[j] >> 1;
  152. y += skip;
  153. u += skip >> 1;
  154. v += skip >> 1;
  155. j++;
  156. } else {
  157. // Set image data as specified and move forward 4 bytes
  158. *(y++) = src[j];
  159. *(u++) = src[j+1];
  160. *(y++) = src[j+2];
  161. *(v++) = src[j+3];
  162. j += 4;
  163. }
  164. }
  165. }
  166. }
  167. *got_frame = 1;
  168. if ((ret = av_frame_ref(data, cpia->frame)) < 0)
  169. return ret;
  170. return avpkt->size;
  171. }
  172. static av_cold int cpia_decode_init(AVCodecContext *avctx)
  173. {
  174. CpiaContext *s = avctx->priv_data;
  175. // output pixel format
  176. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  177. /* The default timebase set by the v4l2 demuxer leads to probing which is buggy.
  178. * Set some reasonable time_base to skip this.
  179. */
  180. if (avctx->time_base.num == 1 && avctx->time_base.den == 1000000) {
  181. avctx->time_base.num = 1;
  182. avctx->time_base.den = 60;
  183. }
  184. s->frame = av_frame_alloc();
  185. if (!s->frame)
  186. return AVERROR(ENOMEM);
  187. return 0;
  188. }
  189. static av_cold int cpia_decode_end(AVCodecContext *avctx)
  190. {
  191. CpiaContext *s = avctx->priv_data;
  192. av_frame_free(&s->frame);
  193. return 0;
  194. }
  195. AVCodec ff_cpia_decoder = {
  196. .name = "cpia",
  197. .type = AVMEDIA_TYPE_VIDEO,
  198. .id = AV_CODEC_ID_CPIA,
  199. .priv_data_size = sizeof(CpiaContext),
  200. .init = cpia_decode_init,
  201. .close = cpia_decode_end,
  202. .decode = cpia_decode_frame,
  203. .capabilities = CODEC_CAP_DR1,
  204. .long_name = NULL_IF_CONFIG_SMALL("CPiA video format"),
  205. };