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.

486 lines
18KB

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