cpia.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #define FRAME_HEADER_SIZE 64
  27. #define MAGIC_0 0x19 /**< First header byte */
  28. #define MAGIC_1 0x68 /**< Second header byte */
  29. #define SUBSAMPLE_420 0
  30. #define SUBSAMPLE_422 1
  31. #define YUVORDER_YUYV 0
  32. #define YUVORDER_UYVY 1
  33. #define NOT_COMPRESSED 0
  34. #define COMPRESSED 1
  35. #define NO_DECIMATION 0
  36. #define DECIMATION_ENAB 1
  37. #define EOL 0xfd /**< End Of Line marker */
  38. #define EOI 0xff /**< End Of Image marker */
  39. typedef struct {
  40. AVFrame frame;
  41. } CpiaContext;
  42. static int cpia_decode_frame(AVCodecContext* avctx,
  43. void* data, int* data_size, AVPacket* avpkt)
  44. {
  45. CpiaContext* const cpia = avctx->priv_data;
  46. int i,j,ret;
  47. uint8_t* const header = avpkt->data;
  48. uint8_t* src;
  49. int src_size;
  50. uint16_t linelength;
  51. uint8_t skip;
  52. AVFrame* const frame = &cpia->frame;
  53. uint8_t *y, *u, *v, *y_end, *u_end, *v_end;
  54. // Get buffer filled with previous frame
  55. if ((ret = avctx->reget_buffer(avctx, frame)) < 0) {
  56. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed!\n");
  57. return ret;
  58. }
  59. // Check header
  60. if ( avpkt->size < FRAME_HEADER_SIZE
  61. || header[0] != MAGIC_0 || header[1] != MAGIC_1
  62. || (header[17] != SUBSAMPLE_420 && header[17] != SUBSAMPLE_422)
  63. || (header[18] != YUVORDER_YUYV && header[18] != YUVORDER_UYVY)
  64. || (header[28] != NOT_COMPRESSED && header[28] != COMPRESSED)
  65. || (header[29] != NO_DECIMATION && header[29] != DECIMATION_ENAB)
  66. ) {
  67. av_log(avctx, AV_LOG_ERROR, "Invalid header!\n");
  68. return AVERROR_INVALIDDATA;
  69. }
  70. // currently unsupported properties
  71. if (header[17] == SUBSAMPLE_422) {
  72. av_log(avctx, AV_LOG_ERROR, "Unsupported subsample!\n");
  73. return AVERROR_PATCHWELCOME;
  74. }
  75. if (header[18] == YUVORDER_UYVY) {
  76. av_log(avctx, AV_LOG_ERROR, "Unsupported YUV byte order!\n");
  77. return AVERROR_PATCHWELCOME;
  78. }
  79. if (header[29] == DECIMATION_ENAB) {
  80. av_log(avctx, AV_LOG_ERROR, "Decimation unsupported!\n");
  81. return AVERROR_PATCHWELCOME;
  82. }
  83. src = header + FRAME_HEADER_SIZE;
  84. src_size = avpkt->size - FRAME_HEADER_SIZE;
  85. if (header[28] == NOT_COMPRESSED) {
  86. frame->pict_type = AV_PICTURE_TYPE_I;
  87. frame->key_frame = 1;
  88. } else {
  89. frame->pict_type = AV_PICTURE_TYPE_P;
  90. frame->key_frame = 0;
  91. }
  92. for ( i = 0;
  93. i < frame->height;
  94. i++, src += linelength, src_size -= linelength
  95. ) {
  96. // Read line length, two byte little endian
  97. linelength = AV_RL16(src);
  98. src += 2;
  99. if (src_size < linelength) {
  100. frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
  101. av_log(avctx, AV_LOG_WARNING, "Frame ended enexpectedly!\n");
  102. break;
  103. }
  104. if (src[linelength - 1] != EOL) {
  105. frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
  106. av_log(avctx, AV_LOG_WARNING, "Wrong line length %d or line not terminated properly (found 0x%02x)!\n", linelength, src[linelength - 1]);
  107. break;
  108. }
  109. /* Update the data pointers. Y data is on every line.
  110. * U and V data on every second line
  111. */
  112. y = &frame->data[0][i * frame->linesize[0]];
  113. u = &frame->data[1][(i >> 1) * frame->linesize[1]];
  114. v = &frame->data[2][(i >> 1) * frame->linesize[2]];
  115. y_end = y + frame->linesize[0] - 1;
  116. u_end = u + frame->linesize[1] - 1;
  117. v_end = v + frame->linesize[2] - 1;
  118. if ((i & 1) && header[17] == SUBSAMPLE_420) {
  119. /* We are on a odd line and 420 subsample is used.
  120. * On this line only Y values are specified, one per pixel.
  121. */
  122. for (j = 0; j < linelength - 1; j++) {
  123. if (y > y_end) {
  124. frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
  125. av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
  126. break;
  127. }
  128. if ((src[j] & 1) && header[28] == COMPRESSED) {
  129. /* It seems that odd lines are always uncompressed, but
  130. * we do it according to specification anyways.
  131. */
  132. skip = src[j] >> 1;
  133. y += skip;
  134. } else {
  135. *(y++) = src[j];
  136. }
  137. }
  138. } else if (header[17] == SUBSAMPLE_420) {
  139. /* We are on an even line and 420 subsample is used.
  140. * On this line each pair of pixels is described by four bytes.
  141. */
  142. for (j = 0; j < linelength - 4; ) {
  143. if (y + 1 > y_end || u > u_end || v > v_end) {
  144. frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
  145. av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
  146. break;
  147. }
  148. if ((src[j] & 1) && header[28] == COMPRESSED) {
  149. // Skip amount of pixels and move forward one byte
  150. skip = src[j] >> 1;
  151. y += skip;
  152. u += skip >> 1;
  153. v += skip >> 1;
  154. j++;
  155. } else {
  156. // Set image data as specified and move forward 4 bytes
  157. *(y++) = src[j];
  158. *(u++) = src[j+1];
  159. *(y++) = src[j+2];
  160. *(v++) = src[j+3];
  161. j += 4;
  162. }
  163. }
  164. }
  165. }
  166. *data_size = sizeof(AVFrame);
  167. *(AVFrame*) data = *frame;
  168. return avpkt->size;
  169. }
  170. static av_cold int cpia_decode_init(AVCodecContext *avctx)
  171. {
  172. // output pixel format
  173. avctx->pix_fmt = PIX_FMT_YUV420P;
  174. /* The default timebase set by the v4l2 demuxer leads to probing which is buggy.
  175. * Set some reasonable time_base to skip this.
  176. */
  177. if (avctx->time_base.num == 1 && avctx->time_base.den == 1000000) {
  178. avctx->time_base.num = 1;
  179. avctx->time_base.den = 60;
  180. }
  181. return 0;
  182. }
  183. AVCodec ff_cpia_decoder = {
  184. .name = "cpia",
  185. .type = AVMEDIA_TYPE_VIDEO,
  186. .id = AV_CODEC_ID_CPIA,
  187. .priv_data_size = sizeof(CpiaContext),
  188. .init = cpia_decode_init,
  189. .decode = cpia_decode_frame,
  190. .capabilities = CODEC_CAP_DR1,
  191. .long_name = NULL_IF_CONFIG_SMALL("CPiA video format"),
  192. };