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.

404 lines
14KB

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