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.

439 lines
17KB

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