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.

424 lines
15KB

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