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.

406 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. #if LIBAVCODEC_VERSION_MAJOR < 55
  84. enum AVPixelFormat ff_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
  85. {
  86. return avpriv_find_pix_fmt(tags, fourcc);
  87. }
  88. #endif
  89. static av_cold int raw_init_decoder(AVCodecContext *avctx)
  90. {
  91. RawVideoContext *context = avctx->priv_data;
  92. const AVPixFmtDescriptor *desc;
  93. ff_dsputil_init(&context->dsp, avctx);
  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 && (avctx->codec_tag & 0xFFFFFF) != MKTAG('B','I','T', 0))
  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. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  107. if (!desc) {
  108. av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
  109. return AVERROR(EINVAL);
  110. }
  111. if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL)) {
  112. context->palette = av_buffer_alloc(AVPALETTE_SIZE);
  113. if (!context->palette)
  114. return AVERROR(ENOMEM);
  115. if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  116. avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
  117. else
  118. memset(context->palette->data, 0, AVPALETTE_SIZE);
  119. }
  120. if ((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
  121. avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
  122. (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))) {
  123. context->is_2_4_bpp = 1;
  124. context->frame_size = avpicture_get_size(avctx->pix_fmt,
  125. FFALIGN(avctx->width, 16),
  126. avctx->height);
  127. } else {
  128. context->is_lt_16bpp = av_get_bits_per_pixel(desc) == 16 && avctx->bits_per_coded_sample && avctx->bits_per_coded_sample < 16;
  129. context->frame_size = avpicture_get_size(avctx->pix_fmt, avctx->width,
  130. avctx->height);
  131. }
  132. if ((avctx->extradata_size >= 9 &&
  133. !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
  134. avctx->codec_tag == MKTAG('c','y','u','v') ||
  135. avctx->codec_tag == MKTAG(3, 0, 0, 0) ||
  136. avctx->codec_tag == MKTAG('W','R','A','W'))
  137. context->flip = 1;
  138. if (avctx->codec_tag == AV_RL32("yuv2") &&
  139. avctx->pix_fmt == AV_PIX_FMT_YUYV422)
  140. context->is_yuv2 = 1;
  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. /*
  149. * Scale sample to 16-bit resolution
  150. */
  151. #define SCALE16(x, bits) (((x) << (16 - (bits))) | ((x) >> (2 * (bits) - 16)))
  152. /**
  153. * Scale buffer to 16 bits per coded sample resolution
  154. */
  155. #define MKSCALE16(name, r16, w16) \
  156. static void name(AVCodecContext *avctx, uint8_t * dst, const uint8_t *buf, int buf_size, int packed) \
  157. { \
  158. int i; \
  159. if (!packed) { \
  160. for (i = 0; i + 1 < buf_size; i += 2) \
  161. w16(dst + i, SCALE16(r16(buf + i), avctx->bits_per_coded_sample)); \
  162. } else { \
  163. GetBitContext gb; \
  164. init_get_bits(&gb, buf, buf_size * 8); \
  165. for (i = 0; i < avctx->width * avctx->height; i++) { \
  166. int sample = get_bits(&gb, avctx->bits_per_coded_sample); \
  167. w16(dst + i*2, SCALE16(sample, avctx->bits_per_coded_sample)); \
  168. } \
  169. } \
  170. }
  171. MKSCALE16(scale16be, AV_RB16, AV_WB16)
  172. MKSCALE16(scale16le, AV_RL16, AV_WL16)
  173. static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
  174. AVPacket *avpkt)
  175. {
  176. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  177. RawVideoContext *context = avctx->priv_data;
  178. const uint8_t *buf = avpkt->data;
  179. int buf_size = avpkt->size;
  180. int linesize_align = 4;
  181. int res, len;
  182. int need_copy = !avpkt->buf || context->is_2_4_bpp || context->is_yuv2 || context->is_lt_16bpp;
  183. AVFrame *frame = data;
  184. AVPicture *picture = data;
  185. frame->pict_type = AV_PICTURE_TYPE_I;
  186. frame->key_frame = 1;
  187. res = ff_decode_frame_props(avctx, frame);
  188. if (res < 0)
  189. return res;
  190. av_frame_set_pkt_pos (frame, avctx->internal->pkt->pos);
  191. av_frame_set_pkt_duration(frame, avctx->internal->pkt->duration);
  192. if (context->tff >= 0) {
  193. frame->interlaced_frame = 1;
  194. frame->top_field_first = context->tff;
  195. }
  196. if ((res = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  197. return res;
  198. if (need_copy)
  199. frame->buf[0] = av_buffer_alloc(FFMAX(context->frame_size, buf_size));
  200. else
  201. frame->buf[0] = av_buffer_ref(avpkt->buf);
  202. if (!frame->buf[0])
  203. return AVERROR(ENOMEM);
  204. //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
  205. if (context->is_2_4_bpp) {
  206. int i;
  207. uint8_t *dst = frame->buf[0]->data;
  208. buf_size = context->frame_size - AVPALETTE_SIZE;
  209. if (avctx->bits_per_coded_sample == 4) {
  210. for (i = 0; 2 * i + 1 < buf_size && i<avpkt->size; i++) {
  211. dst[2 * i + 0] = buf[i] >> 4;
  212. dst[2 * i + 1] = buf[i] & 15;
  213. }
  214. linesize_align = 8;
  215. } else {
  216. av_assert0(avctx->bits_per_coded_sample == 2);
  217. for (i = 0; 4 * i + 3 < buf_size && i<avpkt->size; i++) {
  218. dst[4 * i + 0] = buf[i] >> 6;
  219. dst[4 * i + 1] = buf[i] >> 4 & 3;
  220. dst[4 * i + 2] = buf[i] >> 2 & 3;
  221. dst[4 * i + 3] = buf[i] & 3;
  222. }
  223. linesize_align = 16;
  224. }
  225. buf = dst;
  226. } else if (context->is_lt_16bpp) {
  227. uint8_t *dst = frame->buf[0]->data;
  228. int packed = (avctx->codec_tag & 0xFFFFFF) == MKTAG('B','I','T', 0);
  229. int swap = avctx->codec_tag >> 24;
  230. if (packed && swap) {
  231. av_fast_padded_malloc(&context->bitstream_buf, &context->bitstream_buf_size, buf_size);
  232. if (!context->bitstream_buf)
  233. return AVERROR(ENOMEM);
  234. if (swap == 16)
  235. context->dsp.bswap16_buf(context->bitstream_buf, (const uint16_t*)buf, buf_size / 2);
  236. else if (swap == 32)
  237. context->dsp.bswap_buf(context->bitstream_buf, (const uint32_t*)buf, buf_size / 4);
  238. else
  239. return AVERROR_INVALIDDATA;
  240. buf = context->bitstream_buf;
  241. }
  242. if (desc->flags & AV_PIX_FMT_FLAG_BE)
  243. scale16be(avctx, dst, buf, buf_size, packed);
  244. else
  245. scale16le(avctx, dst, buf, buf_size, packed);
  246. buf = dst;
  247. } else if (need_copy) {
  248. memcpy(frame->buf[0]->data, buf, buf_size);
  249. buf = frame->buf[0]->data;
  250. }
  251. if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
  252. avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
  253. buf += buf_size - context->frame_size;
  254. len = context->frame_size - (avctx->pix_fmt==AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
  255. if (buf_size < len && (avctx->codec_tag & 0xFFFFFF) != MKTAG('B','I','T', 0)) {
  256. av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected frame_size %d\n", buf_size, len);
  257. av_buffer_unref(&frame->buf[0]);
  258. return AVERROR(EINVAL);
  259. }
  260. if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
  261. avctx->width, avctx->height)) < 0) {
  262. av_buffer_unref(&frame->buf[0]);
  263. return res;
  264. }
  265. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  266. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
  267. NULL);
  268. if (pal) {
  269. av_buffer_unref(&context->palette);
  270. context->palette = av_buffer_alloc(AVPALETTE_SIZE);
  271. if (!context->palette) {
  272. av_buffer_unref(&frame->buf[0]);
  273. return AVERROR(ENOMEM);
  274. }
  275. memcpy(context->palette->data, pal, AVPALETTE_SIZE);
  276. frame->palette_has_changed = 1;
  277. }
  278. }
  279. if ((avctx->pix_fmt==AV_PIX_FMT_BGR24 ||
  280. avctx->pix_fmt==AV_PIX_FMT_GRAY8 ||
  281. avctx->pix_fmt==AV_PIX_FMT_RGB555LE ||
  282. avctx->pix_fmt==AV_PIX_FMT_RGB555BE ||
  283. avctx->pix_fmt==AV_PIX_FMT_RGB565LE ||
  284. avctx->pix_fmt==AV_PIX_FMT_MONOWHITE ||
  285. avctx->pix_fmt==AV_PIX_FMT_PAL8) &&
  286. FFALIGN(frame->linesize[0], linesize_align) * avctx->height <= buf_size)
  287. frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
  288. if (avctx->pix_fmt == AV_PIX_FMT_NV12 && avctx->codec_tag == MKTAG('N', 'V', '1', '2') &&
  289. FFALIGN(frame->linesize[0], linesize_align) * avctx->height +
  290. FFALIGN(frame->linesize[1], linesize_align) * ((avctx->height + 1) / 2) <= buf_size) {
  291. int la0 = FFALIGN(frame->linesize[0], linesize_align);
  292. frame->data[1] += (la0 - frame->linesize[0]) * avctx->height;
  293. frame->linesize[0] = la0;
  294. frame->linesize[1] = FFALIGN(frame->linesize[1], linesize_align);
  295. }
  296. if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
  297. (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)) {
  298. frame->buf[1] = av_buffer_ref(context->palette);
  299. if (!frame->buf[1]) {
  300. av_buffer_unref(&frame->buf[0]);
  301. return AVERROR(ENOMEM);
  302. }
  303. frame->data[1] = frame->buf[1]->data;
  304. }
  305. if (avctx->pix_fmt == AV_PIX_FMT_BGR24 &&
  306. ((frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
  307. frame->linesize[0] = (frame->linesize[0] + 3) & ~3;
  308. if (context->flip)
  309. flip(avctx, picture);
  310. if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2') ||
  311. avctx->codec_tag == MKTAG('Y', 'V', '1', '6') ||
  312. avctx->codec_tag == MKTAG('Y', 'V', '2', '4') ||
  313. avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
  314. FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
  315. if (avctx->codec_tag == AV_RL32("I420") && (avctx->width+1)*(avctx->height+1) * 3/2 == buf_size) {
  316. picture->data[1] = picture->data[1] + (avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height;
  317. picture->data[2] = picture->data[2] + ((avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height)*5/4;
  318. }
  319. if (avctx->codec_tag == AV_RL32("yuv2") &&
  320. avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
  321. int x, y;
  322. uint8_t *line = picture->data[0];
  323. for (y = 0; y < avctx->height; y++) {
  324. for (x = 0; x < avctx->width; x++)
  325. line[2 * x + 1] ^= 0x80;
  326. line += picture->linesize[0];
  327. }
  328. }
  329. if (avctx->field_order > AV_FIELD_PROGRESSIVE) { /* we have interlaced material flagged in container */
  330. frame->interlaced_frame = 1;
  331. if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
  332. frame->top_field_first = 1;
  333. }
  334. *got_frame = 1;
  335. return buf_size;
  336. }
  337. static av_cold int raw_close_decoder(AVCodecContext *avctx)
  338. {
  339. RawVideoContext *context = avctx->priv_data;
  340. av_buffer_unref(&context->palette);
  341. return 0;
  342. }
  343. AVCodec ff_rawvideo_decoder = {
  344. .name = "rawvideo",
  345. .long_name = NULL_IF_CONFIG_SMALL("raw video"),
  346. .type = AVMEDIA_TYPE_VIDEO,
  347. .id = AV_CODEC_ID_RAWVIDEO,
  348. .priv_data_size = sizeof(RawVideoContext),
  349. .init = raw_init_decoder,
  350. .close = raw_close_decoder,
  351. .decode = raw_decode,
  352. .priv_class = &rawdec_class,
  353. };