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.

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