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.

505 lines
19KB

  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 || avctx->bits_per_coded_sample == 1 ||
  181. (avctx->bits_per_coded_sample == 0 && (context->is_nut_pal8 || context->is_mono)) ) &&
  182. (context->is_mono || context->is_pal8) &&
  183. (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' ') ||
  184. context->is_nut_mono || context->is_nut_pal8)) {
  185. context->is_1_2_4_8_bpp = 1;
  186. if (context->is_mono) {
  187. int row_bytes = avctx->width / 8 + (avctx->width & 7 ? 1 : 0);
  188. context->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
  189. FFALIGN(row_bytes, 16) * 8,
  190. avctx->height, 1);
  191. } else
  192. context->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
  193. FFALIGN(avctx->width, 16),
  194. avctx->height, 1);
  195. } else {
  196. context->is_lt_16bpp = av_get_bits_per_pixel(desc) == 16 && avctx->bits_per_coded_sample && avctx->bits_per_coded_sample < 16;
  197. context->frame_size = av_image_get_buffer_size(avctx->pix_fmt, avctx->width,
  198. avctx->height, 1);
  199. }
  200. if (context->frame_size < 0)
  201. return context->frame_size;
  202. need_copy = !avpkt->buf || context->is_1_2_4_8_bpp || context->is_yuv2 || context->is_lt_16bpp;
  203. frame->pict_type = AV_PICTURE_TYPE_I;
  204. frame->key_frame = 1;
  205. res = ff_decode_frame_props(avctx, frame);
  206. if (res < 0)
  207. return res;
  208. av_frame_set_pkt_pos (frame, avctx->internal->pkt->pos);
  209. av_frame_set_pkt_duration(frame, avctx->internal->pkt->duration);
  210. if (context->tff >= 0) {
  211. frame->interlaced_frame = 1;
  212. frame->top_field_first = context->tff;
  213. }
  214. if ((res = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  215. return res;
  216. if (need_copy)
  217. frame->buf[0] = av_buffer_alloc(FFMAX(context->frame_size, buf_size));
  218. else
  219. frame->buf[0] = av_buffer_ref(avpkt->buf);
  220. if (!frame->buf[0])
  221. return AVERROR(ENOMEM);
  222. // 1, 2, 4 and 8 bpp in avi/mov, 1 and 8 bpp in nut
  223. if (context->is_1_2_4_8_bpp) {
  224. int i, j, row_pix = 0;
  225. uint8_t *dst = frame->buf[0]->data;
  226. buf_size = context->frame_size - (context->is_pal8 ? AVPALETTE_SIZE : 0);
  227. if (avctx->bits_per_coded_sample == 8 || context->is_nut_pal8 || context->is_mono) {
  228. int pix_per_byte = context->is_mono ? 8 : 1;
  229. for (i = 0, j = 0; j < buf_size && i<avpkt->size; i++, j++) {
  230. dst[j] = buf[i];
  231. row_pix += pix_per_byte;
  232. if (row_pix >= avctx->width) {
  233. i += stride - (i % stride) - 1;
  234. j += 16 - (j % 16) - 1;
  235. row_pix = 0;
  236. }
  237. }
  238. } else if (avctx->bits_per_coded_sample == 4) {
  239. for (i = 0, j = 0; 2 * j + 1 < buf_size && i<avpkt->size; i++, j++) {
  240. dst[2 * j + 0] = buf[i] >> 4;
  241. dst[2 * j + 1] = buf[i] & 15;
  242. row_pix += 2;
  243. if (row_pix >= avctx->width) {
  244. i += stride - (i % stride) - 1;
  245. j += 8 - (j % 8) - 1;
  246. row_pix = 0;
  247. }
  248. }
  249. } else if (avctx->bits_per_coded_sample == 2) {
  250. for (i = 0, j = 0; 4 * j + 3 < buf_size && i<avpkt->size; i++, j++) {
  251. dst[4 * j + 0] = buf[i] >> 6;
  252. dst[4 * j + 1] = buf[i] >> 4 & 3;
  253. dst[4 * j + 2] = buf[i] >> 2 & 3;
  254. dst[4 * j + 3] = buf[i] & 3;
  255. row_pix += 4;
  256. if (row_pix >= avctx->width) {
  257. i += stride - (i % stride) - 1;
  258. j += 4 - (j % 4) - 1;
  259. row_pix = 0;
  260. }
  261. }
  262. } else {
  263. av_assert0(avctx->bits_per_coded_sample == 1);
  264. for (i = 0, j = 0; 8 * j + 7 < buf_size && i<avpkt->size; i++, j++) {
  265. dst[8 * j + 0] = buf[i] >> 7;
  266. dst[8 * j + 1] = buf[i] >> 6 & 1;
  267. dst[8 * j + 2] = buf[i] >> 5 & 1;
  268. dst[8 * j + 3] = buf[i] >> 4 & 1;
  269. dst[8 * j + 4] = buf[i] >> 3 & 1;
  270. dst[8 * j + 5] = buf[i] >> 2 & 1;
  271. dst[8 * j + 6] = buf[i] >> 1 & 1;
  272. dst[8 * j + 7] = buf[i] & 1;
  273. row_pix += 8;
  274. if (row_pix >= avctx->width) {
  275. i += stride - (i % stride) - 1;
  276. j += 2 - (j % 2) - 1;
  277. row_pix = 0;
  278. }
  279. }
  280. }
  281. linesize_align = 16;
  282. buf = dst;
  283. } else if (context->is_lt_16bpp) {
  284. uint8_t *dst = frame->buf[0]->data;
  285. int packed = (avctx->codec_tag & 0xFFFFFF) == MKTAG('B','I','T', 0);
  286. int swap = avctx->codec_tag >> 24;
  287. if (packed && swap) {
  288. av_fast_padded_malloc(&context->bitstream_buf, &context->bitstream_buf_size, buf_size);
  289. if (!context->bitstream_buf)
  290. return AVERROR(ENOMEM);
  291. if (swap == 16)
  292. context->bbdsp.bswap16_buf(context->bitstream_buf, (const uint16_t*)buf, buf_size / 2);
  293. else if (swap == 32)
  294. context->bbdsp.bswap_buf(context->bitstream_buf, (const uint32_t*)buf, buf_size / 4);
  295. else
  296. return AVERROR_INVALIDDATA;
  297. buf = context->bitstream_buf;
  298. }
  299. if (desc->flags & AV_PIX_FMT_FLAG_BE)
  300. scale16be(avctx, dst, buf, buf_size, packed);
  301. else
  302. scale16le(avctx, dst, buf, buf_size, packed);
  303. buf = dst;
  304. } else if (need_copy) {
  305. memcpy(frame->buf[0]->data, buf, buf_size);
  306. buf = frame->buf[0]->data;
  307. }
  308. if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
  309. avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
  310. buf += buf_size - context->frame_size;
  311. len = context->frame_size - (avctx->pix_fmt==AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
  312. if (buf_size < len && ((avctx->codec_tag & 0xFFFFFF) != MKTAG('B','I','T', 0) || !need_copy)) {
  313. av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected frame_size %d\n", buf_size, len);
  314. av_buffer_unref(&frame->buf[0]);
  315. return AVERROR(EINVAL);
  316. }
  317. if ((res = av_image_fill_arrays(frame->data, frame->linesize,
  318. buf, avctx->pix_fmt,
  319. avctx->width, avctx->height, 1)) < 0) {
  320. av_buffer_unref(&frame->buf[0]);
  321. return res;
  322. }
  323. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  324. int pal_size;
  325. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
  326. &pal_size);
  327. int ret;
  328. if (pal_size != AVPALETTE_SIZE) {
  329. av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", pal_size);
  330. pal = NULL;
  331. }
  332. if (!context->palette)
  333. context->palette = av_buffer_alloc(AVPALETTE_SIZE);
  334. if (!context->palette) {
  335. av_buffer_unref(&frame->buf[0]);
  336. return AVERROR(ENOMEM);
  337. }
  338. ret = av_buffer_make_writable(&context->palette);
  339. if (ret < 0) {
  340. av_buffer_unref(&frame->buf[0]);
  341. return ret;
  342. }
  343. if (pal) {
  344. memcpy(context->palette->data, pal, AVPALETTE_SIZE);
  345. frame->palette_has_changed = 1;
  346. } else if (context->is_nut_pal8) {
  347. int vid_size = avctx->width * avctx->height;
  348. int pal_size = avpkt->size - vid_size;
  349. if (avpkt->size > vid_size && pal_size <= AVPALETTE_SIZE) {
  350. pal = avpkt->data + vid_size;
  351. memcpy(context->palette->data, pal, pal_size);
  352. frame->palette_has_changed = 1;
  353. }
  354. }
  355. }
  356. if ((avctx->pix_fmt==AV_PIX_FMT_RGB24 ||
  357. avctx->pix_fmt==AV_PIX_FMT_BGR24 ||
  358. avctx->pix_fmt==AV_PIX_FMT_GRAY8 ||
  359. avctx->pix_fmt==AV_PIX_FMT_RGB555LE ||
  360. avctx->pix_fmt==AV_PIX_FMT_RGB555BE ||
  361. avctx->pix_fmt==AV_PIX_FMT_RGB565LE ||
  362. avctx->pix_fmt==AV_PIX_FMT_MONOWHITE ||
  363. avctx->pix_fmt==AV_PIX_FMT_MONOBLACK ||
  364. avctx->pix_fmt==AV_PIX_FMT_PAL8) &&
  365. FFALIGN(frame->linesize[0], linesize_align) * avctx->height <= buf_size)
  366. frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
  367. if (avctx->pix_fmt == AV_PIX_FMT_NV12 && avctx->codec_tag == MKTAG('N', 'V', '1', '2') &&
  368. FFALIGN(frame->linesize[0], linesize_align) * avctx->height +
  369. FFALIGN(frame->linesize[1], linesize_align) * ((avctx->height + 1) / 2) <= buf_size) {
  370. int la0 = FFALIGN(frame->linesize[0], linesize_align);
  371. frame->data[1] += (la0 - frame->linesize[0]) * avctx->height;
  372. frame->linesize[0] = la0;
  373. frame->linesize[1] = FFALIGN(frame->linesize[1], linesize_align);
  374. }
  375. if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
  376. (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)) {
  377. frame->buf[1] = av_buffer_ref(context->palette);
  378. if (!frame->buf[1]) {
  379. av_buffer_unref(&frame->buf[0]);
  380. return AVERROR(ENOMEM);
  381. }
  382. frame->data[1] = frame->buf[1]->data;
  383. }
  384. if (avctx->pix_fmt == AV_PIX_FMT_BGR24 &&
  385. ((frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
  386. frame->linesize[0] = (frame->linesize[0] + 3) & ~3;
  387. if (context->flip)
  388. flip(avctx, frame);
  389. if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2') ||
  390. avctx->codec_tag == MKTAG('Y', 'V', '1', '6') ||
  391. avctx->codec_tag == MKTAG('Y', 'V', '2', '4') ||
  392. avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
  393. FFSWAP(uint8_t *, frame->data[1], frame->data[2]);
  394. if (avctx->codec_tag == AV_RL32("I420") && (avctx->width+1)*(avctx->height+1) * 3/2 == buf_size) {
  395. frame->data[1] = frame->data[1] + (avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height;
  396. frame->data[2] = frame->data[2] + ((avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height)*5/4;
  397. }
  398. if (avctx->codec_tag == AV_RL32("yuv2") &&
  399. avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
  400. int x, y;
  401. uint8_t *line = frame->data[0];
  402. for (y = 0; y < avctx->height; y++) {
  403. for (x = 0; x < avctx->width; x++)
  404. line[2 * x + 1] ^= 0x80;
  405. line += frame->linesize[0];
  406. }
  407. }
  408. if (avctx->codec_tag == AV_RL32("b64a") &&
  409. avctx->pix_fmt == AV_PIX_FMT_RGBA64BE) {
  410. uint8_t *dst = frame->data[0];
  411. uint64_t v;
  412. int x;
  413. for (x = 0; x >> 3 < avctx->width * avctx->height; x += 8) {
  414. v = AV_RB64(&dst[x]);
  415. AV_WB64(&dst[x], v << 16 | v >> 48);
  416. }
  417. }
  418. if (avctx->field_order > AV_FIELD_PROGRESSIVE) { /* we have interlaced material flagged in container */
  419. frame->interlaced_frame = 1;
  420. if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
  421. frame->top_field_first = 1;
  422. }
  423. *got_frame = 1;
  424. return buf_size;
  425. }
  426. static av_cold int raw_close_decoder(AVCodecContext *avctx)
  427. {
  428. RawVideoContext *context = avctx->priv_data;
  429. av_buffer_unref(&context->palette);
  430. return 0;
  431. }
  432. AVCodec ff_rawvideo_decoder = {
  433. .name = "rawvideo",
  434. .long_name = NULL_IF_CONFIG_SMALL("raw video"),
  435. .type = AVMEDIA_TYPE_VIDEO,
  436. .id = AV_CODEC_ID_RAWVIDEO,
  437. .priv_data_size = sizeof(RawVideoContext),
  438. .init = raw_init_decoder,
  439. .close = raw_close_decoder,
  440. .decode = raw_decode,
  441. .priv_class = &rawdec_class,
  442. .capabilities = AV_CODEC_CAP_PARAM_CHANGE,
  443. };