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.

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