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.

324 lines
12KB

  1. /*
  2. * Raw Video Decoder
  3. * Copyright (c) 2001 Fabrice Bellard
  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. * Raw Video Decoder
  24. */
  25. #include "avcodec.h"
  26. #include "raw.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/opt.h"
  32. typedef struct RawVideoContext {
  33. AVClass *av_class;
  34. uint32_t palette[AVPALETTE_COUNT];
  35. unsigned char *buffer; /* block of memory for holding one frame */
  36. int length; /* number of bytes in buffer */
  37. int flip;
  38. AVFrame pic; ///< AVCodecContext.coded_frame
  39. int tff;
  40. } RawVideoContext;
  41. static const AVOption options[]={
  42. {"top", "top field first", offsetof(RawVideoContext, tff), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM},
  43. {NULL}
  44. };
  45. static const AVClass class = {
  46. .class_name = "rawdec",
  47. .option = options,
  48. .version = LIBAVUTIL_VERSION_INT,
  49. };
  50. static const PixelFormatTag pix_fmt_bps_avi[] = {
  51. { AV_PIX_FMT_MONOWHITE, 1 },
  52. { AV_PIX_FMT_PAL8, 2 },
  53. { AV_PIX_FMT_PAL8, 4 },
  54. { AV_PIX_FMT_PAL8, 8 },
  55. { AV_PIX_FMT_RGB444LE, 12 },
  56. { AV_PIX_FMT_RGB555LE, 15 },
  57. { AV_PIX_FMT_RGB555LE, 16 },
  58. { AV_PIX_FMT_BGR24, 24 },
  59. { AV_PIX_FMT_BGRA, 32 },
  60. { AV_PIX_FMT_NONE, 0 },
  61. };
  62. static const PixelFormatTag pix_fmt_bps_mov[] = {
  63. { AV_PIX_FMT_MONOWHITE, 1 },
  64. { AV_PIX_FMT_PAL8, 2 },
  65. { AV_PIX_FMT_PAL8, 4 },
  66. { AV_PIX_FMT_PAL8, 8 },
  67. // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
  68. // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
  69. { AV_PIX_FMT_RGB555BE, 16 },
  70. { AV_PIX_FMT_RGB24, 24 },
  71. { AV_PIX_FMT_ARGB, 32 },
  72. { AV_PIX_FMT_MONOWHITE,33 },
  73. { AV_PIX_FMT_NONE, 0 },
  74. };
  75. enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
  76. unsigned int fourcc)
  77. {
  78. while (tags->pix_fmt >= 0) {
  79. if (tags->fourcc == fourcc)
  80. return tags->pix_fmt;
  81. tags++;
  82. }
  83. return AV_PIX_FMT_NONE;
  84. }
  85. #if LIBAVCODEC_VERSION_MAJOR < 55
  86. enum AVPixelFormat ff_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
  87. {
  88. return avpriv_find_pix_fmt(tags, fourcc);
  89. }
  90. #endif
  91. static av_cold int raw_init_decoder(AVCodecContext *avctx)
  92. {
  93. RawVideoContext *context = avctx->priv_data;
  94. if ( avctx->codec_tag == MKTAG('r','a','w',' ')
  95. || avctx->codec_tag == MKTAG('N','O','1','6'))
  96. avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_mov,
  97. avctx->bits_per_coded_sample);
  98. else if (avctx->codec_tag == MKTAG('W', 'R', 'A', 'W'))
  99. avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_avi,
  100. avctx->bits_per_coded_sample);
  101. else if (avctx->codec_tag)
  102. avctx->pix_fmt = avpriv_find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
  103. else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
  104. avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_avi,
  105. avctx->bits_per_coded_sample);
  106. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  107. av_log(avctx, AV_LOG_ERROR, "Pixel format was not specified and cannot be detected\n");
  108. return AVERROR(EINVAL);
  109. }
  110. avpriv_set_systematic_pal2(context->palette, avctx->pix_fmt);
  111. if ((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
  112. avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
  113. (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))) {
  114. context->length = avpicture_get_size(avctx->pix_fmt,
  115. FFALIGN(avctx->width, 16),
  116. avctx->height);
  117. if (context->length < 0)
  118. return context->length;
  119. context->buffer = av_malloc(context->length);
  120. if (!context->buffer)
  121. return AVERROR(ENOMEM);
  122. } else {
  123. context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  124. if (context->length < 0)
  125. return context->length;
  126. }
  127. context->pic.pict_type = AV_PICTURE_TYPE_I;
  128. context->pic.key_frame = 1;
  129. avctx->coded_frame = &context->pic;
  130. if ((avctx->extradata_size >= 9 &&
  131. !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
  132. avctx->codec_tag == MKTAG('c','y','u','v') ||
  133. avctx->codec_tag == MKTAG(3, 0, 0, 0) ||
  134. avctx->codec_tag == MKTAG('W','R','A','W'))
  135. context->flip = 1;
  136. if (avctx->field_order > AV_FIELD_PROGRESSIVE) { /*we have interlaced material flagged in container */
  137. avctx->coded_frame->interlaced_frame = 1;
  138. if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
  139. avctx->coded_frame->top_field_first = 1;
  140. }
  141. return 0;
  142. }
  143. static void flip(AVCodecContext *avctx, AVPicture *picture)
  144. {
  145. picture->data[0] += picture->linesize[0] * (avctx->height - 1);
  146. picture->linesize[0] *= -1;
  147. }
  148. static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
  149. AVPacket *avpkt)
  150. {
  151. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  152. RawVideoContext *context = avctx->priv_data;
  153. const uint8_t *buf = avpkt->data;
  154. int buf_size = avpkt->size;
  155. int linesize_align = 4;
  156. int res, len;
  157. AVFrame *frame = data;
  158. AVPicture *picture = data;
  159. frame->pict_type = avctx->coded_frame->pict_type;
  160. frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
  161. frame->top_field_first = avctx->coded_frame->top_field_first;
  162. frame->reordered_opaque = avctx->reordered_opaque;
  163. frame->pkt_pts = avctx->pkt->pts;
  164. frame->pkt_pos = avctx->pkt->pos;
  165. frame->pkt_duration = avctx->pkt->duration;
  166. if (context->tff >= 0) {
  167. frame->interlaced_frame = 1;
  168. frame->top_field_first = context->tff;
  169. }
  170. if ((res = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  171. return res;
  172. //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
  173. if (context->buffer) {
  174. int i;
  175. uint8_t *dst = context->buffer;
  176. buf_size = context->length - AVPALETTE_SIZE;
  177. if (avctx->bits_per_coded_sample == 4) {
  178. for (i = 0; 2 * i + 1 < buf_size && i<avpkt->size; i++) {
  179. dst[2 * i + 0] = buf[i] >> 4;
  180. dst[2 * i + 1] = buf[i] & 15;
  181. }
  182. linesize_align = 8;
  183. } else {
  184. av_assert0(avctx->bits_per_coded_sample == 2);
  185. for (i = 0; 4 * i + 3 < buf_size && i<avpkt->size; i++) {
  186. dst[4 * i + 0] = buf[i] >> 6;
  187. dst[4 * i + 1] = buf[i] >> 4 & 3;
  188. dst[4 * i + 2] = buf[i] >> 2 & 3;
  189. dst[4 * i + 3] = buf[i] & 3;
  190. }
  191. linesize_align = 16;
  192. }
  193. buf = dst;
  194. }
  195. if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
  196. avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
  197. buf += buf_size - context->length;
  198. len = context->length - (avctx->pix_fmt==AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
  199. if (buf_size < len) {
  200. av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected length %d\n", buf_size, len);
  201. return AVERROR(EINVAL);
  202. }
  203. if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
  204. avctx->width, avctx->height)) < 0)
  205. return res;
  206. if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->length) ||
  207. (desc->flags & PIX_FMT_PSEUDOPAL)) {
  208. frame->data[1] = (uint8_t*)context->palette;
  209. }
  210. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  211. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
  212. NULL);
  213. if (pal) {
  214. memcpy(frame->data[1], pal, AVPALETTE_SIZE);
  215. frame->palette_has_changed = 1;
  216. }
  217. }
  218. if ((avctx->pix_fmt==AV_PIX_FMT_BGR24 ||
  219. avctx->pix_fmt==AV_PIX_FMT_GRAY8 ||
  220. avctx->pix_fmt==AV_PIX_FMT_RGB555LE ||
  221. avctx->pix_fmt==AV_PIX_FMT_RGB555BE ||
  222. avctx->pix_fmt==AV_PIX_FMT_RGB565LE ||
  223. avctx->pix_fmt==AV_PIX_FMT_MONOWHITE ||
  224. avctx->pix_fmt==AV_PIX_FMT_PAL8) &&
  225. FFALIGN(frame->linesize[0], linesize_align) * avctx->height <= buf_size)
  226. frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
  227. if (avctx->pix_fmt == AV_PIX_FMT_NV12 && avctx->codec_tag == MKTAG('N', 'V', '1', '2') &&
  228. FFALIGN(frame->linesize[0], linesize_align) * avctx->height +
  229. FFALIGN(frame->linesize[1], linesize_align) * ((avctx->height + 1) / 2) <= buf_size) {
  230. int la0 = FFALIGN(frame->linesize[0], linesize_align);
  231. frame->data[1] += (la0 - frame->linesize[0]) * avctx->height;
  232. frame->linesize[0] = la0;
  233. frame->linesize[1] = FFALIGN(frame->linesize[1], linesize_align);
  234. }
  235. if (context->flip)
  236. flip(avctx, picture);
  237. if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2') ||
  238. avctx->codec_tag == MKTAG('Y', 'V', '1', '6') ||
  239. avctx->codec_tag == MKTAG('Y', 'V', '2', '4') ||
  240. avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
  241. FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
  242. if (avctx->codec_tag == AV_RL32("I420") && (avctx->width+1)*(avctx->height+1) * 3/2 == buf_size) {
  243. picture->data[1] = picture->data[1] + (avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height;
  244. picture->data[2] = picture->data[2] + ((avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height)*5/4;
  245. }
  246. if (avctx->codec_tag == AV_RL32("yuv2") &&
  247. avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
  248. int x, y;
  249. uint8_t *line = picture->data[0];
  250. for (y = 0; y < avctx->height; y++) {
  251. for (x = 0; x < avctx->width; x++)
  252. line[2 * x + 1] ^= 0x80;
  253. line += picture->linesize[0];
  254. }
  255. }
  256. if (avctx->codec_tag == AV_RL32("YVYU") &&
  257. avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
  258. int x, y;
  259. uint8_t *line = picture->data[0];
  260. for(y = 0; y < avctx->height; y++) {
  261. for(x = 0; x < avctx->width - 1; x += 2)
  262. FFSWAP(uint8_t, line[2*x + 1], line[2*x + 3]);
  263. line += picture->linesize[0];
  264. }
  265. }
  266. *got_frame = 1;
  267. return buf_size;
  268. }
  269. static av_cold int raw_close_decoder(AVCodecContext *avctx)
  270. {
  271. RawVideoContext *context = avctx->priv_data;
  272. av_freep(&context->buffer);
  273. return 0;
  274. }
  275. AVCodec ff_rawvideo_decoder = {
  276. .name = "rawvideo",
  277. .type = AVMEDIA_TYPE_VIDEO,
  278. .id = AV_CODEC_ID_RAWVIDEO,
  279. .priv_data_size = sizeof(RawVideoContext),
  280. .init = raw_init_decoder,
  281. .close = raw_close_decoder,
  282. .decode = raw_decode,
  283. .long_name = NULL_IF_CONFIG_SMALL("raw video"),
  284. .priv_class = &class,
  285. };