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.

362 lines
13KB

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