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.

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