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.

410 lines
13KB

  1. /*
  2. * YUV4MPEG format
  3. * Copyright (c) 2001, 2002, 2003 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. #include "avformat.h"
  22. #include "internal.h"
  23. #define Y4M_MAGIC "YUV4MPEG2"
  24. #define Y4M_FRAME_MAGIC "FRAME"
  25. #define Y4M_LINE_MAX 256
  26. struct frame_attributes {
  27. int interlaced_frame;
  28. int top_field_first;
  29. };
  30. #if CONFIG_YUV4MPEGPIPE_MUXER
  31. static int yuv4_generate_header(AVFormatContext *s, char* buf)
  32. {
  33. AVStream *st;
  34. int width, height;
  35. int raten, rated, aspectn, aspectd, n;
  36. char inter;
  37. const char *colorspace = "";
  38. st = s->streams[0];
  39. width = st->codec->width;
  40. height = st->codec->height;
  41. av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
  42. aspectn = st->sample_aspect_ratio.num;
  43. aspectd = st->sample_aspect_ratio.den;
  44. if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
  45. inter = 'p'; /* progressive is the default */
  46. if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
  47. inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
  48. }
  49. switch(st->codec->pix_fmt) {
  50. case PIX_FMT_GRAY8:
  51. colorspace = " Cmono";
  52. break;
  53. case PIX_FMT_YUV411P:
  54. colorspace = " C411 XYSCSS=411";
  55. break;
  56. case PIX_FMT_YUV420P:
  57. colorspace = (st->codec->chroma_sample_location == AVCHROMA_LOC_TOPLEFT)?" C420paldv XYSCSS=420PALDV":
  58. (st->codec->chroma_sample_location == AVCHROMA_LOC_LEFT) ?" C420mpeg2 XYSCSS=420MPEG2":
  59. " C420jpeg XYSCSS=420JPEG";
  60. break;
  61. case PIX_FMT_YUV422P:
  62. colorspace = " C422 XYSCSS=422";
  63. break;
  64. case PIX_FMT_YUV444P:
  65. colorspace = " C444 XYSCSS=444";
  66. break;
  67. }
  68. /* construct stream header, if this is the first frame */
  69. n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
  70. Y4M_MAGIC,
  71. width,
  72. height,
  73. raten, rated,
  74. inter,
  75. aspectn, aspectd,
  76. colorspace);
  77. return n;
  78. }
  79. static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
  80. {
  81. AVStream *st = s->streams[pkt->stream_index];
  82. AVIOContext *pb = s->pb;
  83. AVPicture *picture;
  84. int* first_pkt = s->priv_data;
  85. int width, height, h_chroma_shift, v_chroma_shift;
  86. int i;
  87. char buf2[Y4M_LINE_MAX+1];
  88. char buf1[20];
  89. uint8_t *ptr, *ptr1, *ptr2;
  90. picture = (AVPicture *)pkt->data;
  91. /* for the first packet we have to output the header as well */
  92. if (*first_pkt) {
  93. *first_pkt = 0;
  94. if (yuv4_generate_header(s, buf2) < 0) {
  95. av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
  96. return AVERROR(EIO);
  97. } else {
  98. avio_write(pb, buf2, strlen(buf2));
  99. }
  100. }
  101. /* construct frame header */
  102. snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
  103. avio_write(pb, buf1, strlen(buf1));
  104. width = st->codec->width;
  105. height = st->codec->height;
  106. ptr = picture->data[0];
  107. for(i=0;i<height;i++) {
  108. avio_write(pb, ptr, width);
  109. ptr += picture->linesize[0];
  110. }
  111. if (st->codec->pix_fmt != PIX_FMT_GRAY8){
  112. // Adjust for smaller Cb and Cr planes
  113. avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  114. width >>= h_chroma_shift;
  115. height >>= v_chroma_shift;
  116. ptr1 = picture->data[1];
  117. ptr2 = picture->data[2];
  118. for(i=0;i<height;i++) { /* Cb */
  119. avio_write(pb, ptr1, width);
  120. ptr1 += picture->linesize[1];
  121. }
  122. for(i=0;i<height;i++) { /* Cr */
  123. avio_write(pb, ptr2, width);
  124. ptr2 += picture->linesize[2];
  125. }
  126. }
  127. avio_flush(pb);
  128. return 0;
  129. }
  130. static int yuv4_write_header(AVFormatContext *s)
  131. {
  132. int* first_pkt = s->priv_data;
  133. if (s->nb_streams != 1)
  134. return AVERROR(EIO);
  135. if (s->streams[0]->codec->codec_id != CODEC_ID_RAWVIDEO) {
  136. av_log(s, AV_LOG_ERROR,
  137. "A non-rawvideo stream was selected, but yuv4mpeg only handles rawvideo streams\n");
  138. return AVERROR(EINVAL);
  139. }
  140. if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
  141. av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
  142. }
  143. else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
  144. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
  145. (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
  146. (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
  147. av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
  148. return AVERROR(EIO);
  149. }
  150. *first_pkt = 1;
  151. return 0;
  152. }
  153. AVOutputFormat ff_yuv4mpegpipe_muxer = {
  154. .name = "yuv4mpegpipe",
  155. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  156. .mime_type = "",
  157. .extensions = "y4m",
  158. .priv_data_size = sizeof(int),
  159. .audio_codec = CODEC_ID_NONE,
  160. .video_codec = CODEC_ID_RAWVIDEO,
  161. .write_header = yuv4_write_header,
  162. .write_packet = yuv4_write_packet,
  163. .flags = AVFMT_RAWPICTURE,
  164. };
  165. #endif
  166. /* Header size increased to allow room for optional flags */
  167. #define MAX_YUV4_HEADER 80
  168. #define MAX_FRAME_HEADER 80
  169. static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
  170. {
  171. char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
  172. char *tokstart,*tokend,*header_end;
  173. int i;
  174. AVIOContext *pb = s->pb;
  175. int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
  176. enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
  177. enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
  178. AVStream *st;
  179. struct frame_attributes *s1 = s->priv_data;
  180. for (i=0; i<MAX_YUV4_HEADER; i++) {
  181. header[i] = avio_r8(pb);
  182. if (header[i] == '\n') {
  183. header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
  184. header[i+2] = 0;
  185. break;
  186. }
  187. }
  188. if (i == MAX_YUV4_HEADER) return -1;
  189. if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
  190. s1->interlaced_frame = 0;
  191. s1->top_field_first = 0;
  192. header_end = &header[i+1]; // Include space
  193. for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
  194. if (*tokstart==0x20) continue;
  195. switch (*tokstart++) {
  196. case 'W': // Width. Required.
  197. width = strtol(tokstart, &tokend, 10);
  198. tokstart=tokend;
  199. break;
  200. case 'H': // Height. Required.
  201. height = strtol(tokstart, &tokend, 10);
  202. tokstart=tokend;
  203. break;
  204. case 'C': // Color space
  205. if (strncmp("420jpeg",tokstart,7)==0) {
  206. pix_fmt = PIX_FMT_YUV420P;
  207. chroma_sample_location = AVCHROMA_LOC_CENTER;
  208. } else if (strncmp("420mpeg2",tokstart,8)==0) {
  209. pix_fmt = PIX_FMT_YUV420P;
  210. chroma_sample_location = AVCHROMA_LOC_LEFT;
  211. } else if (strncmp("420paldv", tokstart, 8)==0) {
  212. pix_fmt = PIX_FMT_YUV420P;
  213. chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  214. } else if (strncmp("411", tokstart, 3)==0)
  215. pix_fmt = PIX_FMT_YUV411P;
  216. else if (strncmp("422", tokstart, 3)==0)
  217. pix_fmt = PIX_FMT_YUV422P;
  218. else if (strncmp("444alpha", tokstart, 8)==0) {
  219. av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
  220. return -1;
  221. } else if (strncmp("444", tokstart, 3)==0)
  222. pix_fmt = PIX_FMT_YUV444P;
  223. else if (strncmp("mono",tokstart, 4)==0) {
  224. pix_fmt = PIX_FMT_GRAY8;
  225. } else {
  226. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
  227. return -1;
  228. }
  229. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  230. break;
  231. case 'I': // Interlace type
  232. switch (*tokstart++){
  233. case '?':
  234. break;
  235. case 'p':
  236. s1->interlaced_frame=0;
  237. break;
  238. case 't':
  239. s1->interlaced_frame=1;
  240. s1->top_field_first=1;
  241. break;
  242. case 'b':
  243. s1->interlaced_frame=1;
  244. s1->top_field_first=0;
  245. break;
  246. case 'm':
  247. av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
  248. return -1;
  249. default:
  250. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  251. return -1;
  252. }
  253. break;
  254. case 'F': // Frame rate
  255. sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
  256. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  257. break;
  258. case 'A': // Pixel aspect
  259. sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
  260. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  261. break;
  262. case 'X': // Vendor extensions
  263. if (strncmp("YSCSS=",tokstart,6)==0) {
  264. // Older nonstandard pixel format representation
  265. tokstart+=6;
  266. if (strncmp("420JPEG",tokstart,7)==0)
  267. alt_pix_fmt=PIX_FMT_YUV420P;
  268. else if (strncmp("420MPEG2",tokstart,8)==0)
  269. alt_pix_fmt=PIX_FMT_YUV420P;
  270. else if (strncmp("420PALDV",tokstart,8)==0)
  271. alt_pix_fmt=PIX_FMT_YUV420P;
  272. else if (strncmp("411",tokstart,3)==0)
  273. alt_pix_fmt=PIX_FMT_YUV411P;
  274. else if (strncmp("422",tokstart,3)==0)
  275. alt_pix_fmt=PIX_FMT_YUV422P;
  276. else if (strncmp("444",tokstart,3)==0)
  277. alt_pix_fmt=PIX_FMT_YUV444P;
  278. }
  279. while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
  280. break;
  281. }
  282. }
  283. if ((width == -1) || (height == -1)) {
  284. av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
  285. return -1;
  286. }
  287. if (pix_fmt == PIX_FMT_NONE) {
  288. if (alt_pix_fmt == PIX_FMT_NONE)
  289. pix_fmt = PIX_FMT_YUV420P;
  290. else
  291. pix_fmt = alt_pix_fmt;
  292. }
  293. if (raten <= 0 || rated <= 0) {
  294. // Frame rate unknown
  295. raten = 25;
  296. rated = 1;
  297. }
  298. if (aspectn == 0 && aspectd == 0) {
  299. // Pixel aspect unknown
  300. aspectd = 1;
  301. }
  302. st = avformat_new_stream(s, NULL);
  303. if(!st)
  304. return AVERROR(ENOMEM);
  305. st->codec->width = width;
  306. st->codec->height = height;
  307. av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
  308. avpriv_set_pts_info(st, 64, rated, raten);
  309. st->codec->pix_fmt = pix_fmt;
  310. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  311. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  312. st->sample_aspect_ratio= (AVRational){aspectn, aspectd};
  313. st->codec->chroma_sample_location = chroma_sample_location;
  314. return 0;
  315. }
  316. static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
  317. {
  318. int i;
  319. char header[MAX_FRAME_HEADER+1];
  320. int packet_size, width, height;
  321. AVStream *st = s->streams[0];
  322. struct frame_attributes *s1 = s->priv_data;
  323. for (i=0; i<MAX_FRAME_HEADER; i++) {
  324. header[i] = avio_r8(s->pb);
  325. if (header[i] == '\n') {
  326. header[i+1] = 0;
  327. break;
  328. }
  329. }
  330. if (i == MAX_FRAME_HEADER) return -1;
  331. if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
  332. width = st->codec->width;
  333. height = st->codec->height;
  334. packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
  335. if (packet_size < 0)
  336. return -1;
  337. if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
  338. return AVERROR(EIO);
  339. if (s->streams[0]->codec->coded_frame) {
  340. s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
  341. s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
  342. }
  343. pkt->stream_index = 0;
  344. return 0;
  345. }
  346. static int yuv4_probe(AVProbeData *pd)
  347. {
  348. /* check file header */
  349. if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
  350. return AVPROBE_SCORE_MAX;
  351. else
  352. return 0;
  353. }
  354. #if CONFIG_YUV4MPEGPIPE_DEMUXER
  355. AVInputFormat ff_yuv4mpegpipe_demuxer = {
  356. .name = "yuv4mpegpipe",
  357. .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
  358. .priv_data_size = sizeof(struct frame_attributes),
  359. .read_probe = yuv4_probe,
  360. .read_header = yuv4_read_header,
  361. .read_packet = yuv4_read_packet,
  362. .extensions = "y4m"
  363. };
  364. #endif